请大家看下面的代码,在运行时老是出现
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at OrderArray.insert(OrderedApp.java:43)
at OrderedApp.main(OrderedApp.java:78)
但是我看不出插入模块有什么问题,请大家指点,谢谢!
代码如下:class OrderArray{
private long[] a;
private int nElem;
public OrderArray(int max){
a = new long[max];
nElem = max;
}
public int size(){
return nElem;
}
public int find(long SearchKey){
int LowerBound = 0;
int UpperBound = nElem - 1;
int CurIn;
CurIn = (LowerBound + UpperBound) / 2;
while(true){
if(a[CurIn] == SearchKey){
return CurIn;
}
else if(LowerBound > UpperBound){
return nElem;
}
else{
if(a[CurIn] < SearchKey){
UpperBound = CurIn - 1;
}
else{
LowerBound = CurIn + 1;
}
}
}
}
public void insert(long value){
int j;
for(j = 0;j < nElem;j ++){
if(a[j] > value){
break;
}
}
for(int k = nElem;k > j;k --){
a[k] = a[k - 1];
}
a[j] = value;
nElem ++;
}
public boolean delete(long value){
int j;
for(j = 0;j < nElem;j ++){
if(a[j] == value){
break;
}
}
if(j == nElem){
return false;
}
else{
for(int k = j;k < nElem;k ++){
a[k] = a[k + 1];
}
nElem --;
return true;
}
}
public void display(){
for(int j = 0;j < nElem;j ++){
System.out.print(a[j] + " ");
}
System.out.println("");
}
}
public class OrderedApp { public static void main(String[] args) {
OrderArray arr;
arr = new OrderArray(100);
arr.insert(22);
arr.insert(88);
arr.insert(55);
arr.insert(77);
arr.insert(11);
arr.insert(44);
arr.insert(00);
arr.insert(66);
arr.insert(99);
arr.insert(33);
arr.display();
long SearchKey = 11;
if(arr.find(SearchKey) == arr.size()){
System.out.println("Can't find " + SearchKey);
}
else{
System.out.println("Found " + SearchKey);
}
arr.delete(44);
arr.delete(00);
arr.display();
}}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at OrderArray.insert(OrderedApp.java:43)
at OrderedApp.main(OrderedApp.java:78)
但是我看不出插入模块有什么问题,请大家指点,谢谢!
代码如下:class OrderArray{
private long[] a;
private int nElem;
public OrderArray(int max){
a = new long[max];
nElem = max;
}
public int size(){
return nElem;
}
public int find(long SearchKey){
int LowerBound = 0;
int UpperBound = nElem - 1;
int CurIn;
CurIn = (LowerBound + UpperBound) / 2;
while(true){
if(a[CurIn] == SearchKey){
return CurIn;
}
else if(LowerBound > UpperBound){
return nElem;
}
else{
if(a[CurIn] < SearchKey){
UpperBound = CurIn - 1;
}
else{
LowerBound = CurIn + 1;
}
}
}
}
public void insert(long value){
int j;
for(j = 0;j < nElem;j ++){
if(a[j] > value){
break;
}
}
for(int k = nElem;k > j;k --){
a[k] = a[k - 1];
}
a[j] = value;
nElem ++;
}
public boolean delete(long value){
int j;
for(j = 0;j < nElem;j ++){
if(a[j] == value){
break;
}
}
if(j == nElem){
return false;
}
else{
for(int k = j;k < nElem;k ++){
a[k] = a[k + 1];
}
nElem --;
return true;
}
}
public void display(){
for(int j = 0;j < nElem;j ++){
System.out.print(a[j] + " ");
}
System.out.println("");
}
}
public class OrderedApp { public static void main(String[] args) {
OrderArray arr;
arr = new OrderArray(100);
arr.insert(22);
arr.insert(88);
arr.insert(55);
arr.insert(77);
arr.insert(11);
arr.insert(44);
arr.insert(00);
arr.insert(66);
arr.insert(99);
arr.insert(33);
arr.display();
long SearchKey = 11;
if(arr.find(SearchKey) == arr.size()){
System.out.println("Can't find " + SearchKey);
}
else{
System.out.println("Found " + SearchKey);
}
arr.delete(44);
arr.delete(00);
arr.display();
}}
public OrderArray(int max){
a = new long[max];
nElem = max;//应该是nElem = 0;
}
关键是这行的代码,你贴出来,大家就容易帮你查出问题
int j;
for(j = 0;j < nElem;j ++){
if(a[j] > value){
break;
}
}
for(int k = nElem;k > j;k --){
a[k] = a[k - 1];
}
a[j] = value;
nElem ++;
}
这里越界了。100 + 1 大于你初始化的长度了
if(a[CurIn] < SearchKey){ //应该是>
UpperBound = CurIn - 1;
}
else{
LowerBound = CurIn + 1;
}
逻辑上的问题。看这段代码:
public void insert(long value) {
int j;
for (j = 0; j < nElem; j++) {
if (a[j] > value) {
break;
}
}
for (int k = nElem; k > j; k--) {
a[k] = a[k - 1];
}
a[j] = value;
nElem++;
}首先,a[]
作为长整形类成员变量,在你初始化之后,默认使用0填充。
分析一下你的第一次插入操作。以j作为循环控制变量,自然想要在找到一个大于你插入的22之后break出循环是不可能的,这样循环直道满足结束条件也就是 就是j < nElem;你的nElem传递的100,循环退出时j的值也是100。
那么下面你那个以k做控制的循环也就根本不会进入。
最要命的问题是你使用a[j],前面说了j这个时候等于100啊。作为100个长度的数组,下标的范围是0-99,你这里用了一个a[100]自然就出现了下标越界的问题。唉……你没有看出什么问题,可是,这段代码却是问题重重。当然,解决起来没那么费劲了,lz更换一下思路。
呵呵,考虑控制,考虑边界情形。
private long[] a;
private int nElem;
public OrderArray(int max){
a = new long[max];
nElem = 0;
}
public int size(){
return nElem;
}
public int find(long SearchKey){
int LowerBound = 0;
int UpperBound = nElem - 1;
int CurIn;
while(true){
CurIn = (LowerBound + UpperBound) / 2;
if(a[CurIn] == SearchKey){
return CurIn;
}
else if(LowerBound > UpperBound){
return nElem;
}
else{
if(a[CurIn] > SearchKey){
UpperBound = CurIn - 1;
}
else{
LowerBound = CurIn + 1;
}
}
}
}
public void insert(long value){
int j;
if(nElem == 0){
a[0] = value;
nElem ++;
}
//nElem == max情况暂不予考虑
else{
for(j = 0;j < nElem;j ++){
if(a[j] > value){
break;
}
}
for(int k = nElem;k > j;k --){
a[k] = a[k - 1];
}
a[j] = value;
nElem ++;
}
}
public boolean delete(long value){
int j;
for(j = 0;j < nElem;j ++){
if(a[j] == value){
break;
}
}
if(j == nElem){
return false;
}
else{
for(int k = j;k < nElem;k ++){
a[k] = a[k + 1];
}
nElem --;
return true;
}
}
public void display(){
for(int j = 0;j < nElem;j ++){
System.out.print(a[j] + " ");
}
System.out.println("");
}
}
public class OrderedApp { public static void main(String[] args) {
OrderArray arr;
arr = new OrderArray(100);
arr.insert(22);
arr.insert(88);
arr.insert(55);
arr.insert(77);
arr.insert(11);
arr.insert(44);
arr.insert(00);
arr.insert(66);
arr.insert(99);
arr.insert(33);
arr.display();
long SearchKey = 11;
if(arr.find(SearchKey) == arr.size()){
System.out.println("Can't find " + SearchKey);
}
else{
System.out.println("Found " + SearchKey);
}
arr.delete(44);
arr.delete(00);
arr.display();
}}