4

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.

Arjit
3,4761 gold badge19 silver badges18 bronze badges
asked Apr 14, 2014 at 20:28
6
  • 5
    What's wrong with using a loop? Even contains() uses a loop internally. Commented Apr 14, 2014 at 20:31
  • 2
    Recursion would work :) Commented Apr 14, 2014 at 20:32
  • 1
    @zgc7009 better yet, serialize the array as XML and then search for the text. Everything is better with XML :-) Commented Apr 14, 2014 at 20:33
  • @Ted, I'm just trying to save myself time if possible Commented Apr 15, 2014 at 14:04
  • @zgc, how would I use recursion? I'd have to know if the row contained the value before I knew which row to check, wouldn't I? Commented Apr 15, 2014 at 14:05

3 Answers 3

2

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 Apr 14, 2014 at 21:01
2
  • @DwB I didn't mean to suggest it was better than a loop, just an alternative. Commented 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. Commented Apr 15, 2014 at 14:00
1

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.

Lajos Arpad
79.7k42 gold badges121 silver badges231 bronze badges
answered Apr 15, 2014 at 14:11
0

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)

answered Apr 14, 2014 at 20:41
2
  • Not sure where to even start for a data structure. Would I use Java Dictionaries? Commented 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". Commented Apr 15, 2014 at 16:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.