java 如何遍历对象的ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39928789/
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
How to iterate through ArrayList of objects?
提问by sudomeacat
I have a class called SparseMatrix. It contains an ArrayList of Nodes (also class). I am wondering of how to iterate through the Array and access a value in Node. I have tried the following:
我有一个叫做 SparseMatrix 的类。它包含节点的 ArrayList(也是类)。我想知道如何遍历数组并访问 Node.js 中的值。我尝试了以下方法:
//Assume that the member variables in SparseMatrix and Node are fully defined.
class SparseMatrix {
ArrayList filled_data_ = new ArrayList();
//Constructor, setter (both work)
// The problem is that I seem to not be allowed to use the operator[] on
// this type of array.
int get (int row, int column) {
for (int i = 0; i < filled_data_.size(); i++){
if (row * max_row + column == filled_data[i].getLocation()) {
return filled_data[i].getSize();
}
}
return defualt_value_;
}
}
I will probably switch to static arrays (and remake it every time I add an object). If anyone has a solution, I would very much appreciate you sharing it with me. Also, thank you in advance for helping me.
我可能会切换到静态数组(并在每次添加对象时重新制作)。如果有人有解决方案,我将非常感谢您与我分享。另外,预先感谢您帮助我。
Feel free to ask questions if you don't understand anything here.
如果您在这里有任何不明白的地方,请随时提出问题。
采纳答案by nhouser9
First of all, you should not use raw types. See this link for more info: What is a raw type and why shouldn't we use it?
首先,您不应该使用原始类型。有关更多信息,请参阅此链接:什么是原始类型,我们为什么不应该使用它?
The fix is to declare the type of object held by your array list. Change the declaration to:
解决方法是声明数组列表所持有的对象类型。将声明更改为:
ArrayList<Node> filled_data_ = new ArrayList<>();
Then you can access each element in the array list using filled_data_.get(i)
(as opposed to filled_data_[i]
, which would work for a regular array).
然后您可以使用访问数组列表中的每个元素filled_data_.get(i)
(而不是filled_data_[i]
,它适用于常规数组)。
`filled_data_.get(i)`
The above will return the element at index i
. Documentation here: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int)
以上将返回 index 处的元素i
。此处的文档:https: //docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int)
回答by HARDI
Assuming filled_data_ is a list that contains list of objects of a class named Node.
假设fill_data_ 是一个包含名为Node 的类的对象列表的列表。
List<Nodes> filled_data_ = new ArrayList<>();
for (Node data : filled_data_) {
data.getVariable1();
data.getVariable2();
}
More info http://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/
更多信息http://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/
回答by Faizal
If you didn't use generic, then you need to cast the object
如果您没有使用泛型,那么您需要强制转换对象
//Assume that the member variables in SparseMatrix and Node are fully defined.
class SparseMatrix {
ArrayList filled_data_ = new ArrayList();
//Constructor, setter (both work)
// The problem is that I seem to not be allowed to use the operator[] on
// this type of array.
int get (int row, int column) {
for (int i = 0; i < filled_data_.size(); i++){
Node node = (Node)filled_data.get(i);
if (row * max_row + column == node.getLocation()) {
return node.getSize();
}
}
return defualt_value_;
}
}
}
回答by c0der
If array list contains Nodes
which defines getLocation()
you could use :
如果数组列表包含您可以使用的Nodes
定义getLocation()
:
((Nodes)filled_data_.get(i)).getLocation()
You could also define
你也可以定义
ArrayList<Nodes> filled_data_ = new ArrayList<Nodes>();
回答by Wojciech Kazior
When you create the ArrayList
object, you should specify the type of the contained elements with <>
brackets. It is also good to keep the reference to the List
interface - not ArrayList
class. To iterate through such a collection, use foreach
loop:
创建ArrayList
对象时,应使用<>
方括号指定所包含元素的类型。保留对List
接口而不是ArrayList
类的引用也很好。要遍历这样的集合,请使用foreach
循环:
Here is an example of the Node class:
下面是一个 Node 类的例子:
public class Node {
private int value;
public Node(int value) {
this.value = value;
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Here is an example of the Main class:
这是 Main 类的示例:
public class Main {
public static void main(String[] args) {
List<Node> filledData = new ArrayList<Node>();
filledData.add(new Node(1));
filledData.add(new Node(2));
filledData.add(new Node(3));
for (Node n : filledData) {
System.out.println(n.getValue());
}
}
}