Search code examples
arraylistadditionnodes

JAVA ArrayList<Object>


Question2:

I'm confused on ArrayList<Object>, please explain to me the following:

I have a class Node which has two fields: data1 and data2

public class Node {
    private static int data1;
    private static int data2;
 
    public Node(){...}
    public static void setData1(int data);
    public static void getData1();
    public static void setData2(int data);
    public static void getData2();
} // end of class Node

And then I have another class called Link.

public class Link {
    private ArrayList<Node> linkList = new ArrayList<Node>();
    private Node node = new Node();
    ...
    linkList.add(node)
    linkList.get(how to do it here)
} // end of class Link

I want to output the Node data inside linkList.

linkList.get(how to do it here)

How would I do that?


Solution

  • I think you simply forgot to do something like

    private ArrayList<node> linkList = new ArrayList<node>();
    

    Try:

    public class Link {
        private ArrayList<node> linkList = new ArrayList<node>();
        private node nodelist = new node();
        ...
        linkList.add(nodelist)
    } // end of class link
    

    EDIT

    Take a look to the following sample code taken from here to understand how to work with ArrayList<...>

    java.util.ArrayList<String>  v = new java.util.ArrayList<String>();
        v.add( "able" );
        v.add( "baker" );
        v.add( "charlie" );
        v.add( "delta" );
    
    int n = v.size();
    for(int i = 0; i < n ; i++)
        System.out.println( v.get( i ) );