[Solved] java multi-dimensional array split [closed]


I hope I’ve understood correctly what you wanted to ask.

First of all as it was already mentioned you can’t store integers in String array.

Secondly, in Java two-dimensional array is actually the array of arrays. So when you declare int[][] nums = int[4][3] you create an int[] array nums that has four elemets and each of these elements is another int[] array of length 3. So if you imagine that your two-dimentional array is kind of matrix you can easily retreive it’s “rows” as elements of nums array.

int[][] nums = {{32, 123, 74}, {543, 98, 5}, {96, 24, 23}, {12, 98, 56}};

int[] rowOne = nums[0];     // {32, 123, 74}
int[] rowTwo = nums[1];     // {543, 98, 5}
int[] rowThree = nums[2];   // {96, 24, 23}
int[] rowFour = nums[3];    // {12, 98, 56}

Getting “columns” is a little bit trickier a long as they just don’t exist in terms of java. But you still can do this as follows:

int[] columnOne = new int[nums.length];
for (int i = 0; i < columnOne.length; i++) {
  columnOne[i] = nums[i][0]; // {32, 543, 96, 12}
}

int[] columnTwo = new int[nums.length];
for (int i = 0; i < columnTwo.length; i++) {
  columnTwo[i] = nums[i][1]; // {123, 98, 24, 98}
}

int[] columnThree = new int[nums.length];
for (int i = 0; i < columnThree.length; i++) {
  columnThree[i] = nums[i][2]; // {74, 5, 23, 56}
}

solved java multi-dimensional array split [closed]