counting unique items in an ArrayList
This is a pretty simple issue, I think, and I did search the forum for how to do this. I found one piece of advice that got me a little closer to what I need, but I am still having hang-ups.
I have an ArrayList of integers and I need to count how many times each integer appears. In my code, I have already defined a sorted ArrayList (sorted from least value to greatest value, and it works fine), and my code for the counting loop looks like this.
int count = 1;
value = word_lengths.get(0);
for (int i = 0; i < word_lengths.size(); i++)
{
post_value = word_lengths.get(i+1);
if (value == post_value)
{
count++;
}
else
{
System.out.println(value +"-letter words: " + count);
count = 1;
value = word_lengths.get(i);
}
}
As per the (quoted) advice in another post, I am tyring to
1. take element 0, record the value, set the count as one
2. Next, loop through the array. If the new value is the same as the previous value, add 1 to the count. Otherwise, output the previous value and count, and set the new count to 1 and the new value to the new value.
3. At the end, print out the last value.
My output is kerflunky, though, and looks ike
1-letter words: 9
1-letter words: 1
2-letter words: 26
2-letter words: 1
3-letter words: 21
3-letter words: 1
4-letter words: 28
4-letter words: 1
5-letter words: 20
5-letter words: 1
6-letter words: 13
6-letter words: 1
7-letter words: 15
7-letter words: 1
8-letter words: 3
8-letter words: 1
9-letter words: 6
9-letter words: 1
10-letter words: 3
IndexOutOfBoundsException: Index: 154, Size: 154
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at LengthTallyTest.main(LengthTallyTest.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
Please help me to spot the error. I think I just have defined some parameters in the wrong places... thank you very much for all your help.
[2729 byte] By [
emmoorea] at [2007-10-2 17:25:01]

Use a debugger.First, look at the value that's getting placed into value in your else caseSecond, check out the line on which you're getting the ArrayIndexOutOfBounds and try to think about how that could be happening.
D'oh! I definately figured out the first problem but printing out the values. All I had to do was set value = post_value. Silly mistakes...
I know why the second error is coming up. The program doesn't know what to do with the last element because there is no other element following. I think I am just having a mental block as to how I account for that...
> I know why the second error is coming up. The program
> doesn't know what to do with the last element because
> there is no other element following. I think I am
> just having a mental block as to how I account for
> that...
You could stop the 'for' loop at word_lengths.size()-1, instead of word_lengths.size()
MLRona at 2007-7-13 18:41:21 >

I've been playing with that.
If I make it word_lengths.size() - 1, the program ignores the four instances of 15 in my ArrayList (in the list, there are four instances of 10, but then the program notes nothing else after).
If I define it like
for (int i = 0; i < word_lengths.size(); i++)
{
post_value = word_lengths.get(i+1);
//change is here
if (value == post_value && value != word_lengths.get(word_lengths.size() - 1))
{
count++;
}
else
{
System.out.println(value + "-letter words: " + count);
value = post_value;
count = 1;
}
//thinking I need another condition here, like what to do when it gets to the last one
}
My output is:
[same as before]
9-letter words: 7
10-letter words: 4
15-letter words: 1
15-letter words: 1
15-letter words: 1
IndexOutOfBoundsException: Index: 157, Size: 157
I think it's maybe closer, but I'm still hung up on how to deal with those last few cases. Thank you for the suggestions thus far, I think I'm getting there...
You are pretty close, and I just felt that I wanted to do some homework. You can take a look at this example (which aren't using generics and autoboxing)
List data = Arrays.asList(new Integer[] {
new Integer(1),
new Integer(1),
new Integer(1),
new Integer(2),
new Integer(4),
new Integer(4)
});
int count = 1;
int previousValue = ((Integer)data.get(0)).intValue();
for (int i=1; i<data.size(); i++) {
int currentValue = ((Integer)data.get(i)).intValue();
if (previousValue == currentValue) {
count++;
continue;
}
System.out.println("Value " + previousValue +
" exists " + count + " times");
count = 1;
previousValue = currentValue;
}
System.out.println("Value " + previousValue +
" exists " + count + " times");
Kaj>
kajbja at 2007-7-13 18:41:21 >

> I've been playing with that.
>
> If I make it word_lengths.size() - 1, the program
> ignores the four instances of 15 in my ArrayList (in
> the list, there are four instances of 10, but then
> the program notes nothing else after).
>
> If I define it like
>
>for (int i = 0; i < word_lengths.size(); i++)
>{
>post_value = word_lengths.get(i+1);
>
>
> My output is:
> [same as before]
> 9-letter words: 7
> 10-letter words: 4
> 15-letter words: 1
> 15-letter words: 1
> 15-letter words: 1
> IndexOutOfBoundsException: Index: 157, Size: 157
>
> I think it's maybe closer, but I'm still hung up on
> how to deal with those last few cases. Thank you for
> the suggestions thus far, I think I'm getting there...
What I meant was to change:
for (int i = 0; i < word_lengths.size(); i++)
to
for (int i = 0; i < word_lengths.size()-1; i++)
Then when you do the following line:
word_lengths.get(i+1);
You won't get an ArrayIndexOutOfBoundsException.
MLRona at 2007-7-13 18:41:21 >

You could do it the lazy way:
public int uniqueElements(List data) {
return (new java.util.HashSet(data)).size();
}
; )
> You could do it the lazy way:> public int uniqueElements(List data) {>return (new java.util.HashSet(data)).size();> }> ; )The objects in the data-list must have a proper equals(Object) and hashCode() method, of course.
> You could do it the lazy way:
> public int uniqueElements(List data) {
>return (new java.util.HashSet(data)).size();
> }
> ; )
How will that answer the question:
"I have an ArrayList of integers and I need to count how many times each integer appears. "
?
kajbja at 2007-7-13 18:41:21 >

> How will that answer the question:
>
> "I have an ArrayList of integers and I need to
> count how many times each integer appears. "
>
> ?
Ah, I must confess that I only read the subject of the post carefully, and skimmed the rest of the post.
@OP: sorry, ignore my posts!