use*_*346 2 java linked-list list
我对 Java 和 StackOverflow 真的很陌生,所以请不要吝啬。我真的很感激一些帮助。先谢谢了。
我觉得这真的很容易,而且我已经尝试过一百万种不同的方法,但它不起作用。
我正在尝试接收一个文本文件并将其存储到一个链表中,并且我正在尝试访问该链表的第三个节点。由于某种原因,我可以访问第一个节点,然后我可以使用 getNext() 命令转到下一个节点,但是当我尝试使用 getNext() 转到第三个节点时,它继续返回第二个节点。所以它不会去第三个节点。我只是错过了一些关键概念吗?如果您需要更多信息,也请告诉我。
被取入的文本文件是:5 ABCDEAB //这是我想要的行 BC BD CD CE DE
这是我的代码的一部分:
public static void main(String[] args) throws IOException{
/**
* Check whether the user types the command correctly
*/
if (args.length != 1)
{
System.out.println("Invalid input");
System.out.println(args.length);
System.exit(1);
}
String filename = args[0];
Scanner input = new Scanner (new File(filename));
LinkedList<String> linkedList= new LinkedList<String>();
while(input.hasNext())
{
linkedList.addToRear(input.nextLine());
}
LinearNode<String> link= linkedList.firstLink;
String temp = " ";
link.getNext();
temp = (String)link.getElement();
String[] numofVerticesArray = temp.split(" ");
int numOfVertices = Integer.parseInt(numofVerticesArray[0]);
int lineNumber = 1;
String [] arrayOfVertices;
LinearNode<String> secondLine = link;
String temp2;
for (int i=0; i <= lineNumber; i++)
{
secondLine = link.getNext();
}
lineNumber = 2;
temp2 = (String)secondLine.getElement();
arrayOfVertices = temp2.split(" ");
int[][] adjMatrix = new int[numOfVertices][numOfVertices];
LinearNode<String> edgeLine = link;
String [] arrayOfEdge;
int rowCount = 0;
int columnCount = 0;
String temp3;
lineNumber = 2;
for (int i=0; i <= lineNumber; i++)
{
edgeLine = link.getNext();
System.out.print((String)edgeLine.getElement());
//When this is printed out, the second node's
//content is printed out, not the third node
}
lineNumber++;
temp3 = (String)edgeLine.getElement();
arrayOfEdge = temp3.split(" ");
Run Code Online (Sandbox Code Playgroud)
您继续从 LinkedList 中请求第二个元素。
edgeLine = link.getNext();
Run Code Online (Sandbox Code Playgroud)
将 LinkedList 链接的第二个元素的值设置为edgeLine,然后循环并执行相同的操作,然后一遍又一遍地重复相同的操作。
尝试做
edgeLine = edgeLine.getNext();
Run Code Online (Sandbox Code Playgroud)
这将继续前进。
| 归档时间: |
|
| 查看次数: |
23378 次 |
| 最近记录: |