-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;
}
4
  • Is this what you want to do? stackoverflow.com/questions/157944/… Commented Feb 20, 2014 at 2:33
  • @codeNinja isn't that a recursive link? :) Commented Feb 20, 2014 at 2:37
  • possible duplicate of stackoverflow.com/questions/20623209/… (always good to be able to plug my own answers!) Commented Feb 20, 2014 at 2:38
  • Was your question answered? Commented Feb 20, 2014 at 3:32

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

I want to change the first code from int[] arrays to array lists, by using the name myArrayList
In that case, your code is absolutely correct! ArrayLists are indexed using get() and the size is found using size().
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]);
}

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.