Java 方法在数组列表中插入/追加元素

Java 方法在数组列表中插入/追加元素,java,insert,arraylist,append,Java,Insert,Arraylist,Append,我不确定我是否正确实现了插入或追加,但我遇到了以下错误: 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常: -在AListInt.insert(AListInt.java:81)//listArray[i+1]=listArray[i];在ListTest.main(ListTest.java:52)//list.insert(i) 此外,我不能使用java.util.ArrayList 下面是它的代码和类: 类别: public cla

我不确定我是否正确实现了插入或追加,但我遇到了以下错误:

线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常: -在AListInt.insert(AListInt.java:81)//listArray[i+1]=listArray[i];在ListTest.main(ListTest.java:52)//list.insert(i)

此外,我不能使用java.util.ArrayList 下面是它的代码和类:

类别:

public class AListInt {


int [] listArray;
int listSize;
int curr; // current position

AListInt() {
    listSize = 0;

    // note that curr = -1 when listSize = 0
    curr = -1;
    listArray = new int [2];
}

public int getValue () throws DSException {
    return listArray[curr];
      //returns the value of current position
      //throw exception when there are no elements in the list
}

public int length() {
    return listSize;
    //return # of elements in the list
}


public int currPos() {
    return curr;
    //return current position.
}

public void moveToPos ( int pos ) throws DSException {
    curr = pos;
      //move the current position to pos
      //throw exception if pos is not a valid position
}

public void moveToStart () throws DSException {
    curr = 0;
      //move the current position to the start of the list
      //throw exception if no elements are in the list
}


public void moveToEnd () throws DSException {
    curr = listSize;
      //move the current position to the end of the list
      //throw exception if no elements are in the list
}


public void prev () throws DSException {
    if(curr != 0)
    {
        curr--;
    }
      //move current position to the previous element
      //throws exception if the previous position is not legal or
      //    if there are no elements in the list
}


public void next () throws DSException {
    if(curr < listSize)
    {
        curr++;
    }
      //move current position to the next element
      //throws exception if the next position is not legal or
      //    if there are no elements in the list
}


public void insert ( int item ) {

    for(int i = listSize-1; i >= curr; i++)
    {
        listArray[i+1] = listArray[i];

    }
    listArray[curr] = item;
    listSize ++;

    int[]temp = new int[listArray.length*2];
    for(int i = 0; i< listSize; i++)
    {
        temp[i] = listArray[i];
    }
    listArray = temp;

    // inserts item to the current position
    // if not enough memory, double the size of listArray
}


public void append ( int item ) {
    listArray[listSize++] = item;

    int[]temp = new int[listArray.length*2];
    for(int i = 0; i< listSize; i++)
    {
        temp[i] = listArray[i];
        listArray = temp;
    }
    // inserts item to the end of the list
    // if not enough memory, double the size of listArray       
}

public int remove () throws DSException {
     if((curr < 0)||(curr > listSize))
     {
            return -1;
     }

 int item;
 item = listArray[curr];

    for(int i = curr; i < listSize - 1; i++)
    {
        listArray[i] = listArray[i+1];
    }
    listSize --;
    return item;
      //removes the element at the current position
      //returns the removed element

}

public void clear() {
    listSize = 0;
    curr = -1;
    //reset size.  Set current position to be -1
}

public boolean find ( int val ) {
    for(int i = 0; i > listSize; i ++)
    {
        if(listArray[i] == val)
        {
            return true;
        }
    }
    return false;
  //searches for val in the list
    //returns true if found and false if not found
}

public void print () {
    System.out.print("<");
    for(int i = 0; i < listSize; i++)
    {
        System.out.print(listArray[i]);

        if(listSize == -1)
        {
            System.out.print("-1");
        }
    }
    System.out.print(">");
    //outprint the list
}
主要内容:

公共类列表测试{
公共静态void main(字符串[]args){
试一试{
AListInt list=新的AListInt();
list.print();
//测试长度()
System.out.println(list.length());
//测试电流pos()
System.out.println(list.currPos());
//插入一些数字
对于(int i=0;i<4;i++){
列表。附加(i);
list.print();
}
移动拓扑(0);
list.print();
list.moveToEnd();
list.print();
//测试getValue()
System.out.println(list.getValue());
System.out.println(“删除:+list.remove());
list.print();
list.moveToStart();
list.print();
System.out.println(“删除:+list.remove());
list.print();
list.clear();
list.print();
list.clear();
list.print();
System.out.println(“find 0:+list.find(0));
对于(int i=0;i<4;i++){
清单.插入(i);
list.print();
}
对于(int i=0;i<5;i++){
System.out.println(“find”+i+:“+list.find(i));
list.print();
}
list.next();
list.print();
列表.插入(-9);
list.print();
列表1.2(-2);
list.print();
list.moveToEnd();
列表.插入(-1);
list.print();
System.out.println(“删除:+list.remove());
list.print();
}捕获(DSE例外){
e、 printStackTrace();
}
}

}

您正在读取阵列外部的数据。在

for(int i = listSize-1; i >= curr; i++)
{
    listArray[i+1] = listArray[i];

}
如果
i=listSize-1
,那么
listary[i+1]
就是
listary[listSize]
,这是不受限制的,因为数组从
0
length-1

编辑:


但是由于
listary
的初始大小为2,并且每次插入时的大小都是原来的两倍,因此您可以不必担心。但是,在第一次插入时,
curr
-1
,由于终止是
i>=curr
,将进入循环,您将读取
listArray[-1]
(越界)

它必须是listArray[i]=listArray[i-1]

因为您正在将listArray[i-1]的位置移动到listArray[i]的位置

public class ListTest {

public static void main ( String[] args ) {

try {
    AListInt list = new AListInt();

    list.print();


    // test length()
    System.out.println ( list.length() );


    // test currPos()
    System.out.println ( list.currPos() );

    // insert some numbers
    for ( int i = 0; i < 4; i++ ) {
    list.append(i);
    list.print();
    }

    list.moveToPos(0);
    list.print();

    list.moveToEnd();
    list.print();


    // test getValue()                                                                                                                                                     
    System.out.println ( list.getValue() );

    System.out.println ( "remove: " + list.remove() );
    list.print();

    list.moveToStart();
    list.print();

    System.out.println ( "remove: " + list.remove() );
    list.print();

    list.clear();
    list.print();

    list.clear();
    list.print();

    System.out.println ( "find 0 : " + list.find ( 0 ) );

    for ( int i = 0; i < 4; i++ ) {
    list.insert(i);
    list.print();
    }

    for ( int i = 0; i < 5; i++ ) {
    System.out.println ( "find " + i + " : " + list.find ( i ) );
    list.print();
    }

    list.next();
    list.print();

    list.insert ( -9 );
    list.print();

    list.append ( -2 );
    list.print();

    list.moveToEnd();
    list.insert ( -1 );
    list.print();


    System.out.println ( "remove: " + list.remove() );
    list.print();

} catch ( DSException e ) {
    e.printStackTrace();
}

}
for(int i = listSize-1; i >= curr; i++)
{
    listArray[i+1] = listArray[i];

}