2017-10-16 4 views
-2

Je suis assez nouveau à Java et j'essaie d'écrire un algorithme qui renvoie la somme des indices de paires qui sont égales au total.IndexOutOfBoundsException lors de l'appel de la fonction récursive

Je reçois une erreur lorsque j'appuie sur ma fonction récursive à propos des limites. Les limites me semblent bien, je ne fais que passer dans l'arrayliste mise à jour, donc je ne sais pas d'où ça vient.

Erreur

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 
    at java.util.ArrayList.rangeCheck(ArrayList.java:653) 
    at java.util.ArrayList.get(ArrayList.java:429) 
    at com.example.helloworld.HelloWorld.getMatches(HelloWorld.java:36) 
    at com.example.helloworld.HelloWorld.getMatches(HelloWorld.java:41) 

algorithme

public class HelloWorld { 

ArrayList<Integer> matches = new ArrayList<>(); 
ArrayList<Integer> temp = new ArrayList<>(); 

public static void main(String[] args) { 
    ArrayList<Integer> param = new ArrayList<>(Arrays.asList(0, 0, 0, 0, 1, 1)); 

    int res = new HelloWorld().pairwise(param, 1); 
    System.out.println(res); 
} 

private int pairwise(ArrayList<Integer> arr, Integer total) { 
    for (Integer item: arr) { 
     this.temp.add(item); 
    } 
    getMatches(this.temp, total); 
    return getIndices(this.matches, arr); 
} 

private void getMatches(ArrayList<Integer> arr, Integer total) { 
    boolean noMatch = true; 

    for (int i=1; i<arr.size(); i++) { 
     if (arr.get(0) + arr.get(i) == total) { 
      //add to matches 
      this.matches.add(arr.get(0)); 
      this.matches.add(arr.get(i)); 

      //remove from temp 
      this.temp.remove(0); 
      this.temp.remove(arr.get(i)); 

      noMatch = false; 
      if (this.temp.size() > 1) { 
       //ERROR HERE 
       getMatches(this.temp, total); 
      } 

     } 
    } 
    if (noMatch) { 
     this.temp.remove(0); //remove first one 
     if (this.temp.size() > 1) { 
      getMatches(this.temp, total); 
     } 
    } 
} 

private int getIndices(ArrayList<Integer> matches, ArrayList<Integer> array) { 
    int count = 0; 
    for (Integer item: matches) { 
     int index = array.indexOf(item); 
     count += index; 
     array.set(index, -3000); 
    } 
    return count; 
} 
} 

Toute aide est très appréciée.

Répondre

0

Ajouter -1 dans la pour connaître l'état

private void getMatches(ArrayList<Integer> arr, Integer total) { 
boolean noMatch = true; 

for (int i=1; i<arr.size()-1; i++) { 
    if (arr.get(0) + arr.get(i) == total) { 
     //add to matches 
     this.matches.add(arr.get(0)); 
     this.matches.add(arr.get(i)); 

     //remove from temp 
     this.temp.remove(0); 
     this.temp.remove(arr.get(i)); 

     noMatch = false; 
     if (this.temp.size() > 1) { 
      //ERROR HERE 
      getMatches(this.temp, total); 
     } 

    } 
} 
if (noMatch) { 
    this.temp.remove(0); //remove first one 
    if (this.temp.size() > 1) { 
     getMatches(this.temp, total); 
    } 
} 

}

3

urm ... vous supprimez les éléments du tableau nous parcourons plus ...

ici:

for (int i=1; i<arr.size(); i++) { 
    //...code 
} 

Vous parcourez la taille initiale du tableau qui est 6. Puis à l'intérieur du corps de la boucle pour vous êtes suppression éléments du tableau que nous parcourons:

//remove from temp 
this.temp.remove(0); 
this.temp.remove(arr.get(i)); 

Chaque itération rend le tableau plus court qui est la raison pour laquelle vous obtenez des exceptions Out liées.

Vous voulez dupliquer le tableau que vous passez à getMatches-à-dire

getMatches(new ArrayList<>(this.temp), total); 

De cette façon, vous serez en retirer des éléments de temp mais n'affecterez le tableau que vous êtes en train de itérer sur: arr

+0

Cest il! J'ai même réalisé cela avant et je regardais les «itérateurs de liste» dans une approche différente. Merci, le temps d'une pause ... – andrewgi

1

Vous devez supprimer l'élément à i puis retirez 0:

 //remove from temp 
     this.temp.remove(arr.get(i)); 
     this.temp.remove(0);