简体   繁体   中英

How to iterate through ArrayList of objects?

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:

 //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.

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/

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

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)

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 :

((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. It is also good to keep the reference to the List interface - not ArrayList class. To iterate through such a collection, use foreach loop:

Here is an example of the Node class:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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