2

Is there any way to populate the rows of an already declared multidimensional array without a for loop? can I now add in a row of information at one time?

ex: I declare an array 3 x 3

int[][] newArray = new int[3][3];

I've tried to now do something like this

newArray[1] = {143, 124, 453};

but I get tons of errors, and changing the brackets to other things doesn't help.

IamJohnny5
  • 33
  • 4
  • possible duplicate of [String array initialization as constructor parameter](http://stackoverflow.com/questions/4436458/string-array-initialization-as-constructor-parameter) – jmj Mar 01 '14 at 00:28
  • 3
    Try `newArray[1] = new int [] {143, 124, 453};` – Gene Mar 01 '14 at 00:29

2 Answers2

1

If you need to set all the fields in a row at once, you either need to set them up during construction using

int[][] newArray = { {1,2,3}, {2,3,4}, {3,4,5} }

or if you want to do this at a later time, you can construct a new one dimensional array and assign that to a row, such as

int[] row = {2,3,4};
newArray[1] = row;
Warlord
  • 2,798
  • 16
  • 21
1
int[][] newArray = new int[3][3];

newArray [0]=new int[]{10,13,14};
newArray [1]=new int[]{10,13,14};
newArray [2]=new int[]{10,13,14};

or

newArray=new int[][] {{1,2,3},{4,5,6},{7,8,9}};
SteveL
  • 3,331
  • 4
  • 32
  • 57