5

I want to get the index of a newly added element into an arraylist. e.g.

List listA = new ArrayList();

// Lots of code adding and removing things from `listA`, imagine `listA` now 
// contains approx 10,000 records

listA.add("element 0");

How do I find out the index of the newly added item?

I think that perhaps, I need a different data structure, but cannot think of what to use.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Matthew Smith
  • 366
  • 2
  • 4
  • 16
  • Out of interest, why do you need to know this index value? It hints to me that you might be doing something the wrong way... – Duncan Jones Aug 26 '14 at 14:05

7 Answers7

7

The first element added to the list would have index 0.
The last element added would have the index listA.size()-1.
However, you should note that if you remove elements from the list, some of the indices (indices of elements that had higher index than the removed element) would change. For example, if you remove the n'th element, the former (n+1)'th element would become the n'th element.

Thanks talex for the comment. I forgot about indexOf(), which returns the index of the supplied element (if that element is found in the list). However, this requires linear search time.

If you wish to treat the indices of the list as keys to find the values stored in the list, you might be better off using a HashMap<Integer,String>.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    And you can use `indexOf` method. – talex Aug 26 '14 at 13:44
  • OK, I would be removing elements from the list, so that would be a problem. I could just not bother removing the elements, but that seems bad coding... Is there any other data structure that I could remove items from whilst preserving an index to access the items by? – Matthew Smith Aug 26 '14 at 13:48
  • Also keep in mind this won't work if (ab)using [`set()`](http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#set-int-E-) – blgt Aug 26 '14 at 13:50
  • Hashmap does seem the way forward. Thank you for your help. – Matthew Smith Aug 26 '14 at 14:00
1

The index will be size(), just get it before adding:

List listA = new ArrayList();
...
int index = listA.size();

listA.add("element 0"); // "element 0" will be at the "index" item
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

You may retrieve index of any element in your list using indexOf method :

List<String> listA = new ArrayList<String>();

//Lots of code adding and removing things from listA, imagine listA now contains approx 10,000 records

String str = "element 0"
listA.add(str);

int index = listA.indexOf(str)

If you are only concerned by the index of the last element, the list javadoc says : " add(E e) Appends the specified element to the end of this list (optional operation)."

so you can use :

List<String> listA = new ArrayList<String>();

//Lots of code adding and removing things from listA, imagine listA now contains approx 10,000 records

listA.add("element 0");

int index = listA.size()-1 //minus 1 because index start at 0

As you seem concerned to access element by index ( identifier ?) you should take a look at Map :

Map<int, String> mymap = new HashMap<int, String>();
mymap.add(1, "element1");
mymap.add(2, "element2");
mymap.add(3, "element3");

mymap.get(3);//return "element3"
Arthur Vaïsse
  • 1,551
  • 1
  • 13
  • 26
0
List listA = new ArrayList();

String a = "element 0";

listA.add(a);

int index = listA.indexOf(a);

there you go

Lucas
  • 3,181
  • 4
  • 26
  • 45
  • Your solution could be time cousuming if listA contains a lot of items; another issue is that if ListA contains the same items – Dmitry Bychenko Aug 26 '14 at 13:49
  • Well I always thought ArrayList is meant for fast retrieving values. I don't know how big it would have to be to make it slow... – Lucas Aug 26 '14 at 14:05
  • @Lucas The implementation of `indexOf` will have to loop through the entire list, looking for a matching item. This will be slow compared with other approaches of finding the index. – Duncan Jones Aug 26 '14 at 14:08
0

If you use add(element) order is preserved so the last element in the List will be the most recently add one. So you can use listA.get(listA.size-1);

If you are using add(index,element) this will not be the case as order is not preserved.

See here for related question How to get the last value of an ArrayList

Community
  • 1
  • 1
Revive
  • 2,248
  • 1
  • 16
  • 23
0

Index of a List starts with 0 and ends with size()-1.

If you calculate number of elements, it would be same of size()-1.

For example if your list contains 100 records, then index of last element will be size()-1 which is 99.

List listA = new ArrayList();
int lastIndex=listA.size()-1;
Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
0

unlike what other suggest....size() gives the total size of the List and since List indexes are zero based so size() - 1 gives the index of the last element int he list.

Don't confuse the ;ast element as the element you just added. As long as we user the add() method new elements get appended to the list at the end, but list also has another add method which takes the index as an argument. Using this method you can add the new element anywhere in the list. In that case size() - 1 would not give you the index of the latest added element.

Nazgul
  • 1,892
  • 1
  • 11
  • 15