Java:难道不能使用迭代器来迭代对象的HashMap并改变对象的属性吗?

标签 java oop iterator

我知道这对于这里的专家来说可能是一个非常基本的问题。但我无法理解这个问题。 基本上,我有以下深度优先搜索实例方法。我有一个对象类 Dgraph ,即HashMap<Vertex, ArrayList<Vertex>>Vertex只是我创建的一个对象类,它包含一个 color属性和 node属性。

问题:基本上,我想更改迭代器语句中 Vertex 对象的颜色。但它并没有按预期工作。我能够更改 color迭代器中每个顶点对象的属性,但是当我离开迭代器时,每个顶点的颜色属性仍然是 null .

请注意,具有相同节点值的 Vertex 对象是同一对象,即考虑 5 --> 11 -- 9 ,我们只有一个节点等于 1 的 Vertex 对象这里。

public void depth_first_search(Dgraph graph){
        // for debugging
        System.out.println("Before iterator");
        System.out.println("Graph = " + graph);
        System.out.println("");
        graph.display();
    HashMap<Vertex, ArrayList<Vertex>> dag = graph.returnHashMap(graph);
    for(Map.Entry<Vertex, ArrayList<Vertex>> g_map : dag.entrySet() ){
        Vertex u = g_map.getKey();
        u.set_color("WHITE");
        System.out.println("In iterator --> Vertex u = " + u + " color = " + u.get_color());
    }
    // for debugging 
    System.out.println("*************************");
    System.out.println("After iterator");
    System.out.println("Graph = " + dag);
    graph.display();
    }
}

这是我得到的调试输出:

Before iterator
Graph = Dgraph@7852e922

5 : [1, 9]
    null    null
3 : [511596]
    null
2 : [47646, 47647, 13019, 47648, 47649, 47650, 7700, 47651, 47652]
    null    null    null    null    null    null    null    null    null
1 : [1, 2, 5, 6, 7, 3, 8, 4]
    null    null    null    null    null    null    null    null
in iterator --> Vertex u = 5 color = WHITE
in iterator --> Vertex u = 3 color = WHITE
in iterator --> Vertex u = 2 color = WHITE
in iterator --> Vertex u = 1 color = WHITE
*************************
After iterator
Graph = {5=[1, 9], 3=[511596], 2=[47646, 47647, 13019, 47648, 47649, 47650, 7700, 47651, 47652], 1=[1, 2, 5, 6, 7, 3, 8, 4]}

5 : [1, 9]
    null    null
3 : [511596]
    null
2 : [47646, 47647, 13019, 47648, 47649, 47650, 7700, 47651, 47652]
    null    null    null    null    null        null    null   null   null
1 : [1, 2, 5, 6, 7, 3, 8, 4]
    WHITE   null        null    null    null    null    null       null

我删除了我的toString方法,这样你们就可以查看引用并检查其中一些引用同一个对象。

Before iterator
Graph = Dgraph@7852e922

Vertex@9b : [Vertex@1f, Vertex@117]
    null    null
Vertex@5d : [Vertex@f1ff14]
    null
Vertex@3e : [Vertex@1689a2, Vertex@1689c1, Vertex@62885, Vertex@1689e0, Vertex@1689ff, Vertex@168a1e, Vertex@3a46c, Vertex@168a3d, Vertex@168a5c]
    null    null    null    null    null    null    null    null    null
Vertex@1f : [Vertex@1f, Vertex@3e, Vertex@9b, Vertex@ba, Vertex@d9, Vertex@5d, Vertex@f8, Vertex@7c]
    null    null    null    null    null    null    null    null
in iterator --> Vertex u = Vertex@9b color = WHITE
in iterator --> Vertex u = Vertex@5d color = WHITE
in iterator --> Vertex u = Vertex@3e color = WHITE
in iterator --> Vertex u = Vertex@1f color = WHITE
*************************
After iterator
Graph = {Vertex@9b=[Vertex@1f, Vertex@117], Vertex@5d=[Vertex@f1ff14], Vertex@3e=[Vertex@1689a2, Vertex@1689c1, Vertex@62885, Vertex@1689e0, Vertex@1689ff, Vertex@168a1e, Vertex@3a46c, Vertex@168a3d, Vertex@168a5c], Vertex@1f=[Vertex@1f, Vertex@3e, Vertex@9b, Vertex@ba, Vertex@d9, Vertex@5d, Vertex@f8, Vertex@7c]}

Vertex@9b : [Vertex@1f, Vertex@117]
    null    null
Vertex@5d : [Vertex@f1ff14]
    null
Vertex@3e : [Vertex@1689a2, Vertex@1689c1, Vertex@62885, Vertex@1689e0, Vertex@1689ff, Vertex@168a1e, Vertex@3a46c, Vertex@168a3d, Vertex@168a5c]
    null    null    null    null    null    null    null    null    null
Vertex@1f : [Vertex@1f, Vertex@3e, Vertex@9b, Vertex@ba, Vertex@d9, Vertex@5d, Vertex@f8, Vertex@7c]
    WHITE   null    null    null    null    null    null    null

