Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Optiknights · Jun 23, 2014 at 11:59 PM · c#arrayprogramming

Jagged Array

Hi I am trying to create a Jagged array, however I keep getting errors in the compiler such as invalid rank specifier.

 int[][] numbers = new int[][]
         {
         numbers[0] = new int[] {0,0,0,0},
         numbers[1] = new int[] {0,0,0,0,0,0}
     };
            
     }
 
     numbers[0][0] = 1;
     numbers[1][0] = 2;
     numbers[2][0] = 3;
     numbers[3][0] = 4;
 
     numbers[0][1] = 5;
     numbers[1][1] = 6;
     numbers[2][1] = 7;
     numbers[3][1] = 8;
 
     numbers[0][2] = 9;
     numbers[1][2] = 10;
     numbers[2][2] = 11;
     numbers[3][2] = 12;
 
     numbers[0][3] = 13;
     numbers[1][3] = 14;
     numbers[2][3] = 15;
     numbers[3][3] = 16;
 
     numbers[0][4] = 17;
     numbers[1][4] = 18;
     numbers[2][4] = 19;
     numbers[3][4] = 20;
 
     numbers[0][5] = 21;
     numbers[1][5] = 22;
     numbers[2][5] = 23;
     numbers[3][5] = 24;

Please someone tell me what I am doing wrong...

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Jun 24, 2014 at 12:03 AM 0
Share

As you are using 4 elements in the first field, shouldn't you initialise them? You did 2, why not the rest?

avatar image Optiknights meat5000 ♦ · Jun 24, 2014 at 12:11 AM 0
Share

I do not know what you mean, by initializing numbers[0] = new int[] {0,0,0,0} numbers[1] = new int[] {0,0,0,0,0,0} this creates the full 24 set 4x6 grid needed.

numbers[0][0] = 1; should be correct as it is pointing to a single int unless I am mistaken??

avatar image Clet_ meat5000 ♦ · Jun 24, 2014 at 12:14 AM 0
Share

Absolutely not. Look at my answer, then take a look at the jagged array page on $$anonymous$$SDN

avatar image meat5000 ♦ meat5000 ♦ · Jun 24, 2014 at 01:07 AM 0
Share

I had a look. I'm quite new to C# so I posted out of curiosity really :)

I saw the declaration on $$anonymous$$SDN which made what OP wrote look legit. I was confused as to why he initialised

 numbers[0] = new int[] {0,0,0,0},
 numbers[1] = new int[] {0,0,0,0,0,0}

but used

 numbers[2][0] = 3;
 numbers[3][0] = 4;

A 4x6 would be

 numbers[0] = new int[] {0,0,0,0,0,0},
 numbers[1] = new int[] {0,0,0,0,0,0}
 numbers[2] = new int[] {0,0,0,0,0,0},
 numbers[3] = new int[] {0,0,0,0,0,0}


I see it now, but what really confuses me is this:

The following is a declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers: C#

  int[][] jaggedArray = new int[3][];

Isn't that actually just a 2D array?

avatar image Clet_ · Jun 24, 2014 at 01:20 AM 0
Share

Yes, it is. It is a single dimension array that contains arrays, soooooooo a 2D array

avatar image meat5000 ♦ · Jun 24, 2014 at 01:24 AM 0
Share

So, what's the point of "Jagged Array" :D

Is it simply that it doesn't have to be a block? Less padding but isn't it less efficient than a block array, choosing memory saving over processing?

avatar image Bunny83 · Jun 24, 2014 at 02:42 AM 1
Share

Jagged arrays are usually workarounds in languages which doesn't support multidimensional arrays. And yes, they don't require one large memory block as they are build up with several smaller arrays. They also allow to have different array sizes in each sub array.

If you don't need any of those and your array isn't that large it's way easier to use a multidimensional array.

avatar image meat5000 ♦ · Jun 24, 2014 at 10:12 AM 0
Share

Thanks Bunny83 :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Clet_ · Jun 24, 2014 at 12:11 AM

Edited: Updated answer. Jagged array was initially wrong. Thanks to @Bunny83

Initializer are used to Initialize an array. Your declaration is all over the place. What you want to have (I think) is a int[4][6].

Your first lines should be :

 int[][] numbers = new int[][] {
     new int[6],
     new int[6],
     new int[6],
     new int[6]
 }
 

OR

 new int[][] numbers = new int[][] {
     new int[6],
     new int[6],
     new int[6],
     new int[6]
 }

In summary, you need to initialize each row of a jagged array in the initalizer for it to work accordingly. Also, array values are always created with the default value of the specified type, so you don't need to fill it with zeroes, since 0 is the default value of an int.

Also, by using a simple logic, you can GREATLY reduce the number of lines to initialize your array.

 int count = 1;
 for(int i = 0; i < 6; i++) {
     for(int j = 0; j < 4; j++) {
         numbers[j][i] = count++;
     }
 }

Work smarter, not harder

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Optiknights · Jun 24, 2014 at 12:17 AM 0
Share

Thanks a lot! Learning to work smarter SLOWLY haha I am doing this to replace a 500 line logic matrix... I will get there eventually!

avatar image Clet_ · Jun 24, 2014 at 12:20 AM 1
Share

If a day pass without me learning anything new, I consider it a bad day. Just keep improving an read, read, read.

It is very helpful to get lost in the $$anonymous$$SDN documentations.

avatar image Clet_ · Jun 24, 2014 at 12:26 AM 0
Share

Today I learned that you can have different array's length in a jagged array... like

 int[][] jaggedArray = new int[2][] {
     new int[3],
     new int[5]
 }
avatar image Bunny83 · Jun 24, 2014 at 01:24 AM 0
Share

@Clet_: Could you please fix your array initialization in your answer? Because you can't initialize a jagged array like this. You always have to create all of the inner arrays manually like you did in your comment. To initialize a 4 by 6 jagged array you have to do:

 int[][] numbers = new int[4][] {
     new int[6],
     new int[6],
     new int[6],
     new int[6]
 };

Only multidimensional arrays can be initialized like this:

 int[,] numbers = new int[4,6];
avatar image Bunny83 · Jun 24, 2014 at 01:34 AM 0
Share

btw: If you use an array initializer you can omit the size of the array. If you specify a size explicitly the number of values in the initializer has to match the given length.

 int[][] numbers = new int[][] {
     new int[6],
     new int[6],
     new int[6],
     new int[6]
 };


avatar image
0

Answer by sj631 · Sep 24, 2021 at 05:14 AM

 int[][] array = new int[rowCount][];
 for (int i = 0; i < rowCount; i++)
 {
     array[i] = new int[columnCount];
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Help using LitJson 1 Answer

Index Out of Range Exception - can't figure it out 1 Answer

Distribute terrain in zones 3 Answers

Passing an Array between classes 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges