Issue
Without a for loop
, is there any way to see if a value exists in a multidimensional array
? I found
Arrays.asList(*ArrayName*).contains(*itemToFind*)
but that will only search the first dimension of the array, and I need to search 2 dimensions.
Solution
I've created a two-dimensional array that contains 5 rows and 5 columns. The array is an int type and have initialized with a value i*j. Already exists a method that takes a row number and value to search for.
private static Integer[][] myarray = new Integer[5][5];
public static boolean exists(int row, int value) {
if(row >= myarray.length) return false;
List<Integer> rowvalues = Arrays.asList(Arrays.asList(myarray).get(row));
if(rowvalues.contains(value)) return true;
return exists(row+1, value);
}
Answered By - deanosaur
Answer Checked By - Katrina (JavaFixing Volunteer)