I was wondering if array list had respective methods that function the same as int[]
arrays. So would something like myArray[index]
be the same as myArray.get(index)
(assuming that myArray
is an array list in the second one)?
Some more examples:
myArray.length
= myArray.size()
If so, I need to convert code from int[]
arrays to array list. (By the way, myArray
is a private int[]
)
public boolean deleteZeros()
{
int rightZero = -1;
for(int position1 = 0; position1 < myArray.length; position1++)
{
if(myArray [position1] == 0)
{
rightZero = position1;
}
}
if(rightZero == -1)
return false;
int [] temp = new int [myArray.length - rightZero -1];
for(int position2 = 0; position2 < temp.length; position2++ )
{
temp [position2] = myArray [rightZero + 1 + position2];
}
numMoves += 1;
myArray = temp;
return true;
}
So would that code look like this? (after replacing myArray
with myArrayList
and fixing the respective methods)
public boolean deleteZeros()
{
int rightZero = -1;
for(int position1 = 0; position1 < myArrayList.size(); position1++)
{
if(myArrayList.get(position1) == 0)
{
rightZero = position1;
}
}
if(rightZero == -1)
return false;
int [] temp = new int [myArrayList.size() - rightZero -1];
for(int position2 = 0; position2 < temp.length; position2++ )
{
temp[position2] = myArrayList.get(rightZero + 1 + position2);
}
numMoves += 1;
for (int i = 0; i < temp.length; i ++)
{
myArrayList.set(i, temp[i]);
}
return true;
}