【问题标题】:Java: Object Array assignment in for loopJava:for循环中的对象数组赋值
【发布时间】:2012-11-19 07:52:59
【问题描述】:

我正在尝试使用 Dijkstra 的算法来找到从特定顶点(v0)到其余顶点的最短路径。这已解决,并且与以下链接中的此代码配合良好:http://en.literateprograms.org/index.php?title=Special:DownloadCode/Dijkstra%27s_algorithm_(Java)&oldid=15444

我无法根据用户输入在 for 循环中分配 Edge 数组,而不是像这里那样对其进行硬编码。

任何帮助从每个顶点为 Edge[] 邻接分配新边?请记住,它可能是 1 个或多个边缘。

class Vertex implements Comparable<Vertex>
{
    public final String name;
    public Edge[] adjacencies;
    public double minDistance = Double.POSITIVE_INFINITY;
    public Vertex previous;
    public Vertex(String argName) { name = argName; }
    public String toString() { return name; }

    public int compareTo(Vertex other){
    return Double.compare(minDistance, other.minDistance);
    }
}


class Edge{
    public final Vertex target;
    public final double weight;

    public Edge(Vertex argTarget, double argWeight){
        target = argTarget; weight = argWeight; }
    }

public static void main(String[] args)
{
    Vertex v[] = new Vertex[3];
    Vertex v[0] = new Vertex("Harrisburg");
    Vertex v[1] = new Vertex("Baltimore");
    Vertex v[2] = new Vertex("Washington");

    v0.adjacencies = new Edge[]{ new Edge(v[1],  1),
                             new Edge(v[2],  3) };
    v1.adjacencies = new Edge[]{ new Edge(v[0], 1),
                             new Edge(v[2],  1),};
    v2.adjacencies = new Edge[]{ new Edge(v[0],  3),
                                 new Edge(v[1],  1)  };

    Vertex[] vertices = { v0, v1, v2};
       /*Three vertices with weight: V0 connects (V1,1),(V2,3)
                                     V1 connects (V0,1),(V2,1)
                                     V2 connects (V1,1),(V2,3)
       */  
    computePaths(v0);
    for (Vertex v : vertices){
    System.out.println("Distance to " + v + ": " + v.minDistance);
    List<Vertex> path = getShortestPathTo(v);
    System.out.println("Path: " + path);
    }
}
}

上面的代码可以很好地找到从 v0 到所有其他顶点的最短路径。将新的 edge[] 分配给 edge[] 邻接时会出现问题。

例如,这不会产生正确的输出:

for (int i = 0; i < total_vertices; i++){
        s = br.readLine();
        char[] line = s.toCharArray();
        for (int j = 0; j < line.length; j++){  
           if(j % 4 == 0 ){ //Input: vertex weight vertex weight: 1 1 2 3
            int vert = Integer.parseInt(String.valueOf(line[j]));
            int w = Integer.parseInt(String.valueOf(line[j+2]));
            v[i].adjacencies = new Edge[] {new Edge(v[vert], w)};
        }
        }
    }

与此相反:

v0.adjacencies = new Edge[]{ new Edge(v[1],  1),
                         new Edge(v[2],  3) };

如何获取用户输入并制作 Edge[],以将其传递给邻接?问题是它可能是 0 条边或很多条边。

任何帮助将不胜感激 谢谢!

【问题讨论】:

  • 你可能想看看 Scanner 类来读取输入,它有一个方法 nextInt 从字符串中读取数字。

标签: java arrays object input variable-assignment


【解决方案1】:

在您的情况下,您正在为 j 的每次迭代分配 v[i].adjacencies... 所以这意味着它 line.length = 8,然后 v[i].adjacencies 被分配了 2 次。我不认为那是你的意图。

    for (int j = 0; j < line.length; j++){  
       if(j % 4 == 0 ){ //Input: vertex weight vertex weight: 1 1 2 3
        int vert = Integer.parseInt(String.valueOf(line[j]));
        int w = Integer.parseInt(String.valueOf(line[j+2]));
        v[i].adjacencies = new Edge[] {new Edge(v[vert], w)};
    }

您可以像这样更改您的代码...

    Edge[] edges = new Edge[line.length/4];  
    for (int j = 0; j < line.length; j++){           
       if(j % 4 == 0 ){ //Input: vertex weight vertex weight: 1 1 2 3
        int vert = Integer.parseInt(String.valueOf(line[j]));
        int w = Integer.parseInt(String.valueOf(line[j+2]));
        edges[j/4] =  {new Edge(v[vert], w)};
    }
   v[i].adjacencies = edges;

它可能不是确切的代码,但你必须在循环之外分配。

【讨论】:

  • 效果很好!只有一件事是 Edge[] 边缘初始化应该是: Edge[] edges = new Edge[(line.length+1)/4];非常感谢!
猜你喜欢
  • 2018-10-26
  • 2016-01-17
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 2019-02-12
  • 2017-08-04
  • 2011-04-16
  • 2012-03-07
相关资源
最近更新 更多