191
public static ArrayList mainList = someList;

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

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
KJW
  • 15,035
  • 47
  • 137
  • 243
  • 90
    New to Java, wanted to know how to access an ArrayList element, Googled it, first result was this question. Got what I needed in a few seconds. – Gareth Lewis Mar 15 '13 at 09:55
  • 1
    JavaDoc is the documentation for Java, it contains all Objects and it's methods – xorinzor Nov 06 '13 at 21:49
  • 1
    it's a bit of an easy question, but SO posts always come up first on Google and therefore we have all of these upvotes. – SDG Aug 24 '15 at 23:15
  • 6
    JavaDoc is > 600 lines of clutter with respect to this question so referring to it is inefficient. – m12lrpv Jul 27 '18 at 22:47
  • Stackoverflow questions I always find are easier to understand and comprehend as well as more concise compared to javadoc or online tutorials. – sam1370 Jul 03 '19 at 02:35

8 Answers8

261

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);

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
Tomas Narros
  • 13,390
  • 2
  • 40
  • 56
40

Time to familiarize yourself with the ArrayList API and more:

ArrayList at Java 6 API Documentation

For your immediate question:

mainList.get(3);

wkl
  • 77,184
  • 16
  • 165
  • 176
14
mainList.get(list_index)
Upul Bandara
  • 5,973
  • 4
  • 37
  • 60
  • Presumably this was downvoted given the lack of explanation or link to where this function is documented, or perhaps just because it's (by far) the worst of (now) 6 answers which all say essentially the same thing. – Bernhard Barker Aug 13 '15 at 17:37
6
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!

Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
5

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.

Vamsi
  • 51
  • 2
  • 9
4

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

YoK
  • 14,329
  • 4
  • 49
  • 67
3

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.

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
Ash
  • 31
  • 1
-2

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);
1ambharath
  • 391
  • 3
  • 14