armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

• 0

# Array of arrays

## Question

Hello friends.

I am trying to find a way to create an array with many arrays using integers. As example I have :

int sequence1[ ] = new int[ ] {1, 2, 3, 4, 5};

int sequence2[ ] = new int[ ] {6, 7, 8, 9, 10};

int sequence3[ ] = new int[ ] {11, 12, 13, 14, 15};

etc.

I want something like this main principle (but in one line if it is possible) :

int allSequences[ ] [ ];

allSequences[0] [ ]= sequence1;

allSequences[1] [ ]= sequence2;

allSequences[2] [ ]= sequence3;

so allSequences[1][3] is 9, allSequences[2][2] is 13, etc.

I would like to create only one array with all these arrays, but I tried many ways - and it doesn't work because I cannot find the right semantic.

It became a little bit confusing for me. Do you have an idea? Thank you ++

## Recommended Posts

• 1

I prefer this one - thank you for your help. I really appreciate ++

##### Share on other sites

• 1

Don't be sorry boy. As I said you really helped me - and I have to do the work ++

##### Share on other sites
• 0

dude its basics:

int[][] allSequences = new int[][] {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};

• 0

##### Share on other sites
• 0

Again me. This part of code is driving me crazy. I didn't understand something and I cannot find any efficient answer on Unity forums. I did :

int[] seq0 = new int[] { 1, 2, 3, 4 };
int[] seq1 = new int[] { 5, 6, 7, 8 };
int[] seq2 = new int[] { 1, 3, 2, 1 };
int[] seq3 = new int[] { 5, 4, 3, 2 };

int[][] allSequences = new int[][] {};

allSequences = new int[][] {
seq0,
seq1,
seq2,
seq3 };

So I have all sequences in a unique array of arrays, but I would like to read all segments in a loop - the first seq0, when it is over the second (seq1), then the third (seq2), etc - but sometimes they have a different sizes - so I use their lenght as limit. I am stucking on this part of code. Really a help will be very appreciated. Thank you my friends ++

##### Share on other sites
• 0

//you mean something like this?:

for(int i=0; i<allSequences.length; i++){

for(int j=0; j<allSequences.length; j++){

something = redStuff();

allSequences[j] = something;

}

}

##### Share on other sites
• 0

Hello. I need to create a double loop - one for each segment, reading each integer inside them.

[0] [0] do something (checking lenght)

[0] [1] do something (checking lenght)

[0] [2] do something (checking lenght)

[0] [3] do something (checking lenght) - it read all integers

[1] [0] do something (checking lenght)

[1] [1] do something (checking lenght)

[1] [2] do something (checking lenght)

[1] [3] do something (checking lenght) - it read all integers

[2] [0] do something (checking lenght) ...

etc etc - until it read all sequences

To do simple, I want to read all sequences as a single line, starting at [0][0] to [3][3].

I don't understand how I can increment for each array(s) its index. Thank you ++

##### Share on other sites
• 0

That is actually what i just posted.

Havent coded in c# a while so you might need to use .Length ... not sure if it was captial, but other than that my code does exactly that. Loops thorough the 1st array then goes to the next and so on...

In the for loop the condition is the length of the array so arrays with different sizes wont be a problem.

##### Share on other sites
• 0

Ok. Thank you for your explaination. I have no time today - I will test it tomorow ++

##### Share on other sites
• 0

I tested you code today and it doesn't work as expected because there is a little mistake. You used the same lenght for all arrays (even the main).

allSequences.Lenght is not allSequences[sq].Lenght - for each sub arrays...

However you helped me so as to enlighten this part. Thank you ++

##### Share on other sites
• 0

yeah youre right it should be:

sorry bout that ^^

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.