//接口部分
 package collection;public interface Element
{
public int getCount();//返回数组存放元素的个数
public int gerSize();//返回数组的大小
public void add(int num);//添加方法;参数为要添加的数;添加位置默认在数组的最后一个位置
public void insert(int num,int location);//插入方法;参数分别为:插入的数,插入的位置;
public void del(int location);//删除方法;参数为删除的位置;删除点后的元素自动上移
public void sort();//排序方法;使用起泡算法
public void foreach();//使用foreach方法打印数组中的每个元素并求数组元素和
}
//类部分
package collection;import javax.swing.JOptionPane;public class Forever implements Element//类Forever实现接口Element{
private int box[];//声明数组
private  int last;//声明最后输入的数字

public int gerSize() //不懂的地方,自动改正的部分
{
return 0;
}

public int Forever(int count)//不懂的地方,自动改正的部分,有警告:此方法具有构造函数名
{
return 0;
}

public Forever()//使用构造函数  说明:返回的是类类型本身
{
box = new int[20];
last = 0;
} public Forever(int count) //初始化数组
{
box = new int[count];//分配给box存储空间
last = 0;
} public int getCount() // 数组所存放数据的个数
{
return last;//即数组的最后一个元素的下标
} public int getSize() // 数组的大小
{
return box.length;//使用数组本身的属性.length,取出数组的长度
} public void add(int number) //add添加方法,主要操纵的是last,将添加的元素放置在数组的最后一个位置
{
box[last] = number;//number为将要添加的数
last++;//++后的last的值就为最后一个元素的数组下标
} public void insert(int number, int location) //插入方法,number为将要插入的元素;location为插入的位置
{
if (location > box.length || location <= 0)//异常处理
{
JOptionPane.showMessageDialog(null, "Location is error !!");

else 
{
box[location] = number;//将元素放到要插入位置
//for(int i=location+1;i<=box.length;i++)//插入点后的元素下移,有错误
//{
// box[i+1]=box[i];
//}
}
} public void del( int location) //删除方法,number为将要删除的元素;location为删除的位置
{
if (location > box.length || location <= 0)//异常处理
{
JOptionPane.showMessageDialog(null, "Location is error !!");

else 
{
for(int  i=0;i<=location;i++)//删除location位置的元素,并将location以后的元素上移一个位置
{
if(i==location)
for(int j=i;j<=location-1;j++)
box[j+1]=box[1];
}
}
}

public int FindIf (int location) //有条件查找FindIf,location为条件即元素的位置
{
if (location > box.length || location <= 0)//异常处理 
{
JOptionPane.showMessageDialog(null, "Location is error !!");
return -1;

else
{
return box[location ];//返回location位置的元素
}
} public void sort() //排序,起泡法
{
int n = box.length;//n为数组的长度
for (int i = 0; i < n - 1; i++) //行号
{
for (int j = i + 1; j < n; j++) //元素位置
{
if (box[j] < box[i])
{
int temp = box[i];
box[i] = box[j];
box[j] = temp;
}
}
}
} public void foreach() //foreach方法的实现,功能为打印数组内的每个元素,并求和
{
int sum=0;
for(int x:box)//打印数组的每个元素,并求和
{
System.out.println("排序后的数为:"+x);
sum+=x;
}
System.out.println("以上数的和为:"+sum);
}

public static void main(String args[]) //实现
{
Forever a = new Forever(10); a.add(123);
a.add(435);
a.add(515);
a.add(154);
a.add(514);
a.add(2);
a.insert(42, 8);
a.del(3);
a.sort();
    System.out.println(a.getCount());//打印数组中元素的个数
a.foreach(); }

}就是在insert方法中的被注释掉的那个for循环,为什么编译的时候没有错误,可是一运行了就有错误,不能运行呢?偶新手,请各位细致解答,感激不尽!!!

解决方案 »

  1.   

    问题就出在楼主数组长度与下标关系。数组长度为数组最后元素下标+!,所以代码应该为下面这样:public void insert(int number, int location) //插入方法,number为将要插入的元素;location为插入的位置
    {
    if (location >= box.length || location <= 0)//异常处理
    {
    JOptionPane.showMessageDialog(null, "Location is error !!");

    else 
    {
    box[location] = number;//将元素放到要插入位置
    //for(int i=location+1;i<box.length;i++)//插入点后的元素下移,有错误
    //{
    // if ( i+1 < box.length)
      boxbox[i+1]=box[i];
    else
      JOptionPane.showMessageDialog(null, "Location is error !!"); //}
    }
    }
      

  2.   

    楼上的修改后的程序调试没有问题,不过运行时还是有location的错误消息提示,请问错误出在什么地方啊?