KJW
KJW

Reputation: 15251

Get specific ArrayList item

public static ArrayList mainList = someList;

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

Upvotes: 191

Views: 612645

Answers (8)

Ash
Ash

Reputation: 31

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.

Upvotes: 3

1ambharath
1ambharath

Reputation: 391

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

Upvotes: -2

Tomas Narros
Tomas Narros

Reputation: 13468

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

Upvotes: 262

Vamsi
Vamsi

Reputation: 51

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.

Upvotes: 5

YoK
YoK

Reputation: 14505

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

Upvotes: 4

Tyler Treat
Tyler Treat

Reputation: 14988

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!

Upvotes: 6

Upul Bandara
Upul Bandara

Reputation: 5958

mainList.get(list_index)

Upvotes: 14

wkl
wkl

Reputation: 79893

Time to familiarize yourself with the ArrayList API and more:

ArrayList at Java 6 API Documentation

For your immediate question:

mainList.get(3);

Upvotes: 40

Related Questions