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