Just a simple problem here (which has got me stumped), I have constructed a program that scans an array for repeated numbers and then prints the quantity of each repeated number, but I can't seem to get it to print out the last array index (number 17 which would print the quantity as being 1). I am sure the problem is somewhere in my "for" loop. eg. for(int i=0,j=1,j<list.length,i++,j++)
but it prints everything except the last array index, I thought if maybe I went "j < list.length+1" but then it returns an arrayoutofbounds exception runtime error. I can't seem to find the in between of going out of bounds and stopping one short. I'm sure it must be a simple fix.
CODE
class Frequency
{
public static void main(String[] args)
{
int list[] = {2, 2, 2, 3, 4, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 9, 17}; // construct array
int count = 0; // counts individual elements
int[] thisCount; // counts each group of same numbers
for(int i = 0, j = 1; j < list.length; i++, j++) // scan the array
{
if(list[i] == list[j]) // if these two are the same
{
count++; // increment element count
}
else // otherwise
{
count++; // increment element count
thisCount = new int[count]; // and put the group count in this array
System.out.println(thisCount.length); // then print it
count = 0; // and reset to zero for next iteration
}
}
}
}
This post has been edited by stew_downunder: 23 May, 2007 - 03:20 AM