Jump to content

Jagged Array Initializers

- - - - -

  • Please log in to reply
3 replies to this topic

#1
photoScan

photoScan

    Newbie

  • Members
  • Pip
  • 5 posts
Hi again,


I need some way of initializing a jagged array that emulates the following code:


           for (int i = 0; i < 7; i++) 

		{

			myArray.IndexAndColor[i] = new int[3];

		}

		

		for (int n = 0; n < 7; n++) 

		{

			myArray.IndexAndColor[n][0] = 1; 

			myArray.IndexAndColor[n][1] = 0; 

			myArray.IndexAndColor[n][2] = 0; 

		}



#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
int[][] myarray = {

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

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

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

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

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

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

    new int[] {1, 0, 0}

};


#3
photoScan

photoScan

    Newbie

  • Members
  • Pip
  • 5 posts
TY, and if I may, would you have a more preferred method given this concept: a table of food items each having their own specific assigned value, you pick one up and know its value, and you also notice that this item can have three states, 0, 1 or 2, which can change over time in any way such as 2->1 or 1->2 or 0->1 or 0->2 .. etc. given that idea, would YOU choose a jagged array?

#4
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
It depends. Do they always have three states, or can they have more or less? If it is a constant, I'd use a standard array. If variable, a jagged array.

More likely I'd create a 'food interface', derive a new class from this interface for each food and have a method that manages the state of the food and maintain a List<IFood> of them all :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users