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.
3 Answers 3
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);
}
-
@DwB I didn't mean to suggest it was better than a loop, just an alternative.deanosaur– deanosaur2014年04月14日 21:27:13 +00:00Commented Apr 14, 2014 at 21:27
-
It certainly works, even though it doesn't answer the question, but maybe the answer is that its impossible. If I don't see any more feasible alternatives in a few days, I'll select yours.user2956947– user29569472014年04月15日 14:00:22 +00:00Commented Apr 15, 2014 at 14:00
You can do almost anything with recursion if you care to headache your way through the logic of it. In this case it shouldn't be too hard
private boolean checkForValue(int val, int row, int col){
if(row == numRows && col == numCols)
return false;
else{
if(values[row][col] == val)
return true
else if(col < (numCols - 1))
checkForValue(val, row, col + 1);
else
checkForValue(val, row + 1, 1);
}
}
However, if you are just wanting to save time I think the for loop really is pretty efficient to start
private boolean checkForValue(int val){
for(int i = 0; i < numRows; i++){
for(int j = 0; j < numCols; j++){
if(values[i][j] == val) return true;
}
}
return false; //it will reach here if return true was not called.
}
Neither is too rough.
Yes.
You can use Bloom filters (http://en.wikipedia.org/wiki/Bloom_filter) or create a tree-based index for the keys of your Array, such as a Trie (http://en.wikipedia.org/wiki/Trie)
Basically you'd need a data structure to look for the values, and not for the keys. It would not cost much space or speed since you could re-use the references of the value objects on both data structures (yours and the one you elect)
-
Not sure where to even start for a data structure. Would I use Java Dictionaries?user2956947– user29569472014年04月15日 13:58:45 +00:00Commented Apr 15, 2014 at 13:58
-
Well, think in term of a database. A database doesn't work like an array where the elements are accessed via a positional index. They work with a second data structure ( for example primary keys ) that help find the desired row. What I am suggesting here is, since you don't want to use the positional array, that you can introduce a secondary structure, which would serve as an index for your array. Anyway, your question was "Is it possible?" The answer is "Yes".Alexandre Santos– Alexandre Santos2014年04月15日 16:30:51 +00:00Commented Apr 15, 2014 at 16:30
Explore related questions
See similar questions with these tags.
contains()
uses a loop internally.