Android: ArrayList Move Item to Position 0

13,951

Solution 1

You need to use Collections class's swap method. Collections, with an s at the end.

Change -

Collection.swap(myArray, i, 0);

to this -

Collections.swap(myArray, i, 0);

Take a look at this example.

Collection and Collections are two different things in Java. The first one is an interface, the second one is a class. The later one has a static swap method, but the former one doesn't.

Solution 2

I don't know what Collection.swap is, but this code should work:

for(int i=0; i<myArray.size(); i++){    
    if(myArray.get(i).isStartItem()){
        Collections.swap(myArray, i, 0);
        break;
    }
}

Or you can do it long-hand:

for(int i=0; i<myArray.size(); i++){    
    if(myArray.get(i).isStartItem()){
        Object thing = myArray.remove(i); // or whatever type is appropriate
        myArray.add(0, thing);
        break;
    }
}

Solution 3

There are 2 ways of moving an item to the desired position in the ArrayList.

1. Swap the items

Collections.swap(myArray, i, 0);

--> Here position "i" will be moved to 0th position and all the other items in between this range will remain as it is.

2. Shift the items

myArray.add(0,myArray.remove(i))

--> Here item at position "i" will be removed and added to the 0th position. Here all the other items position will be shifted as you're adding a new item at 0.

Hope this will helps you to understand the difference between swap and shift the position. Use the solution according to your requirement.

Share:
13,951
ryandlf
Author by

ryandlf

Updated on June 14, 2022

Comments

  • ryandlf
    ryandlf over 1 year

    I have an ArrayList and I need to make sure a specific item is at the 0 position and if it is not, I need to move it there. The item has an isStartItem boolean on it, so I can easily find the specific item I need to be in position 0 but then how do I go about moving it to the right position?

    I am assuming I need to use something like this:

    for(int i=0; i<myArray.size(); i++){    
        if(myArray.get(i).isStartItem()){
            Collection.swap(myArray, i, 0);
        }
    }
    

    But this does not seem to work...

  • Ted Hopp
    Ted Hopp over 12 years
    That will just replace whatever was in the arraylist at 0 with object. I don't think OP wants to lose any elements, just reorder them.
  • ryandlf
    ryandlf over 12 years
    Oops...I just forgot the s when I was typing out the question.
  • MD Sayem Ahmed
    MD Sayem Ahmed over 12 years
    @ryandlf: Oh.....then could you tell me what do you mean by "doesn't seem to work" ?
  • ryandlf
    ryandlf over 12 years
    I did try and this method and it did exactly as Ted described.
  • MrTristan
    MrTristan over 10 years
    does this actually move the element that is at 0 to i when it moves the element at i to 0? if so then i don't think it is what OP is looking for. I usually just simply create a new object to save what's at i, then call .remove(index) to to pull it out, then call .add(index, object) with index being 0. yada yada
  • Ankita Mojidra
    Ankita Mojidra about 3 years
    Really happy to got this answer, I am trying to from one hour but it wasnt work for me , Using this working fine for me