-2

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;
}
Evan
  • 870
  • 1
  • 12
  • 24

4 Answers4

3

So would something like myArray[index] be the same as myArray.get(index)(assuming that myArray is an array list in the second one)?

Yes

Some more examples:

myArray.length = myArray.size()

Yes

So would that code look like this? (after replacing myArray with myArrayList and fixing the respective methods)

Yes it all looks good to me. Although, you could just try it.


However, a problem that could occur is that your temp array is not the same size as your original myArrayList, so then when setting all of your ArrayList elements to be your temp elements, some the last few elements in your ArrayList will remain unchanged. If you need clarification for what I mean, or you want me to come up with a solution for that possible case, let me know.

Community
  • 1
  • 1
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
0

To convert an int[] to an ArrayList, use

ArrayList<Integer> myArrayList = new ArrayList<Integer>(Arrays.asList());

You will need to import java.util.Arrays.

McLovin
  • 3,554
  • 1
  • 14
  • 15
  • I want to change the first code from `int[]` arrays to array lists, by using the name `myArrayList` – Evan Feb 20 '14 at 02:31
  • In that case, your code is absolutely correct! ArrayLists are indexed using `get()` and the size is found using `size()`. – McLovin Feb 20 '14 at 02:34
0

Just construct a new ArrayList, and then add each integer in your myArray to the ArrayList one by one. Sample code is here:

int[] myArray = new int[]{1,2,3};
List<Integer> myArrayList = new ArrayList<Integer>(myArray.length);
for (int i = 0; i < myArray.length; i++) {
    myArrayList.add(myArray[i]);
}
Weibo Li
  • 3,565
  • 3
  • 24
  • 36
0

Lists also have a method myArray.subList(fromIndex, toIndex) which would make it easier for you to cut your list, not neading for loops at the end.

esoyitaque
  • 61
  • 2