I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code:
public class MyClass {
public s
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
double[][] test = { {100}, {200}, {300}, {400}, {500}, {600}, {700}, {800}, {900}, {1000}};
int [][] removeRow = { {0}, {1}, {3}, {4}, };
double[][] newTest = new double[test.length - removeRow.length][test[0].length];
for (int i = 0, j = 0, k = 0; i < test.length; i++) {
if (j < removeRow.length) {
if (i == removeRow[j][0]) {
j++;
continue;
}
}
newTest[k][0] = test[i][0];
k++;
}
System.out.println(Arrays.deepToString(newTest));
}
}
Consider
public static void main(String[] args) {
int[][] foo = new int[][] {
new int[] { 1, 2, 3 },
new int[] { 1, 2, 3, 4},
};
System.out.println(foo.length); //2
System.out.println(foo[0].length); //3
System.out.println(foo[1].length); //4
}
Column lengths differ per row. If you're backing some data by a fixed size 2D array, then provide getters to the fixed values in a wrapper class.
It is also possible to get the quantity of elements of a 2d-array row, using "Arrays.toString(arr[x])", or total quantity of elements of a 2d/3d-array, using "Arrays.deepToString(arr)". It will give out a string, consisting of all the elements of the array separated by commas (rows are also limited with square brackets). AND: the main idea is that the total quantity of commas will be by 1 less than the total number of array / array row elements! Then you create one more string consisting only of all the commas (removing everything else using a regex), and its length + 1 will be the result we need. Examples:
public static int getNumberOfElementsIn2DArray() {
int[][] arr = {
{1, 2, 3, 1, 452},
{2, 4, 5, 123, 1, 22},
{23, 45},
{1, 122, 33},
{99, 98, 97},
{6, 4, 1, 1, 2, 8, 9, 5}
};
String str0 = Arrays.deepToString(arr);
String str = str0.replaceAll("[^,]","");
return str.length() + 1;
}
It will return "27";
public static int getNumberOfElementsIn2DArrayRow() {
int[][] arr = {
{1, 2, 3, 1, 452},
{2, 4, 5, 123, 1, 22},
{23, 45},
{1, 122, 33},
{99, 98, 97},
{6, 4, 1, 1, 2, 8, 9, 5}
};
String str0 = Arrays.toString(arr[5]);
String str = str0.replaceAll("[^,]","");
return str.length() + 1;
}
It will return "8".
Note! This method will return a wrong result if your multidimensional array is a String- or char array, in which content of elements contains commas :D
It was really hard to remember that
int numberOfColumns = arr.length;
int numberOfRows = arr[0].length;
Let's understand why this is so and how we can figure this out when we're given an array problem. From the below code we can see that rows = 4 and columns = 3:
int[][] arr = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3} };
arr
has multiple arrays in it, and these arrays can be arranged in a vertical manner to get the number of columns. To get the number of rows, we need to access the first array and consider its length. In this case, we access [1, 1, 1, 1] and thus, the number of rows = 4. When you're given a problem where you can't see the array, you can visualize the array as a rectangle with n X m dimensions and conclude that we can get the number of rows by accessing the first array then its length. The other one (arr.length
) is for the columns.
String [][] example = {{{"Please!", "Thanks"}, {"Hello!", "Hey", "Hi!"}},
{{"Why?", "Where?", "When?", "Who?"}, {"Yes!"}}};
example.length;
= 2
example[0].length;
= 2
example[1].length;
= 2
example[0][1].length;
= 3
example[1][0].length;
= 4
There's not a cleaner way at the language level because not all multidimensional arrays are rectangular. Sometimes jagged (differing column lengths) arrays are necessary.
You could easy create your own class to abstract the functionality you need.
If you aren't limited to arrays, then perhaps some of the various collection classes would work as well, like a Multimap.