//insert value at specific index and shift everything from that index to the right
public void insertAt(int index, int value)
{
if ((index<0)||(index>size))
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
else
{
int theLength = storage.length;
int temp=0;
int j=theLength;
ensureCapacity(size+1);
for(int i = (theLength-1);i != index;i--)
{
temp = storage[i];
storage[j] = temp;
j--;
}
storage[index] = value;
}
}
I was thinking if I get the size of the array, and look for the length, then I get length -1 to have the last index, and parse it thru the parameter.
//add value to the end of array
public void add(int value)
{
int endPosition = size;
insertAt(endPosition, value);
}
This post has been edited by RozenKristal: 14 October 2012 - 12:15 AM

New Topic/Question
Reply



MultiQuote



|