• Post Reply Bookmark Topic Watch Topic
  • New Topic

same elements instring array or not  RSS feed

 
Nidheesh Krishna
Ranch Hand
Posts: 62
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
i want to check if a String array containd same elements or not.
This is my code,

But i am getting the following error



Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Testing.check(Testing.java:29)
at Testing.main(Testing.java:22)
Java Result: 1
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an array


Array index starts at 0
In this array the maximim index is 3 (and not 4)
In your code you use
which will correctly be restricted to 3
However in the next line you increment it by one with which takes it to str[4] when i is 3
Thus the exception
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!