Java 获取特定的 ArrayList 项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3920602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:53:49  来源:igfitidea点击:

Get specific ArrayList item

javaarraylist

提问by KJW

public static ArrayList mainList = someList;

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

我如何从中获得特定项目ArrayListmainList[3]?

采纳答案by Tomas Narros

As many have already told you:

正如许多人已经告诉你的那样:

mainList.get(3);

Be sure to check the ArrayList Javadoc.

请务必检查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);

另外,请注意数组索引:在 Java 中,第一个元素位于 index 处0。所以如果你想获得第三个元素,你的解决方案是mainList.get(2);

回答by wkl

Time to familiarize yourself with the ArrayListAPI and more:

是时候熟悉ArrayListAPI 等了:

ArrayListat Java 6 API Documentation

ArrayList在 Java 6 API 文档中

For your immediate question:

对于您的直接问题:

mainList.get(3);

mainList.get(3);

回答by Upul Bandara

mainList.get(list_index)

回答by Tyler Treat

mainList.get(3);

For future reference, you should refer to the Java API for these types of questions:

为了将来参考,您应该参考 Java API 了解以下类型的问题:

http://download.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

http://download.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

It's a useful thing!

这是一个有用的东西!

回答by YoK

You can simply get your answer from ArrayList APIdoc.

您可以简单地从 ArrayList API文档中获得答案。

Please always refer API documentation .. it helps

请始终参考 API 文档 .. 它有帮助

Your call will looklike following :

您的电话将如下所示:

mainList.get(3);

Here is simple tutorial for understanding ArrayList with Basics :) :

这是通过基础理解 ArrayList 的简单教程:):

http://www.javadeveloper.co.in/java/java-arraylist-tutorial.html

http://www.javadeveloper.co.in/java/java-arraylist-tutorial.html

回答by Vamsi

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.

我们使用 mainList.get(index) 打印值,其中 index 以“0”开头。例如: mainList.get(2) 打印列表中的第三个元素。

回答by 1ambharath

I have been using the ArrayListAdapter to dynamically put in the entries into the respective fields ; This can be useful , for future queries

我一直在使用 ArrayListAdapter 动态地将条目放入相应的字段中; 这对将来的查询很有用

 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

And then , you can fetch any arraylist item as below :

然后,您可以获取任何数组列表项,如下所示:

arrayListName(info.position);

回答by Ash

Try:

尝试:

ArrayListname.get(index);

Where indexis the position in the index and ArrayListnameis the name of the Arraylist as in your case is mainList.

index索引中的位置在哪里ArrayListname,在您的情况下是 Arraylist 的名称是 mainList。