Get specific ArrayList item

JavaArraylist

Java Problem Overview


public static ArrayList mainList = someList;

How can I get a specific item from this ArrayList? mainList[3]?

Java Solutions


Solution 1 - Java

As many have already told you:

mainList.get(3);

Be sure to check the ArrayList Javadoc.

Also, be careful with the arrays indices: in Java, the first element is at index 0. So if you are trying to get the third element, your solution would be mainList.get(2);

Solution 2 - Java

Time to familiarize yourself with the ArrayList API and more:

ArrayList at Java 6 API Documentation

For your immediate question:

mainList.get(3);

Solution 3 - Java

mainList.get(list_index)

Solution 4 - Java

mainList.get(3);

For future reference, you should refer to the Java API for these types of questions:

http://download.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

It's a useful thing!

Solution 5 - Java

We print the value using mainList.get(index) where index starts with '0'. For Example: mainList.get(2) prints the 3rd element in the list.

Solution 6 - Java

You can simply get your answer from ArrayList API doc.

Please always refer API documentation .. it helps

Your call will looklike following :

mainList.get(3);

Here is simple tutorial for understanding ArrayList with Basics :) :

http://www.javadeveloper.co.in/java/java-arraylist-tutorial.html

Solution 7 - Java

Try:

ArrayListname.get(index);

Where index is the position in the index and ArrayListname is the name of the Arraylist as in your case is mainList.

Solution 8 - Java

I have been using the ArrayListAdapter to dynamically put in the entries into the respective fields ; This can be useful , for future queries

 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

And then , you can fetch any arraylist item as below :

arrayListName(info.position);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKJWView Question on Stackoverflow
Solution 1 - JavaTomas NarrosView Answer on Stackoverflow
Solution 2 - Java逆さまView Answer on Stackoverflow
Solution 3 - JavaUpul BandaraView Answer on Stackoverflow
Solution 4 - JavaTyler TreatView Answer on Stackoverflow
Solution 5 - JavaVamsiView Answer on Stackoverflow
Solution 6 - JavaYoKView Answer on Stackoverflow
Solution 7 - JavaAshView Answer on Stackoverflow
Solution 8 - Java1ambharathView Answer on Stackoverflow