0

I have two arrays say original[] and input[]. In the first array i have 3 values

1 
2 
3

and in the second array i give 2 values

2 
3

I want to check if these two values are present in the first array How can i do this?

templatetypedef
375k112 gold badges948 silver badges1.1k bronze badges
asked Mar 25, 2011 at 5:33

6 Answers 6

7
Integer original[] = {1,2,3};
Integer input[] = {2,3};
if (Arrays.asList(original).containsAll(Arrays.asList(input)))
{
 System.out.println("Yup, they're in there!");
}

Note that this won't work on primitive (int) arrays; you have to box them in Integer objects. If your arrays are String[] you're golden.

answered Mar 25, 2011 at 5:38
1
  • Gah, sorry - was doing this from memory, and the memory failed ... editing Commented Mar 25, 2011 at 5:50
1

I want to check if these two values are present in the first array How can i do this?

Iterate your second array and check it against the elements of first one.

answered Mar 25, 2011 at 5:38
2
  • @Xan: if the OP asked for some code, this question would have been closed. Commented Jul 24, 2012 at 15:43
  • Maybe he didn't check his email? Or he don't care? Commented Jul 24, 2012 at 16:02
1

If the larger array is sorted, check out the binarySearch methods in http://download.oracle.com/javase/1.4.2/docs/api/java/util/Arrays.html

Iterate through your second array and search for each one in the array.

answered Mar 25, 2011 at 5:39
1

If you're interested in the query "is the value X contained in my array?," then you might want to considered switching data structures. Arrays are good for storing finite lists of elements in sequence, and the main properties of array elements are their positions. If you want to store the elements so that you can easily check whether or not a given element is present, I'd strongly suggest switching to storing the elements in a Set, since this interface more naturally supports this idea. Moreover, both HashSet and TreeSet can do lookup queries faster than a standard search over an array.

That said, if you must stick with arrays, then any of the other answers here should be perfectly fine. I'd just suggest reevaluating whether or not you should be using arrays in the first place.

answered Mar 25, 2011 at 5:41
0

Take the first array apply the search logic onto it by getting one element form your second array and repeat this until the last element not finished. You will get help from this link how to make search in array. http://www.roseindia.net/java/beginners/OccurancesInArray.shtml

answered Mar 25, 2011 at 5:46
0
for(int i=0;i<=orignal.length;i++) {
 if(orignal[0]==input[i] | orignal[0]==input[++i]) {
 System.out.println("Yup, they're in there!");
 }
}

This will surely work.

answered Mar 25, 2011 at 7:43

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.