0

I have written the code for a program that takes two Lists and returns true if all the elements of the second array also occur in the first array. However, this code only works for two arrays that are equal in size (e.g {1, 2, 3} and {3, 2, 1} returns true) but if the first array is larger than the second I get a rangeCheck exception (was fully expecting this, to be fair).

import java.util.*;

public class Ex8 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("How many numbers do you wish to enter into the 
array?");
        int thisMany = scanner.nextInt();
        ArrayList<Integer> numbers = new ArrayList<Integer>();
        System.out.println("Please enter some numbers, separated by 
spaces");
        for(int i = 0; i<thisMany; i++) {
            int x = scanner.nextInt();
            numbers.add(x);
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("How many numbers do you wish to enter into the 
second array?");
        int nowThisMany = sc.nextInt();
        ArrayList<Integer> numbers2 = new ArrayList<Integer>();
        System.out.println("Enter some numbers, separated by spaces");
        for(int j = 0; j<nowThisMany; j++) {
            int y = sc.nextInt();
            numbers2.add(y);
        }
        System.out.println(Arrays.toString(numbers.toArray()));
        System.out.println(Arrays.toString(numbers2.toArray()));
        isContained(numbers, numbers2);
        System.out.println(isContained(numbers, numbers2));
    }

    public static boolean isContained(ArrayList<Integer> numbers, 
ArrayList<Integer> numbers2) {
        boolean equalsTest = false;
        for(int i = 0; i<numbers.size(); i++) { //This gives exception if
//both arrays aren't the same size
            if(numbers.get(i) == numbers2.get(i)) {
                equalsTest = true;
            }
        }
        return equalsTest;
    }
}

Can anyone explain to me how I can check the second array against the first array without running into exception? Thanks

Mattia Righetti
  • 1,265
  • 1
  • 18
  • 31
Overclock
  • 73
  • 7

4 Answers4

0

I'd be surprised if {1, 2, 3} and {3, 2, 1} returned true using your current code. Since you're checking that every item in numbers2 is in numbers, iterate over numbers2 and check if each item is in numbers.

public static boolean isContained(ArrayList<Integer> numbers, 
        ArrayList<Integer> numbers2) {
    outer:
    for(int i = 0; i<numbers2.size(); i++) {
        for(int j = 0; j<numbers.size(); j++){
            if(numbers2.get(i) == numbers.get(j)){
                continue outer;
        return false;
    return true;
jpyams
  • 4,030
  • 9
  • 41
  • 66
0

You get the exception because you don't check which array is larger. You may add an if statement like this:

public static boolean isContained(ArrayList<Integer> numbers, 
ArrayList<Integer> numbers2) {
    boolean equalsTest = false;
    if(numbers.size() > numbers2.size()){
        for(int i = 0; i<numbers.size(); i++) {
            if(numbers.get(i) == numbers2.get(i)) {
                public static boolean isContained(ArrayList<Integer> numbers, ArrayList<Integer> numbers2) {
                    boolean equalsTest = false;
                    for(int i = 0; i<numbers.size(); i++) { 
                        if(numbers.get(i) == numbers2.get(i)) {
                        equalsTest = true;
                        }
                    }
                    return equalsTest;
                 }     
                 equalsTest = true;
             } 
        }
        return equalsTest;
    }
    else ;// Here add a logic of checking if the second list is larger
}
Evgeny Mamaev
  • 1,237
  • 1
  • 14
  • 31
0

As is often the case with asking for help, I find that the solution comes to you a few moments after posting the question.

At any rate, I managed to solve the problem with a nested for loop using int j to go through the second List. This is the code I used in my isContained method:

public static boolean isContained(ArrayList<Integer> numbers, 
ArrayList<Integer> numbers2) {
        boolean equalsTest = false;
        for(int i = 0; i<=numbers.size()-1; i++) {
            for(int j = 0; j<=numbers2.size()-1; j++) { //This second for loop goes through the elements of the second List separately without causing an exceptoion
                if(numbers.get(i) == numbers2.get(j)) {
                    equalsTest = true;
                    break;
                }
            }
        }
        return equalsTest;
}
Overclock
  • 73
  • 7
0

The problem with your code arise at line :

 if(numbers.get(i) == numbers2.get(i)) {

Consider that when the size are not equal you can not do this, cause when your first array contains 5 elemnts and the second array contain 2 elemants there is a case where you are trying to compare

 if(numbers.get(3) == numbers2.get(3)) {

which the numbers2 does not have that element, that's why you get the error, if you want to check an array contains another one the simple way is using

boolean Collection.containsAll(Collection<?> c);

but if you are trying to write your own code, make sure you are considering the following aspects, and update your code :

  • Consider the size of arrays, you should search for smaller array inside the large one !!
  • for each element of small size array, check if the large array contains it, if you find the element in large array go for the next one (next element of small array),
  • if an element of small array is not found in large array the result is false, so you can return false and ignore searching for others
  • if all elements of small array are founded in large array the answer is true,
Sir1
  • 732
  • 1
  • 8
  • 18