0

I am creating an array of arrays such that:

var arrNewArray = new string[arrOldArray.Length][7];

arrOldArray is an array of arrays such that it's [X][4], meaning the length of the 1st array or "outside" array can change, but the length of the "inside" array is ALWAYS 4, or hold 4 strings ([0][1][2][3]).

Why won't the compiler accept my statement above?

Essentially, I'm trying to take arrOldArray and expand it, or add a few more "columns" by increasing the [4] in the old array to a [7] in the new array and then copy the contents over. Perhaps I'm not doing it the best/efficient way, so any guidance would be appreciated thanks.

4
  • 1
    It sounds like you really need a List<> instead of an array here. Commented Jan 24, 2013 at 16:58
  • Is it impossible to do with the array? If so, I will read up on Lists. Commented Jan 24, 2013 at 16:59
  • @phan it's not that it's impossible but that you're not using arrays idiomatically. You want a growable collection of some item, that's a List<T>. Commented Jan 24, 2013 at 17:12
  • @Joel. My constraint is that the old data already comes in as an array of arrays, [][]. I just really want to add a few more columns to insert new data such that [][4] becomes [][7], or [][X] where X can be anything I want. How would you transfer an array of arrays of [x][4] dimensions into a list? Would it be a list of lists? Perhaps, the answer to that should be a new thread. Commented Jan 24, 2013 at 17:35

6 Answers 6

4

I think you want a two dimensional array:

var arrNewArray = new string[arrOldArray.Length, 7];

You would access it like this: arrNewArray[x, y].

This is better than a jagged array, because it clearly communicates that the number of "columns" is the same for every row.

If you want to continue using a jagged array, you need to do it like this:

var arrNewArray = new string[arrOldArray.Length][];
for(int i = 0; i < arrOldArray.Length; ++i)
    arrNewArray[i] = new string[7];

The reason for this convoluted way is: With a jagged array, each "row" can have a different number of "columns". A short-hand syntax for the case where each "row" has the same number of "columns" doesn't exist. That's why your code doesn't compile.
A jagged array is essential an array of arrays, so you need to create a new array instance for each "row" of the outer array and explicitly assign it. That's what the for loop is doing.

You can't use Array.Copy with jagged arrays. Each child-array is it's own instance and Array.Copy doesn't make a deep copy, it merely copies the references from one array to another. The effect would be, that both arrays would point to the same items and changing an item in one array would be seen from the other.

Sign up to request clarification or add additional context in comments.

4 Comments

No, that's actually a multi-dimensional array whereas I think I have a jagged array.
I guess I pointed out the difference because I am not sure if Array.Copy would still work if my old data was in a jagged array (albeit of the same size), and the new array was a multi-dimensional array. Is that possible? To copy a jagged array directly into a multi-dimensional one? In any case, your new addition to your answer addressed what I was looking for. Thank you!
@phan: Please see the last paragraph on why Array.Copy can't be used with jagged arrays to achieve what you want.
Daniel, thank you so very much for explaining the interaction between Array.Copy and jagged arrays. This clears it up for me.
2

You are not creating the jagged array properly. The proper way is to create the first dimension of the jagged array and then loop through the items of the first dimension to create the nested arrays and copy the data from the old arrays. Here's an example:

int newSize = 7;
string[][] newArray = new string[oldArray.Length][];
for (int i = 0; i < oldArray.Length; i++)
{
    newArray[i] = new string[newSize];
    Array.Copy(oldArray[i], newArray[i], oldArray[i].Length);
}

1 Comment

I was torn between Daniel's first-in-time answer and Pavel's. Ultimately, Daniel helped me understand what you can't do with Array.Copy and jagged arrays, and Pavel showed me what you can do with Array.Copy in his code example.
1

You would be wanting

var arrNewArray = new string[arrOldArray.Length, 7];

6 Comments

No, that's actually a multi-dimensional array whereas I think I have a jagged array.
Jagged arrays are used for when each one needs to be a different length, and you stated that isn't the case.
This doesn't actually answer the question, "Why won't the compiler accept my statement above?" Also note that there are other reasons to use a jagged array than just to allow different lengths.
@Matt Right, but my original data, arrOldArray, came in this format: [x][4]. If I use your multi-dimensional array, can I still use Array.Copy to copy the contents of arrOldArray into arrNewArray?
@phan - if your existing data is in a jagged array, then there isn't a succinct way of copying the content to a new array with a different dimension - so you could use array.copy to copy the 4 from each element in your original array. What might be simpler is to just use Array.Resize on each element of your original array.
|
0
var arrNewArray = new[] {new string[7]};//array of arrays
var arrNewArray = new string[arrOldArray.Length, 7];//two-dimensional array

1 Comment

The result of your first line would be an array with one row and seven columns. Not with arrOldArray.Length rows and 7 columns.
0

Using Linq:

int[][] jaggedArray2 = new int[][] 
{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};
int length = jaggedArray.Sum(a => a.Length);

4 Comments

That's not answering the question that is asked.
Linq is fun and all but really? That's not even an answer to the question.
Read the title again. It answers the question.
Actually, thanks Jani. I am actually learning LINQ/lambda expressions too and found this to be enlightening!
0

I don't believe what you're asking is directly possible. Because the syntax that you are using is for a jagged array, and what you are doing is effectively asking it to create a multi-dimensional array.

The syntax is confusing since it reads like what you really want is a multi-dimensional array (although I'm aware that's not the case.)

I don't believe you could store your arrays in the newly allocated array either due to a size change. You would need to build a custom copy method to move the data into the larger array.

1 Comment

I think ultimately a multi-dimensional array or list or list of lists is the ideal way to go. However, I was having trouble understanding how I would copy my old data, which is an array of arrays/jagged array, into a new data structure (whichever it may be) while still potentially using Array.Copy (which seems to be the simplest method).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.