Java:在 Eclipse 中运行简单的图形表示代码时出错

问题描述 投票:0回答:1

我正在尝试运行一个简单的 Java 程序,该程序在 Eclipse 中使用邻接列表表示图形,但遇到错误。这是我的代码:

package com.traversal.dsa;

public class GraphRepresentation {

    public static void main(String[] args) {
        int [][] graph = {
                {0,1,0,0,1},
                {1,0,1,0,1},
                {0,1,0,1,0},
                {0,0,1,0,1},
                {1,1,0,1,0}
        };
        
        Graph g = new Graph(5); // no. of vertex 
        
        g.addEdge(0,1);
        g.addEdge(2,3);
        g.addEdge(1,2);
        g.addEdge(3,4);
        g.addEdge(0,4);
        g.addEdge(1,4);
        
        g.printadjList();
    }

}
package com.traversal.dsa;

import java.util.ArrayList;

public class Graph {
    ArrayList<ArrayList<Integer>> adjList = new ArrayList<>();
    
    Graph(int v){
        for(int i = 0; i < v; i++) {
            adjList.add(new ArrayList<Integer>());
        }
    }
    
    public void addEdge(int x, int y) {
        adjList.get(x).add(y);
        adjList.get(y).add(x);
    }
    
    public void printadjList() {
        for(int i = 0;i < adjList.size();i++) {
            System.out.println("Adjacent List vectex : " + i);
            for(int j = 0; j < adjList.get(i).size();j++) {
                System.out.println(" " + adjList.get(i).get(j));
            }
        }
    }
}

我已将代码组织到两个文件中:GraphRepresentation.java 和 Graph.java,这两个文件都位于名为“graph”的包中。但是,当我尝试在 Eclipse 中运行此代码时,遇到了错误。 Eclipse 指出包声明中存在错误,但我似乎无法弄清楚出了什么问题。

有人可以帮我找出问题并提供解决方法的指导吗?任何帮助将不胜感激。

This is the image of the error shown in console

java eclipse compiler-errors package adjacency-list
1个回答
0
投票

您的包名称(“graph”)似乎与源文件中的包声明(“com.traversal.dsa”)不匹配。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.