The Artima Developer Community
Sponsored Link

Java Answers Forum
Array Exception

1 reply on 1 page. Most recent reply: Jul 16, 2005 11:53 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Marie Thibault

Posts: 1
Nickname: neil007
Registered: Jul, 2005

Array Exception Posted: Jul 16, 2005 11:00 AM
Reply to this message Reply
Advertisement
Hi,
I'm sure this will be an easy one for lots of folks out there, but I'm stumped. My code compiles but I get an "Arrayoutofbounds exception and I can't spot where my mistake lies. Any suggestions? It's a binary search code. I suspect the problem lies in the sort.

public class Binary{
//** declarations of variables
int[] list,list_search;
int size,count;
long time1;
long time2;

//** constructor
public Binary(int n, int k){
size=n;
count=k;
int i;
list=new int[size];
list_search=new int[count];
time1 = 0;
time2 = 0;

//** generate the list of elements
for(i=0;i<size;i++){
list=1+(int)(n*Math.random());
}
for(i=0;i<count;i++)
list_search=1+(int)(n*Math.random());
//sort the list
sort();
//search the element
startsearch();
}

//** sort the list before binary search... done in ascending order
public void sort(){
int i, j, increment, temp;

increment = 3;
while (increment > 0)
{
for (i=0; i < size; i++)
{
j = i;
temp = list;
while ((j >= increment) && (list[j-increment] > temp))
{
list[j] = list[j - increment];
j = j - increment;
}
list[j] = temp;
}
if (increment/2 != 0)
increment = increment/2;
else if (increment == 1)
increment = 0;
else
increment = 1;
}
}

//** method for binary search
public void binary(int key){
int low=0;
int high=size-1;
int middle;

while(low<high){
middle=(low+high)/2;
if(key==list[middle])
break;
else if(key<list[middle])
high=middle-1;
else if(key>list[middle])
low=middle+1;

}

}

public void startsearch(){
int i;
time1 = System.currentTimeMillis();
for(i=0;i<count;i++){
binary(list_search);
time2 = System.currentTimeMillis();
}

System.out.println("Search Time: "+ (time2 - time1) +" ms");

}

//** main function
public static void main(String args[]){
int n,k;
if(args.length!=2){
System.out.println("Required number of arguments are not provided");
}
else{
n=Integer.parseInt(args[0]);//parsing the size for number of elements in the list
k=Integer.parseInt(args[1]);// parsing the size of number of elements to be searched
Binary ss =new Binary(n,k);//object created to compare the two search

}
}
}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Array Exception Posted: Jul 16, 2005 11:53 AM
Reply to this message Reply
The runtime environment should tell you exactly in wich line your error lies.

Try printing out every index variable used in this line.


If you then still can't find it, could you please repost your code using the java flag? (like shown on the right of the input text area)?

All the "[ i ]"'s are missing

It would also help to read the code, too.

Flat View: This topic has 1 reply on 1 page
Topic: Regarding Sun Certification Please Help Previous Topic   Next Topic Topic: How to display text in different colors in JTextField of Swings

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use