``````class Temp22
{
static int []years={-80000,-79950,20,70,22,60,58,60,58,65,1950,2005};
public static int[] findMaxdinosaur(int[] years)
{
int[] count = new int[(years.length)/2];
int i=0;
int j=0;
int k=0;
for(i=0;i<years.length;i=i+2)
{
int m=0;
{
for(j=0;j<years.length;j=j+2)
{
if(years[i]>years[j+2])
{
m=m+1;
}
}
}
{
count[k]=m;
k=k+1;
}
}
return count;
}
public static void main(String...s)
{
int[] count=findMaxdinosaur(years);
for(int l=0;l<=783;l=l+1)
{
System.out.println(count[l]);
}
}
}
``````

this code is showing exception "ArrayoutofBound", can anybudy tell me, why??

4
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by hinterface

count size is 5? did you mean 5?

``````int[] count = new int[(years.length)/2];
``````

yet you parse the array till the number 783, which is way greater than years.length/2.

``````for(int l=0;l<=783;l=l+1)
``````

Why don't you go till the end of the actual array?

``````for(int l=0;l<count.length;l++)
``````

Edited by Lucaci Andrew

for further reference: could you poste more usefull information?
yes, you get the notice of an arrayOutOfBoundsException. but you also are told on which line of your code that occurs (just an example)
showing us the relevant code 'll do, you don't always need to show the entire code

@stultuske: sorry, actually, I am new here, so don't know how to use it. I will make these points in mind next time. Thanks

@all: the problem is solved now, and it was really simple, Thanks to all of you, for your co-operation.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.