0

Given java code:

private List<String> list_from = new ArrayList<String>();
private List<String> list_dest = new ArrayList<String>();

values of list_from is:

[v5, v3, v5, v1, v1, v6, v6, v3, v4, v5, v4, v6]

values of list_dest is:

[v3, v5, v1, v5, v6, v1, v3, v6, v5, v4, v6, v4]

I'm using loop for to print out the values in list_from with an if statement:

for(int i=0;i < list_from.size();i++){                    
    if(list_dest.get(i+1) == list_from.get(i)){
        System.out.println(list_from.get(i));
    }
}

it gives out a correct result but with unknown error at an if statement.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 18, Size: 18

Thank you so much for your help!

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Ender
  • 835
  • 1
  • 12
  • 23
  • Can you provide content of this error? – Michał Piątkowski May 12 '17 at 18:49
  • 5
    *with unknown error at an if statement.* It's never an unknown error... Post the stack trace – Frakcool May 12 '17 at 18:49
  • `i` will go until `list_from.size()`, so `list_dest.get(i+1)` looks suspicious. For example, if the two lists have the same size, then you'll get an index out of bounds exception – janos May 12 '17 at 18:50
  • IndexOutOfBoundsException? – Suresh Atta May 12 '17 at 18:50
  • 3
    _it gives out a correct result_, surely this cannot give correct output --> `if(list_dest.get(i+1) == list_from.get(i))` , you're comparing Strings with `==`. – Ousmane D. May 12 '17 at 18:50
  • @MichałPiątkowski edited – Ender May 12 '17 at 18:51
  • 1
    Please don't use the "back" button of your browser to edit your question. It breaks the edits made by others. Instead use the [edit] link below your question or the one I provided here – Frakcool May 12 '17 at 18:52
  • @Frakcool so sorry. I'm just a newbie. I will mention about this next time – Ender May 12 '17 at 18:55
  • 1
    Please don't edit the solution inside your question. [Accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) one of the ones given below instead. Or answer your own question with the solution you got (But if it's going to be equal to one or more like the ones below, then just accept it) – Frakcool May 12 '17 at 18:56
  • 1
    I know you're new to the site, so I recommend you to take the [tour], and learn [ask], this will improve your questions greatly and won't get downvoted. Next time do a [Google search](https://www.google.com.mx/search?q=arrayindexoutofboundsexception+in+java&ie=utf-8&oe=utf-8&client=firefox-b&gfe_rd=cr&ei=5QUWWb7oMe_P8AfAqoeYCA) before asking too, – Frakcool May 12 '17 at 18:59

3 Answers3

3

list_dest.get(i+1), here's the problem. In the last iteration, value of i is the maximum index that is possible. When you add 1 to it it will give arrayOutOfBoundsException.

Also better to use string.equals() method to compare string.

3

Comparing strings should be done by string.equals(string2).
Strings are objects. You should always use equals when comparing objects. Also you are comparing too many indexes in array.

Your code should look like that:

for(int i=0;i < list_from.size()-1;i++){                    
    if(list_dest.get(i).equals(list_from.get(i+1))){
        System.out.println(list_from.get(i));
    }
}
2

Multiple Problems. First, change the loop condition to <list_from.size()-1.

Second, compare Strings with .equals(), and not ==.

Code becomes

for(int i=0;i < list_from.size()-1;i++){                    
    if(list_dest.get(i+1).equals(list_from.get(i))){
        System.out.println(list_from.get(i));
    }
}
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
  • I got it. Thank you so much! coz the code has so many stuff I have to handle. Didn't mention about it. Got it working now :) – Ender May 12 '17 at 18:53