0

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?


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 ) );
4

2 回答 2

1

OP solved it by using:

linkList.get(0).getData();
于 2015-05-21T14:16:14.070 回答
1

我想你只是忘了做类似的事情

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

尝试:

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

编辑

查看从此处获取的以下示例代码以了解如何使用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 ) );
于 2012-11-13T06:12:37.817 回答