更新:实例方法 returnHashMap坐在类(class)里Dgraph如下:

class Dgraph{

    // instance variable
    HashMap<Vertex, ArrayList<Vertex>> dag;

    // constructor
    public Dgraph(String file, boolean reverse) throws IOException{
        dag = read_file_and_populate(file, reverse);
    }

    public HashMap<Vertex, ArrayList<Vertex>> returnHashMap(Dgraph g){
        return g.dag;
    }
    .....
}

Vertex 类如下所示:

class Vertex{
    private long node;
    private String color;
    private long d;
    private long pi;
    private long f;

public Vertex(long node){
    this.node = node;
}

// return color
public String get_color(){
    return color;
}

// change color
public void set_color(String color){
    this.color = color;
}

// return distance
public long get_d(){
    return d;
}

// change distance
public void set_d(long d){
    this.d = d;
}

// return predecessor
public long get_pi(){
    return pi;
}

// assign predecessor
public void set_pi(long pi){
    this.pi = pi;
}

// to String (prevent print reference)
public String toString(){
    return node+"";
}

// return node
public long get_node(){
    return node;
}

// return finishing time
public long get_f(){
    return f;
}

// set finishing time
public void set_f(long f){
    this.f = f;
}

// Need hashCode() and equals() to compare objects
 public int hashCode(){
     return (int)(node * 31);
}

public boolean equals(Object o) {
    if (o == this){
        return true;
    }
    if (o == null || getClass() != o.getClass()){
        return false;
    }
    Vertex other = (Vertex)o;
    return node  == other.node;
}
}

这是我正在使用的显示方法:

public void display(){
    for(Map.Entry<Vertex, ArrayList<Vertex>> entry : dag.entrySet()){
        System.out.println(entry.getKey() + " : " + entry.getValue());
        for(Iterator<Vertex> iterator = dag.get(entry.getKey()).iterator(); iterator.hasNext();){
            Vertex vv = iterator.next();
            System.out.print("\t" + vv.get_color());
        }
        System.out.println("");

    }
}

更新:下面是我用来创建 HashMap 的代码。我以为我正在创建一个 HashMap,这样如果两个具有相同节点值的 Vertex 对象出现在 key 和 value 中,它们将共享相同的内存地址并指向相同的 Vertex 对象(即应该只有一个 Vertex 对象) )。

public Vertex getVertex(Map<Long,Vertex> vertices_map, long node){
        if( ! vertices_map.containsKey(node) ) vertices_map.put(node, new Vertex(node));
        return vertices_map.get(node);
    }

    // read file and return graphs
    public HashMap<Vertex, ArrayList<Vertex>> read_file_and_populate(String file_loc, boolean reverse) throws IOException{

    HashMap<Vertex, ArrayList<Vertex>> graph = new HashMap<Vertex, ArrayList<Vertex>>();
    int l = 0;
    int r = 0;
    if(reverse == false){
        r = 1;
    } else{
        l = 1;
    }

    FileInputStream fil = new FileInputStream(file_loc);
    BufferedReader br = new BufferedReader( new InputStreamReader(fil));
    String element = null;

    while( (element = br.readLine()) != null){
        String[] line = element.split("\\s");
        /*
         *
         *  Create a new vertex object when the node first occurs;
         *  And if the node occurs more than once, return a copy of the
         *  reference to the same object with the same node value.
         *
         */
        HashMap<Long, Vertex> vertices_map = new HashMap<Long, Vertex>();
        Vertex v_l = getVertex(vertices_map , Long.parseLong(line[l]) );
        Vertex v_r = getVertex(vertices_map , Long.parseLong(line[r]) );

        if(graph.containsKey(v_l) == false){
            ArrayList<Vertex> edges_of_this_vertex = new ArrayList<Vertex>();
            edges_of_this_vertex.add(v_r);
            graph.put(v_l, edges_of_this_vertex);
        } else{
            graph.get(v_l).add(v_r);
        }
    }
    return graph;
}

最佳答案

您修改的颜色,但显示方法仅打印的颜色。您有一个顶点,它是一个键和一个值(“Vertex@1f”),这就是为什么您的输出的底行中有单独的“WHITE”输出。

更新:

看看这一行(注意它在代码流中的位置):

    HashMap<Long, Vertex> vertices_map = new HashMap<Long, Vertex>();

关于Java:难道不能使用迭代器来迭代对象的HashMap并改变对象的属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27516723/

相关文章:

python - 与 zip(*[iter(s)]*n) 相反

c++ - 如何从 C++ 中的另一个类迭代获取数据?

c++ - 如何使用迭代器作为模板参数并返回其值?

java - 如何从 android.jar 中删除包?

java - Java 中的日志记录是怎么回事?

c# - 我在这里需要抽象类或接口(interface)吗(或者两者都不需要)?

php - 在没有多重继承的情况下如何解决这个问题?

c++ - 在类的树层次结构中访问对象时避免空指针

java - 在 Java 中读取序列化对象时获取 EOFException

java - 为什么会返回 org.h2.jdbc.JdbcSQLException?