简体   繁体   English

如何遍历对象的ArrayList?

[英]How to iterate through ArrayList of objects?

I have a class called SparseMatrix. 我有一个名为SparseMatrix的类。 It contains an ArrayList of Nodes (also class). 它包含节点的ArrayList(也是类)。 I am wondering of how to iterate through the Array and access a value in Node. 我想知道如何遍历数组并访问Node中的值。 I have tried the following: 我尝试过以下方法:

 //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. 如果您对此无法理解,请随时提出问题。

Assuming filled_data_ is a list that contains list of objects of a class named Node. 假设filled_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/

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 . 以上将返回索引i处的元素。 Documentation here: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int) 文档: https//docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int)

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_;
 }

} }

If array list contains Nodes which defines getLocation() you could use : 如果数组列表包含定义getLocation() Nodes ,则可以使用:

((Nodes)filled_data_.get(i)).getLocation()

You could also define 你也可以定义

ArrayList<Nodes> filled_data_ = new ArrayList<Nodes>();

When you create the ArrayList object, you should specify the type of the contained elements with <> brackets. 创建ArrayList对象时,应使用<>括号指定包含元素的类型。 It is also good to keep the reference to the List interface - not ArrayList class. 保持对List接口的引用也很好 - 而不是ArrayList类。 To iterate through such a collection, use foreach loop: 要遍历这样的集合,请使用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());
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM