191
public static ArrayList mainList = someList;

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

The Guy with The Hat
  • 10,290
  • 8
  • 59
  • 73
KJW
  • 14,705
  • 45
  • 133
  • 241
  • 9
    Downvoted for not referring ArrayList API Javadoc. Which is also easily available online. – YoK Oct 13 '10 at 03:52
  • 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,290
  • 8
  • 59
  • 73
Tomas Narros
  • 13,317
  • 2
  • 38
  • 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);

逆さま
  • 71,868
  • 14
  • 160
  • 174
14
mainList.get(list_index)
Upul Bandara
  • 5,855
  • 4
  • 34
  • 59
  • 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,350
  • 13
  • 77
  • 113
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
  • 8
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,075
  • 4
  • 47
  • 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,562
  • 11
  • 31
  • 50
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
  • 13