Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java-使用Arraylist实现邻接列表_Java_Arraylist_Nested - Fatal编程技术网

java-使用Arraylist实现邻接列表

java-使用Arraylist实现邻接列表,java,arraylist,nested,Java,Arraylist,Nested,这就是我所拥有的: class Node { Integer value; ArrayList<Integer> adjList; public Node(Integer val, ArrayList<Integer> l) { value = val; Collections.sort(l); adjList = l; } } 但让我烦恼的是: 如果我将邻接列表声明为ArrayList

这就是我所拥有的:

class Node {

    Integer value;
    ArrayList<Integer> adjList;

    public Node(Integer val, ArrayList<Integer> l) {
        value = val;
        Collections.sort(l);
        adjList = l;
    }
}
但让我烦恼的是:

如果我将邻接列表声明为
ArrayList
类型,那么该列表的每个元素会存储一个指针还是保留一个节点的完整副本(从而将一个ArrayList嵌套在另一个ArrayList中…无限远!)

我希望它被存储为指针。在这里做什么?

与C(++)不同,Java总是对对象使用指针。这意味着列表将只存储这些引用

然后,节点中的列表也将存储对其他列表的引用,但每个列表在内存中只存在一次

class Node {

    Integer value;
    ArrayList<Node> adjList;

    public Node(Integer val, ArrayList<Integer> l) {
        value = val;
        Collections.sort(l,/*some comparator*/);
        adjList = l;
    }
}
private void process(){
    Random r = new Random(System.currentTimeMillis());
    while(vertices.size() > 2){
        Node v1 = vertices.get(r.nextInt(vertices.size()));
        Node v2 = v1.adjList.get(r.nextInt(v1.adjList.size()));
        contract(v1,v2); // Randomized contraction algorithm aka Karger's Min Cut algorithm
    }
}