【问题标题】:How to iterate through ArrayList of objects?如何遍历对象的ArrayList?
【发布时间】:2016-10-08 04:52:14
【问题描述】:

我有一个名为 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] 从 ArrayList 中获取元素,你必须使用 .get(i)。

标签: java arraylist


【解决方案1】:

假设 fill_data_ 是一个列表,其中包含一个名为 Node 的类的对象列表。

List<Nodes> filled_data_ = new ArrayList<>();
for (Node data : filled_data_) {
    data.getVariable1();
    data.getVariable2();
}

更多信息http://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/

【讨论】:

  • 我没有提到这个,但我不会解析整个ArrayList。不过,这很高兴知道。谢谢。
【解决方案2】:

首先,您不应该使用原始类型。有关更多信息,请参阅此链接:What is a raw type and why shouldn't we use it?

解决方法是声明数组列表所包含的对象类型。将声明更改为:

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

然后您可以使用filled_data_.get(i) 访问数组列表中的每个元素(而不是filled_data_[i],后者适用于常规数组)。

`filled_data_.get(i)`

上面将返回索引i 处的元素。此处的文档:https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int)

【讨论】:

  • 这很有帮助,谢谢。 (我有使用 C++ 的经验,所以 Java 概念对我来说相当新。)我找到了一种使用静态数组(效率低下)的方法,所以我会考虑到这一点。再次感谢您的帮助。
  • @diuqSehTnettiK 没问题 =]
【解决方案3】:

如果你没有使用泛型,那么你需要强制转换对象

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

}

【讨论】:

  • 听起来很有趣,请举例说明您的意思。
【解决方案4】:

如果数组列表包含定义getLocation() 的Nodes,您可以使用:

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

你也可以定义

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

【讨论】:

    【解决方案5】:

    创建ArrayList 对象时,应使用&lt;&gt; 括号指定所包含元素的类型。保留对List 接口的引用也很好——而不是ArrayList 类。要遍历这样的集合,请使用foreach 循环:

    这是一个 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;
        }
    }
    

    这是 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());
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-16
      • 2015-07-22
      • 2019-04-19
      • 2018-10-07
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 2017-08-12
      相关资源
      最近更新 更多