为什么我的邻接表包含空白 space? (Java)
Why does my adjacency list contain a blank space? (Java)
我的代码从给定的文本文件创建并打印邻接列表,但是当打印出来时,无论我使用哪个输入文件,它都会在第二个节点之后留下空白 space。这是代码的主要部分:
class Neighbor {
public int vertexNum;
public Neighbor next;
public Neighbor(int vnum, Neighbor nbr) {
this.vertexNum = vnum;
next = nbr;
}
}
class Vertex {
String name;
Neighbor adjList;
Vertex(String name, Neighbor neighbors) {
this.name = name;
this.adjList = neighbors;
}
}
public class Graph
{
static Vertex[] adjLists;
public Graph(String file) throws FileNotFoundException
{
Scanner fileScanner = new Scanner(new File(file));
fileScanner.useDelimiter("[^A-Za-z0-9]");
ArrayList<String> words = new ArrayList<String>();
while (fileScanner.hasNext())
{
String nextWord = fileScanner.next();
if (!words.contains(nextWord))
{
words.add(nextWord);
}
}
adjLists = new Vertex[words.size()];
// read vertices
for (int v=0; v < adjLists.length; v++)
{
adjLists[v] = new Vertex(words.get(v), null);
}
// read edges
Scanner sc = new Scanner(new File(file));
while (sc.hasNext())
{
// read vertex names and translate to vertex numbers
int v1 = indexForName(sc.next());
int v2 = indexForName(sc.next());
// add v2 to front of v1's adjacency list and
// add v1 to front of v2's adjacency list
adjLists[v1].adjList = new Neighbor(v2, adjLists[v1].adjList);
// Doesn't add the node twice if it's connected to itself
if(v1!=v2) {
adjLists[v2].adjList = new Neighbor(v1, adjLists[v2].adjList);
}
}
}
int indexForName(String name)
{
for (int v=0; v < adjLists.length; v++) {
if (adjLists[v].name.equals(name)) {
return v;
}
}
return -1;
}
public void print()
{
System.out.println();
for (int v=0; v < adjLists.length; v++) {
System.out.print(adjLists[v].name);
for (Neighbor nbr=adjLists[v].adjList; nbr != null;nbr=nbr.next) {
System.out.print(" --> " + adjLists[nbr.vertexNum].name);
}
System.out.println("\n");
}
}
这是示例输出:
Sara --> Ajay --> Sam
Sam --> Mira --> Sean --> Sara
Ajay --> Sara
Sean --> Sam
etc.
为什么 Sam 和 Ajay 之间存在这种差距?无论列表有多长,或者我使用什么文件,第 2 个和第 3 个节点之间始终存在间隙,并且当对列表进行 运行 分析时它会产生影响,因为它本质上是读取一个空行并且将其计为一个节点。这不会发生在列表中的其他任何地方,就在第二个节点之后。
我的输入数据是这样的
Sara Sam
Sara Ajay
Sam Sean
Sam Mira
Mira Jane
etc.
调用打印:
Scanner br = new Scanner(System.in);
System.out.print("Enter graph input file name: ");
String file = br.nextLine();
Graph graph = new Graph(file);
graph.print();
如果调试代码,您会发现单词 ArrayList 的第三个元素是一个空字符串。这是由于文件的读取方式以及您的输入的处理方式。
注释 fileScanner.useDelimiter 行可提供正确的输出。否则你可以只添加以下条件:
if (!nextWord.isEmpty() && !words.contains(nextWord)) {
words.add(nextWord);
}
应该没问题。
我的代码从给定的文本文件创建并打印邻接列表,但是当打印出来时,无论我使用哪个输入文件,它都会在第二个节点之后留下空白 space。这是代码的主要部分:
class Neighbor {
public int vertexNum;
public Neighbor next;
public Neighbor(int vnum, Neighbor nbr) {
this.vertexNum = vnum;
next = nbr;
}
}
class Vertex {
String name;
Neighbor adjList;
Vertex(String name, Neighbor neighbors) {
this.name = name;
this.adjList = neighbors;
}
}
public class Graph
{
static Vertex[] adjLists;
public Graph(String file) throws FileNotFoundException
{
Scanner fileScanner = new Scanner(new File(file));
fileScanner.useDelimiter("[^A-Za-z0-9]");
ArrayList<String> words = new ArrayList<String>();
while (fileScanner.hasNext())
{
String nextWord = fileScanner.next();
if (!words.contains(nextWord))
{
words.add(nextWord);
}
}
adjLists = new Vertex[words.size()];
// read vertices
for (int v=0; v < adjLists.length; v++)
{
adjLists[v] = new Vertex(words.get(v), null);
}
// read edges
Scanner sc = new Scanner(new File(file));
while (sc.hasNext())
{
// read vertex names and translate to vertex numbers
int v1 = indexForName(sc.next());
int v2 = indexForName(sc.next());
// add v2 to front of v1's adjacency list and
// add v1 to front of v2's adjacency list
adjLists[v1].adjList = new Neighbor(v2, adjLists[v1].adjList);
// Doesn't add the node twice if it's connected to itself
if(v1!=v2) {
adjLists[v2].adjList = new Neighbor(v1, adjLists[v2].adjList);
}
}
}
int indexForName(String name)
{
for (int v=0; v < adjLists.length; v++) {
if (adjLists[v].name.equals(name)) {
return v;
}
}
return -1;
}
public void print()
{
System.out.println();
for (int v=0; v < adjLists.length; v++) {
System.out.print(adjLists[v].name);
for (Neighbor nbr=adjLists[v].adjList; nbr != null;nbr=nbr.next) {
System.out.print(" --> " + adjLists[nbr.vertexNum].name);
}
System.out.println("\n");
}
}
这是示例输出:
Sara --> Ajay --> Sam
Sam --> Mira --> Sean --> Sara
Ajay --> Sara
Sean --> Sam
etc.
为什么 Sam 和 Ajay 之间存在这种差距?无论列表有多长,或者我使用什么文件,第 2 个和第 3 个节点之间始终存在间隙,并且当对列表进行 运行 分析时它会产生影响,因为它本质上是读取一个空行并且将其计为一个节点。这不会发生在列表中的其他任何地方,就在第二个节点之后。
我的输入数据是这样的
Sara Sam
Sara Ajay
Sam Sean
Sam Mira
Mira Jane
etc.
调用打印:
Scanner br = new Scanner(System.in);
System.out.print("Enter graph input file name: ");
String file = br.nextLine();
Graph graph = new Graph(file);
graph.print();
如果调试代码,您会发现单词 ArrayList 的第三个元素是一个空字符串。这是由于文件的读取方式以及您的输入的处理方式。
注释 fileScanner.useDelimiter 行可提供正确的输出。否则你可以只添加以下条件:
if (!nextWord.isEmpty() && !words.contains(nextWord)) {
words.add(nextWord);
}
应该没问题。