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.

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

@DwB I didn't mean to suggest it was better than a loop, just an alternative.
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.
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.

Comments

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)

2 Comments

Not sure where to even start for a data structure. Would I use Java Dictionaries?
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".

Your Answer

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.