How to iterate through an ArrayList of objects?

I have a class called SparseMatrix. It contains an ArrayList of nodes (also a class). I am wondering how to go through an array and access a value in Node. I 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 redo it every time I add an object). If anyone has a solution, I would really appreciate that you share it with me. Also, in advance for helping me.

Feel free to ask questions if you do not understand anything here.

+5
source share
5 answers

First of all, you should not use raw types. See this link for more information: What is a raw type and why shouldn't we use it?

The fix is ​​to declare the type of object stored in your list of arrays. Change the announcement to:

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

Then you can access each element in the list of arrays using filled_data_.get(i) (as opposed to filled_data_[i] , which will work for a regular array).

 `filled_data_.get(i)` 

The above item will return the item to index i . Documentation here: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int)

+2
source

Assuming fill_data_ is a list containing a list of class objects named Node.

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

Additional Information http://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/

+3
source

If you have not used generic, you need to drop 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_; } 

}

+1
source

If the array list contains Nodes , which defines getLocation() , you can use:

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

You can also define

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

When you create an ArrayList object, you must specify the type of contained elements with the brackets <> . It is also useful to keep a reference to the List interface - not an ArrayList . To iterate through such a collection, use the foreach :

Here is an example 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 a 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()); } } } 
0
source

Source: https://habr.com/ru/post/1257882/


All Articles