简体   繁体   English

添加到双向链接列表

[英]Add to Doubly Linked List

private class Node{
int num;
Node next;
Node prev;
Node (int num){
this.num=num;
this.next=null;
this.prev=null;
}
}

Scanner sc = new Scanner(System.in);
System.out.println("Enter file name");
String fileName = sc.next();
Scanner s = new Scanner (new File(fileName));
while(s.hasNextLine()){
    //No idea           
}

File example: 文件示例:

 //Skip this 3 6 2
 a 3 b 8
 2 3 4

I want to be able to go through each line of the file, skip everything in the comments, and put just the first two numbers of each line into a doubly linked list. 我希望能够遍历文件的每一行,跳过注释中的所有内容,并将每行的前两个数字仅放入双向链接列表中。 I have been trying for awhile but I am new to doubly linked lists and I can't figure this out. 我已经尝试了一段时间,但我是双重链接列表的新手,但我无法弄清楚。 Also I want to be able to do this without using ArrayList. 我也希望不用ArrayList就能做到这一点。

Take each line as a string eg: String temp = sc.nextLine(); 将每行作为一个字符串,例如:String temp = sc.nextLine();

Check it the first char is '/', if it isn't then break the String into a char[] eg: char [] numbers = temp.toCharArray(); 检查第一个字符是否为'/',如果不是,则将字符串拆分为char []例如:char [] Numbers = temp.toCharArray(); Then you have access to which ever parts of the string you want. 然后,您可以访问所需的字符串部分。 Sorry about the formatting I'm still in bed answering on my phone, no formatting possible ;-) Actually you could just split the String into a char[] first up, up to you. 很抱歉,我仍在床上接电话,无法格式化;-)实际上,您可以先将String拆分为char [],由您决定。 There are always multiple ways of doing this. 总是有多种方法可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM