Java Floyd Warshall应解决实施问题
我在为自己的项目实现Floyd Warshall算法时遇到了困难,我正在努力解决这个问题。我有一个测试数据集,但当我在创建Java Floyd Warshall应解决实施问题,java,algorithm,floyd-warshall,Java,Algorithm,Floyd Warshall,我在为自己的项目实现Floyd Warshall算法时遇到了困难,我正在努力解决这个问题。我有一个测试数据集,但当我在创建ShortestPath后打印它时,我只得到一个null和一个内存地址。不确定这个算法从这里到哪里去。非常感谢您的帮助 public static void main(String[] args) { int x = Integer.MAX_VALUE; int[][] adj = {{ 0, 3, 8, x, 4 },
ShortestPath
后打印它时,我只得到一个null
和一个内存地址。不确定这个算法从这里到哪里去。非常感谢您的帮助
public static void main(String[] args) {
int x = Integer.MAX_VALUE;
int[][] adj = {{ 0, 3, 8, x, 4 },
{ x, 0, x, 1, 7 },
{ x, 4, 0, x, x },
{ 2, x, 5, 0, x },
{ x, x, x, 6, 0 }};
ShortestPath sp = new ShortestPath(adj);
System.out.println(sp);
}
public class ShortestPath {
private int[][] adj;
private int[][] spTable;
private int n;
public static void copy(int[][] a, int[][] b) {
for (int i=0; i < a.length; i++)
for (int j = 0; j < a[0].length; j++)
a[i][j] = b[i][j];
}
public ShortestPath(int[][] adj) {
n = adj.length;
this.spTable = new int[n][n];
copy(this.spTable, adj);
for(int k = 0; k < n; k++) {
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if (spTable[i][k] + spTable[k][j] < spTable[i][j]) {
spTable[i][j] = spTable[i][k] + spTable[k][j];
adj[i][j] = adj[k][j];
}
}
}
}
}
@Override
public String toString() {
return adj + "\n\n" + spTable + "";
}
publicstaticvoidmain(字符串[]args){
int x=整数最大值;
int[]adj={{0,3,8,x,4},
{x,0,x,1,7},
{x,4,0,x,x},
{2,x,5,0,x},
{x,x,x,6,0};
最短路径sp=新的最短路径(adj);
系统输出打印项次(sp);
}
公共类最短路径{
私有int[][]adj;
私有int[][]表;
私人int n;
公共静态无效副本(int[]a、int[]b){
for(int i=0;i
您在此处传递的参数adj
正在隐藏您的adj
类成员-您从未给该类成员赋值。一个简单的解决方法是将以下代码行放在上述构造函数的任意位置:
this.adj = adj;
更多信息,请参阅
另一个问题是:
return adj + "\n\n" + spTable + "";
您不能仅仅通过将数组添加到字符串来打印数组中的值,这将只打印地址
需要双for循环来打印数组中的值。有关详细信息,请参阅
return adj + "\n\n" + spTable + "";