Array Index Out of bounds?
Code :
]public static int[] encode(int array[]) throws FileNotCompressedException
{
int[] count=new int[array.length];
int[] Tarray=new int[array.length];
int j=0;
int c=0;
int NewArray[]=new int[array.length];
System.out.println(array[2]);
for (int i=0; i<array.length; i++)
{
if(array[i]==array[i+1])
{
if(count[j]==0)
{
count[j]++;
}
count[j]++;
}
else
{
Tarray[c]=array[i];
c++;
j++;
if(count[j]==0)
{
count[j]++;
}
}
}
if(count.length+Tarray.length>=array.length)
{
throw new FileNotCompressedException();
}
else
{ int a=0;
int k=0;
while(a<NewArray.length)
{
NewArray[a]=count[k];
NewArray[a+1]=Tarray[k];
a=a+2;
k++;
}
return NewArray;
}
}
This is a small part of my code but to keep it short, i read from a file that has 1000's of int then i need to sort them in a special way but when i use my for loop to begin my sort i get that error on array[i]=array[i+1]; that says array index out of bounds but when i print array[1000] or any other it works fine;
Re: Array Index Out of bounds?
Let's say that your array has 1000 cells which would mean it has indices 0-999. Your for loop would use i from 0-999, the problem is when you add 1 you would get 1000 which is not an index in the array hence the index out of bounds.