Get the index of a newly added item in a list?

I want to get the index of an added element in an arraylist. eg

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 a recently added item?

I think that maybe I need a different data structure, but I can't think of what to use.

+3


source to share


7 replies


The first item added to the list will be at index 0.
The last item added will be at index listA.size()-1

.
Note, however, that if you remove items from the list, some of the indices (the indices of the items with a higher index than the removed item) will change. For example, if you remove the nth element, the first (n + 1) th element becomes the nth element.

Thanks talex for the comment. I forgot about indexOf()

which returns the index of the provided item (if that item is found in the list). However, this requires linear search time.



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

.

+6


source


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

      

+1


source


You can get the index of any item in your list with the method indexOf

:

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 interested in the index of the last item, the javadoc list says "add (E e) Adds the specified item to the end of this list (optional).

so that 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 think you need access to the element by index (id?), You should take a look at the 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"

      

+1


source


List listA = new ArrayList();

String a = "element 0";

listA.add(a);

int index = listA.indexOf(a);

      

there you go

0


source


If you use add(element)

, the order is preserved, so the last item in the list will be the last to add. So you can uselistA.get(listA.size-1);

If you use add(index,element)

it will not be the case as the order is not preserved.

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

0


source


Pointer a List

starts with 0

and ends with size()-1

.

If you count the number of elements, it will be the same as that of size()-1

.

For example, if your list contains 100 entries, then the index of the last element will be size()-1

99.

List listA = new ArrayList();
int lastIndex=listA.size()-1;

      

0


source


unlike other suggestions .... size()

gives the total size List

, and since the indices List

are zero, therefore size() - 1

gives the index of the last element of int it list.

Don't confuse the: ast element as the element you just added. For now, we, the user, are adding new method elements add()

to the list at the end, but the list also has another add method that takes an index as an argument. With this method, you can add a new item anywhere in the list. In this case, size() - 1

will not give you the index of the last added element.

0


source







All Articles