Get specific ArrayList item

余生长醉 提交于 2019-12-27 13:05:13

问题


public static ArrayList mainList = someList;

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


回答1:


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




回答2:


Time to familiarize yourself with the ArrayList API and more:

ArrayList at Java 6 API Documentation

For your immediate question:

mainList.get(3);




回答3:


mainList.get(list_index)



回答4:


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!




回答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.




回答6:


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




回答7:


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.




回答8:


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


来源:https://stackoverflow.com/questions/3920602/get-specific-arraylist-item

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!