2

Given two lists of numbers(called listA and listB),I need to write a Java method which returns a boolean value to state whether or not all the elements in listA are contained in listB.

I can't quite get my code to work as it always seems to be returning false.

public static boolean containsAll( int[] listA, int[] listB) { boolean containsAll = true; int ctr1 = 0, ctr2 = 0; while(ctr1 < listA.length && containsAll) { ctr2 = 0; while(ctr2<listB.length && containsAll) { if(listA[ctr1] != listB[ctr2]) containsAll = false; ctr2++; } ctr1++; } return containsAll; } public static void testContainsAll() { Scanner input1 = new Scanner(System.in); System.out.println("Enter size of first array"); int array1[] = new int[input1.nextInt()]; System.out.println("Enter number of ints required"); for(int i=0; i<array1.length; i++) { array1[i] = input1.nextInt(); } Scanner input2 = new Scanner(System.in); System.out.println("Enter size of second array"); int array2[] = new int[input2.nextInt()]; System.out.println("Enter number of ints required"); for(int i=0; i<array2.length; i++) { array2[i] = input2.nextInt(); } boolean containsAll = containsAll(array1,array2); if(containsAll == true) System.out.print("listB contains all elements of listA"); else System.out.print("listB doesn't contain all elements of listA"); } 
7
  • 1
    Have you stepped through the code in your debugger? What did you find? Commented Mar 2, 2017 at 2:13
  • You can simply transform each array into a HashSet and check their intersection. Commented Mar 2, 2017 at 2:14
  • @JacobG. Not simply. Commented Mar 2, 2017 at 2:14
  • @shmosel Why not? new HashSet<>(Arrays.asList(array)).retainAll(otherSet); Commented Mar 2, 2017 at 2:15
  • @JacobG. Try it and let me know. Commented Mar 2, 2017 at 2:16

3 Answers 3

1

You can't set containsAll to false as soon as you find a different element. It's only false if no elements match, which you don't know until the inner loop completes. Here's a simple implementation:

public static boolean containsAll(int[] listA, int[] listB) { outer: for (int a : listA) { for (int b : listB) { if (a == b) { continue outer; } } return false; } return true; } 

Or if you dislike gotos:

public static boolean containsAll(int[] listA, int[] listB) { for (int a : listA) { if (!contains(listB, a)) { return false; } } return true; } private static boolean contains(int[] list, int i) { for (int e : list) { if (e == i) { return true; } } return false; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks shmosel didn't realise i was setting containsAll to false in the inner loop so that it wasn't really checking against all the elements in the second array
0

An easy way to go about this is to convert each array into a Set, and then use Set#retainAll to determine equality.

int[] a = {1, 2, 3, 4, 5}; int[] b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Set<Integer> A = Arrays.stream(a).boxed().collect(Collectors.toSet()); Set<Integer> B = Arrays.stream(b).boxed().collect(Collectors.toSet()); System.out.println(B.containsAll(A)); 

This will print true if every element in A is in B, otherwise false.

Thanks to shmosel for recommending Set#containsAll!

2 Comments

Boxing into temporary collections seems a little excessive to me. Btw you can just call containsAll().
True, containsAll() would be simpler. I'll edit my post. However I don't think OP will have to deal with a large amount of elements due to using a Scanner for user-input to populate each array, so boxing shouldn't be an issue.
0

You can initialize a HashSet from the int[] and then check if the array contains all elements by calling containsAll

public static boolean containsAll(int[] listA, int[] listB){ Set<Integer> a = Arrays.stream(listA).boxed().collect(Collectors.toSet()); Set<Integer> b = Arrays.stream(listB).boxed().collect(Collectors.toSet()); return a.containsAll(b); } 

2 Comments

You can't wrap a primitive array in Arrays.asList().
Why would you collect to a list and then copy to a set?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.