-2

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.

public static List<Integer> matchingStrings(List<String> strings, List<String> queries) {
       List<Integer> s = new ArrayList<Integer>(queries.size());
       for(int i = 0;i<queries.size();i++){
           s.set(i,0);
       }
       for(int i = 0;i < queries.size();i++){
           for(int j = 0; j<strings.size();j++){
               if((strings.get(j)).equals(queries.get(i))){
                   s.set(i,(s.get(i)+1));
               }
           }
       }
       return s;
   }

//Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:359) at java.base/java.util.ArrayList.set(ArrayList.java:441) at Result.matchingStrings(Solution.java:27) at Solution.main(Solution.java:68)

gagang
  • 7

1 Answers1

-1

Lets take an example :

List<String> arr = new ArrayList<String>();
        System.out.println(arr.get(0));

This gives, Index 0 out of bounds for length 0.

This means, your arraylist is empty & you are trying to access element of it.

Same happened with your case, check if any of your arraylist is empty & you are trying to access element by use of get in second for loop.

Ashish Patil
  • 4,428
  • 1
  • 15
  • 36