java程序出错了,java.lang.ArrayIndexOutOfBoundsException: -1
要实现的功能就是将ArrayList表里面所有以给定开头的数据都列出来importjava.util.ArrayList;publicclasstest_project{...
要实现的功能就是将ArrayList表里面所有以给定开头的数据都列出来
import java.util.ArrayList;
public class test_project {
public static void main(String[] args) {
ArrayList<String> a=new ArrayList();
a.add("aabbc");
a.add("aabbcda");
a.add("aabbdg");
a.add("aabbfh");
a.add("aabbdsfh");
a.add("aabbsfdhg");
search s=new search();
int[] ed=new int[2];
ed=s.search(a, "aa");
}
}
public class compare {//这个类完全正确//
public int compare(String a,String b){
char[] a1=new char[a.length()+1];
a1=a.toLowerCase().toCharArray();
char[] b1=new char[b.length()+1];
b1=b.toLowerCase().toCharArray();
int num=Math.min(a.length(),b.length());
int c=0;
for(int i=0;i<=num-1;i++)
{if(a1[i]<b1[i])
{c=-1;break;}
else if(a1[i]>b1[i])
{c=1;break;}
else{continue;}}
return c;
}}
import java.util.ArrayList;
public class search {
int middle;
public int[] search(ArrayList<String> sorted,String key){
int low=0;
int size=sorted.size();
int high=size-1;
int[] aa=new int[2];
compare c=new compare();
while(low<=high)
{
if(low>high){System.out.println("error");break;}
else{int mid=(int)(low+high)/2;
if(c.compare(key,sorted.get(mid))==0){
this.middle=mid;
break;}
else if(c.compare(key,sorted.get(mid))==-1){
high=mid-1;}
else{low=mid+1;}
}}
for(int down=1;down<=size;)
{
if((c.compare(key,sorted.get(this.middle-down)))==0){
down=down+1;}
else{aa[0]=(middle-down+1);break;}
}
for(int up=1;up<=size;)
{
if(c.compare(key,sorted.get(this.middle+up))==0){
up=up+1;}
else{aa[1]=(middle+up-1);break;}
}
return aa;
}} 展开
import java.util.ArrayList;
public class test_project {
public static void main(String[] args) {
ArrayList<String> a=new ArrayList();
a.add("aabbc");
a.add("aabbcda");
a.add("aabbdg");
a.add("aabbfh");
a.add("aabbdsfh");
a.add("aabbsfdhg");
search s=new search();
int[] ed=new int[2];
ed=s.search(a, "aa");
}
}
public class compare {//这个类完全正确//
public int compare(String a,String b){
char[] a1=new char[a.length()+1];
a1=a.toLowerCase().toCharArray();
char[] b1=new char[b.length()+1];
b1=b.toLowerCase().toCharArray();
int num=Math.min(a.length(),b.length());
int c=0;
for(int i=0;i<=num-1;i++)
{if(a1[i]<b1[i])
{c=-1;break;}
else if(a1[i]>b1[i])
{c=1;break;}
else{continue;}}
return c;
}}
import java.util.ArrayList;
public class search {
int middle;
public int[] search(ArrayList<String> sorted,String key){
int low=0;
int size=sorted.size();
int high=size-1;
int[] aa=new int[2];
compare c=new compare();
while(low<=high)
{
if(low>high){System.out.println("error");break;}
else{int mid=(int)(low+high)/2;
if(c.compare(key,sorted.get(mid))==0){
this.middle=mid;
break;}
else if(c.compare(key,sorted.get(mid))==-1){
high=mid-1;}
else{low=mid+1;}
}}
for(int down=1;down<=size;)
{
if((c.compare(key,sorted.get(this.middle-down)))==0){
down=down+1;}
else{aa[0]=(middle-down+1);break;}
}
for(int up=1;up<=size;)
{
if(c.compare(key,sorted.get(this.middle+up))==0){
up=up+1;}
else{aa[1]=(middle+up-1);break;}
}
return aa;
}} 展开
2个回答
2015-01-22
展开全部
在for(int down=1;down<=size;) 这个地方,size为你List 的大小,而sorted.get(this.middle-down),这里的middle是size的一半,遍历的时候this.middle-down可能小于0,所以会出现数组越界,判断一下this.middle-down的大小就行;同理,你sorted.get(this.middle+up)这个地方也有可能大于sort的大小,先判断
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询