473,394 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Array reallocation question

Are existing elements in the array preserved when array is reallocated ?

int[] A = new int[1];
A[0] = 1;
A = new int[ A.Length + 1 ];
A[1] = 2;

Will A[0] be 1 at this point ?

Oleg Subachev
Oct 5 '06 #1
2 2084
No, they aren't. the new array is wiped to 0s. You could, however, create
the new array in a second variable, then oldArray.CopyTo(newArray, 0) to get
the values from the old array to the new array.

However, if you are going to be doing this often, then maybe you are using
the wrong object structure? Maybe use a list or collection wrapper?

Marc
Oct 5 '06 #2
What happens when you run the code in the debugger?

"Oleg Subachev" <ol**@urvb.ruwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Are existing elements in the array preserved when array is reallocated ?

int[] A = new int[1];
A[0] = 1;
A = new int[ A.Length + 1 ];
A[1] = 2;

Will A[0] be 1 at this point ?

Oleg Subachev


Oct 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Davids | last post by:
is it true that I cannot dynamically add an item to an array? Eg public char = {"a","b"}; char.Add("newitem"); Do I really have to switch to ArrayList to do this?
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
4
by: adamrobillard | last post by:
Can you redim an array in C++. // I need 10 integers int *pInteger = new int; // Oops, now I need to store 11 Can you allocate memory at the end of this block, or would you need to create...
12
by: Maxwell2006 | last post by:
Hi, I declared an array like this: string scriptArgs = new string; Can I resize the array later in the code? Thank you, Max
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
27
by: pkirk25 | last post by:
Assume an array of structs that is having rows added at random. By the time it reaches your function, you have no idea if it has a few hundred over over 10000 rows. When your function recieves...
24
by: VijaKhara | last post by:
hi all, i am trying to create a dynamic 2D array with size N x 3 (N will be put in as a parameter) using the following code: int **xyz; int i,N; N=30000; xyz=malloc(3*sizeof(int*));
5
by: Andreas Schmitt | last post by:
Hi, I recently worked on an open source project and tried to make on of the arrays they are using dynamically allocated to get rid of the max size. I used the realloc instead of the usual C++...
4
by: Rakesh Kumar | last post by:
Hi All - In a project of mine - I was trying to scale down the actual issue to the following piece of code. I need to allocate an array of strings and reserve the individual string to a particular...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.