Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting a java.lang.IndexOutOfBoundsException using arraylists  RSS feed

 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a while look with a nested for and if loop while trying to count how many of each number this is in a different array. The output I am getting is

The number 1913 appears 1 times.
The number 2016 appears 2 times.
The number 32843 appears 2 times.
The number 31 appears 2 times.
The number 27 appears 2 times.
The number 1.4 appears 2 times.
The number 4 appears 2 times.
The number 7 appears 2 times.
The number 2 appears 2 times.
The number 23 appears 2 times.
The number 23 appears 2 times.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Lecture5Demo2.main(Lecture5Demo2.java:272)


this is the part of my code that I assume I am having issues with. The rest if my code is long using regular expression to extract the numbers from the two different files.



It is also giving me 2 23s and I'm not sure why below are the 2 arraylists

Whole list=[1913, 2016, 1, 1913, 186, 2016, 1711, 32843, 2016, 518, 518, 3, 1913, 32843, 32001, 4, 250, 5, 3500, 6, 7, 7, 8, 27, 73, 9, 10, 1711, 73, 11, 2, 1.4, 1.4, 1.4, 12, 33.75278, 84.38611, 1913, 19, 1930, 20, 21, 1947, 1947, 22, 1955, 23, 1961, 23, 1969, 27, 1995, 26, 27, 27, 1962, 28, 29, 30, 1970, 31, 31]
Whole list=[1913, 2016, 32843, 31, 27, 1.4, 4, 7, 2, 23, 23]
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
23 appears twice in your second list. What kind of ArrayList are you using? (like: ArrayList<Number>?).

Is 'a' incremented in the correct loop, you think?
 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:What kind of ArrayList are you using? (like: ArrayList<Number>?).

Is 'a' incremented in the correct loop, you think?


its an ArrayList <String>

Ive traced it through a few times I believe it is. I swapped them around and my output was completely off :/
 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a <string> type because I had to extract the numbers from two different text files and print them
 
Piet Souris
Rancher
Posts: 1979
67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.

Well, have a look at your 'i' loop. As soon as i becomes 11, a also becomes 11. How many elements does your second list have?
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Natasha Sherrod wrote:I'm using a <string> type because I had to extract the numbers from two different text files . . .
A String is still the wrong type to use. If you want numbers, use numbers. Remember that a Scanner can read a text file and convert the tokens to different sorts of number. And that System.out.println or similar can display every kind of number.
 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Okay.

. How many elements does your second list have?


The I loop has 57 elements and the a loop is supposed to have 10 but it has 11 because its doing the 2 23s.

I have it so that it takes the first element for the a loop and goes through the whole i loop counting the reoccurrence. Atleast that's what I'm trying to do and think i have it doing but its only counting 2 of everything and some have more than two. Its like its not running through the whole 57 numbers in the i loop
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't indeed, does it? Look at the point where you increment variable 'a'. Where you do it makes that a always equals i. Now, your first list is much longer than your second, so as soon as i becomes bigger  (actually: equal to) than the length of your second list, you get this IOOB exception. That's why I hinted that you should increment a at some other place. Can you think of such a better place?

I do not know where your double 23 is coming from.
 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Can you think of a better place?.


I'm not sure I have tried a ton of different things :/ I tried moving it out and putting it in a for loop instead which made the outcome way worse.

currently this is what I have but now its not counting anything they all say 0 times. I sorted the arrays thinking it would make it a bit easier. do I need to switch the placement of the arrays? I have literally tried everything lol (that I can think of)

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem here:
is with the indexes a and b.  They should always be smaller than the size, never equal.
 
Piet Souris
Rancher
Posts: 1979
67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Natasha,

sometimes a bug IS hard to track down. Your latest code makees things worse, I'm afraid, so let's get back to your original code in your opening post. I added a println to that code, so that you can see where it is going wrong:

(I saved myself some typing by making your lists a little shorter)



So, the error comes from the place where a gets incremented (inside the 'i' loop). So, let's move it out of that loop and see the change:



And finally, let's remove the necessity to increase a 'manually' alltogether, by using a for loop as well:



It all becomes more clear if you would use better names than 'a' and 'i', to see what is going on. For instance: 'indexOfNumbers1'. Also: are you satisfied with the way you use the variable 'count'?
 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Natasha,


It all becomes more clear if you would use better names than 'a' and 'i', to see what is going on. For instance: 'indexOfNumbers1'. Also: are you satisfied with the way you use the variable 'count'?


Do I need to convert the arraylist to an int arraylist or double because I have decimals?
I'm not sure why but even with this its still printing out like none of the numbers are in my list
I am still getting this output:


The number 1.4 appears 0times.
The number 1913 appears 0times.
The number 2 appears 0times.
The number 2016 appears 0times.
The number 23 appears 0times.
The number 23 appears 0times.
The number 27 appears 0times.
The number 31 appears 0times.
The number 32843 appears 0times.
The number 4 appears 0times.
The number 7 appears 0times.
 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
It all becomes more clear if you would use better names than 'a' and 'i', to see what is going on. For instance: 'indexOfNumbers1'. Also: are you satisfied with the way you use the variable 'count'?


Wait!!! I think I did it. I had to change to == to .equals()

just have to count and make sure the numbers are right. it is telling me they are all 3s. so instead of using while then for us for then for to increment without having to say it. still trying to find out exactly where it was messing up before
 
Piet Souris
Rancher
Posts: 1979
67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I hope it is clear now where this AIOOB exception came from.

In your original code, you used a List<String>. To work with that as well would mean I had to type 57 times a double quote around all these numbers. That's why I used two much shorter List<Integer>'s.
But because odf this, I could simply use 'if list1.get(a) == list2.get(i)' making use of Java's automatic boxing and unboxing.

If you use a list<String> then indeed  you must use 'equals'. So, that part depends on the type of your lists.

In your list, you use doubles and ints. That's why I initially asked for the type of your numbers, which were strings.

Have you found out how you got 23 twice in your list? Is it twice in your source? If that is a problem, you could use a Set instead of a List here, since Sets do not allow duplicate entries.
 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:

Have you found out how you got 23 twice in your list? Is it twice in your source? If that is a problem, you could use a Set instead of a List here, since Sets do not allow duplicate entries.


Yes, I think I pretty much understand most of it now. I'm still not sure where the two 23s are coming from I am going to have to go back and look at my code from earlier when I imported the files, sorted the numbers out and added them to list. Its probably in there. Thank you so much for your help.

What is a set? I'm going to google and try it out. maybe see if that will fix the problem with the 2 23s.
 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just finished checking the numbers still aren't matching up. :/ Its saying they are all listed 3 times I just went through and that's not correct so it looks like something is still wrong. do you think it would help if I used arrays instead of array lists?

 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Natasha Sherrod wrote:(...)
Yes, I think I pretty much understand most of it now. I'm still not sure where the two 23s are coming from I am going to have to go back and look at my code from earlier when I imported the files, sorted the numbers out and added them to list. Its probably in there. Thank you so much for your help.

You're welcome!

What is a set? I'm going to google and try it out. maybe see if that will fix the problem with the 2 23s.

I would leave it for now. No doubt you will learn about Sets soon enough!

Greetings,
Piet
 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so it looks like its taking the number of times the first number in the list appeared and not changing the count for the rest of the numbers
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you do not reset the 'count' variable. If I may yse your original code, corrected for a++ and resetting count (and using Strings as well), can you test to see if this is what you wanted?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!