191
public static ArrayList mainList = someList;

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

5
  • 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. Commented Mar 15, 2013 at 9:55
  • 1
    JavaDoc is the documentation for Java, it contains all Objects and it's methods Commented Nov 6, 2013 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. Commented Aug 24, 2015 at 23:15
  • 6
    JavaDoc is > 600 lines of clutter with respect to this question so referring to it is inefficient. Commented Jul 27, 2018 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. Commented Jul 3, 2019 at 2:35

8 Answers 8

262

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

Sign up to request clarification or add additional context in comments.

Comments

40

Time to familiarize yourself with the ArrayList API and more:

ArrayList at Java 6 API Documentation

For your immediate question:

mainList.get(3);

Comments

14
mainList.get(list_index)

1 Comment

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.
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!

Comments

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.

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.