0

I have an ArrayList<String> of length 4 like below -

{"ABC","DEF","GHI","JKL"}

In above ArrayList the index of data ABC is 0, the index of data DEF is 1 and so on. Now, there is need that suppose we change the index of data ABC from 0 to 2 in that condition my ArrayList look like below

{"DEF","GHI","ABC","JKL"}

again I change in above the ArrayList the index of data GHI from 1 to 4 my array look like below

{"DEF","ABC","JKL","GHI"}

In above discussion, I wanted to know that when I changed the index of a single data how can we get index of another data. I hope you will be understand my question.

I have a ListView in android that have drag and drop row. When drag and drop a row from position 0 to 3, then the position of another row will be change. How can we retrieve another row position.

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
shailesh ojha
  • 247
  • 4
  • 15

4 Answers4

0

Use indexOf method.

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

int indexOfAbd = yourList.indexOf("ABD");
Alessandro Da Rugna
  • 4,571
  • 20
  • 40
  • 64
0

You should ideally use a LinkedList and whenever you need to modify the list

  1. First store the element of initial index in a temp variable
  2. Delete it from the list.remove(int index)
  3. Use add(int index, E element) with the new index.
Helios
  • 851
  • 2
  • 7
  • 22
0

Assuming that I understand your question correctly, you want to automatically keep track of the indices to existing array elements while you remove an element and add it at another position.

E.g. [A, B, C, D] has the indices [A:0, B:1, C:2, D:3]. Now you are removing A and insert it again at position 3: [B, C, A, D]. This results in [A:2, B:0, C:1, D:3].

If this paraphrases your question correctly, then no, there is no automatic way to update the indices to existing elements while you are removing and inserting elements.

Note that there is indexOf(x), which will search the list again for the first occurrence of x. If you are willing to pay the cost for the recurring search and if your elements are unique, this might help you.

Markus
  • 3,155
  • 2
  • 23
  • 33
0

Update an index does not make much sense per itself: the index is defined as the position of an item in an array. But you can:

  • find an item in an ArrayList: int indexOf(E item)
  • remove an item from an ArrayList: E remove(int index) or boolean remove(E item)
  • insert an item at a given position in an ArrayList: void add(int index, E item)

If I have correctly understood your question, what you want to do could look like:

boolean changeIndex(String item, int index, List<String> lst) {
    boolean cr = lst.remove(item);
    if (cr == false) {
        return cr;
    }
    lst.add(index, item);
    return true;
}

This code returns false if the item was not present in lst and throws an IndexOutOfBoundsException if index is negative (strictly) or greater (or equal) than the size of the array.

BTW, above code will work the same on any modifyable list, for example an ArrayList or a LinkedList - for operations like that, a LinkedList should be more appropriate

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252