ArrayList.add(int index, element) doesn't seem to move element


I have a long list of Card objects. If the user misses one question I want a card of the same category to moved to the next slot. Coming the list is sorted in a way that staggers all the cateogries, so that every next question is different.

I have a sorting method that should take in the current position in the list and the desired category of the next element and move the next occurance of an element of that type to right after the current position.

I don't see anything wrong with this algorithim so I think its possible that I am using the ArrayList methods wrong. However, after looking at the docs it seems they should work as expected.

I have tested list after it was supposedly sorted and nothing had moved, but the list did contain the category I was looking for.

public static void sortCategoryToNext(ArrayList<Card> cardlist,String cat,int currentIndex){
           boolean sorted = false;
           int i = currentIndex;
           int insertionpoint = i;
            do {
                Card nextCard = cardlist.get(i);
                    sorted = true;


Show source
| java   | sorting   | data-structures   2017-08-08 21:08 1 Answers

Answers to ArrayList.add(int index, element) doesn&#39;t seem to move element ( 1 )

  1. 2017-08-08 22:08

    ArrayList.remove(Object) removes the first occurrence of the object, which is at the list position at which you had just inserted that object.

    Instead of this:


    try this:


    As a refinement, the above code also uses ArrayList.remove(int), which should be more efficient, since it does not have to iterate through the list to find the matching item.

Leave a reply to - ArrayList.add(int index, element) doesn't seem to move element

◀ Go back