java - method to insert/append elements in array list -
i not sure if implementing insert or append correctly error:
exception in thread "main" java.lang.arrayindexoutofboundsexception: -1 @ alistint.insert(alistint.java:81) // listarray[i+1] = listarray[i]; @ listtest.main(listtest.java:52) // list.insert(i);
also cannot utilize java.util.arraylist here code , classes it:
class:
public class alistint { int [] listarray; int listsize; int curr; // current position alistint() { listsize = 0; // note curr = -1 when listsize = 0 curr = -1; listarray = new int [2]; } public int getvalue () throws dsexception { homecoming listarray[curr]; //returns value of current position //throw exception when there no elements in list } public int length() { homecoming listsize; //return # of elements in list } public int currpos() { homecoming curr; //return current position. } public void movetopos ( int pos ) throws dsexception { curr = pos; //move current position pos //throw exception if pos not valid position } public void movetostart () throws dsexception { curr = 0; //move current position start of list //throw exception if no elements in list } public void movetoend () throws dsexception { curr = listsize; //move current position end of list //throw exception if no elements in list } public void prev () throws dsexception { if(curr != 0) { curr--; } //move current position previous element //throws exception if previous position not legal or // if there no elements in list } public void next () throws dsexception { if(curr < listsize) { curr++; } //move current position next element //throws exception if next position not legal or // if there no elements in list } public void insert ( int item ) { for(int = listsize-1; >= curr; i++) { listarray[i+1] = listarray[i]; } listarray[curr] = item; listsize ++; int[]temp = new int[listarray.length*2]; for(int = 0; i< listsize; i++) { temp[i] = listarray[i]; } listarray = temp; // inserts item current position // if not plenty memory, double size of listarray } public void append ( int item ) { listarray[listsize++] = item; int[]temp = new int[listarray.length*2]; for(int = 0; i< listsize; i++) { temp[i] = listarray[i]; listarray = temp; } // inserts item end of list // if not plenty memory, double size of listarray } public int remove () throws dsexception { if((curr < 0)||(curr > listsize)) { homecoming -1; } int item; item = listarray[curr]; for(int = curr; < listsize - 1; i++) { listarray[i] = listarray[i+1]; } listsize --; homecoming item; //removes element @ current position //returns removed element } public void clear() { listsize = 0; curr = -1; //reset size. set current position -1 } public boolean find ( int val ) { for(int = 0; > listsize; ++) { if(listarray[i] == val) { homecoming true; } } homecoming false; //searches val in list //returns true if found , false if not found } public void print () { system.out.print("<"); for(int = 0; < listsize; i++) { system.out.print(listarray[i]); if(listsize == -1) { system.out.print("-1"); } } system.out.print(">"); //outprint list }
}
exception:
public class dsexception extends exception { public dsexception() { } public dsexception(string msg) { super(msg); } }
main:
public class listtest { public static void main ( string[] args ) { seek { alistint list = new alistint(); list.print(); // test length() system.out.println ( list.length() ); // test currpos() system.out.println ( list.currpos() ); // insert numbers ( int = 0; < 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 ) ); ( int = 0; < 4; i++ ) { list.insert(i); list.print(); } ( int = 0; < 5; i++ ) { system.out.println ( "find " + + " : " + list.find ( ) ); 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(); } grab ( dsexception e ) { e.printstacktrace(); } }
}
you reading outside array. in
for(int = listsize-1; >= curr; i++) { listarray[i+1] = listarray[i]; }
if i = listsize -1
, listarray[i+1]
listarray[listsize]
, out of bounds, since arrays go 0
length -1
edit:
but since listarray
has initial size of 2, , double size @ each insert away that. however, @ first insert curr
-1
, , since termination i >= curr
, loop entered , read listarray[-1]
(out of bounds)
java insert arraylist append
No comments:
Post a Comment