简体   繁体   English

LinkedList主程序(是否可以要求用户输入数据并以linkedList的方式存储它…?)

[英]LinkedList main program(is it possible to ask user to input the data and stored it in a linkedList way…? )

Below are my question 以下是我的问题

1. In an ArrayList we used below function to ask user to input 20 data, so, how to rewrite this ?
2. Function using the LinkedList method? 
3. Is it possible to ask user to input the data and stored it in a LinkedList way...? 

Code: 码:

public static void main(String[] args) 
{ 
ArrayList al = new ArrayList(20); 
Scanner in = new Scanner(System.in); 
Student st; 
String name, id; 

float cgpa; 
for(int i=0; i<20; i++) 
{ 
System.out.println("Name : "); 
name = in.next(); 
System.out.println("ID : "); 
id = in.next(); 
System.out.println("CGPA : "); 
cgpa = in.nextFloat(); 

st = new Student(name, id, cgpa); 
al.add(i,st); 
}

这就是您所需要的:

LinkedList al = new LinkedList(); 

LinkedList also provides add method, which both the classes(ArrayList and LinkedList) overrides from List interface. LinkedList还提供add方法,这两个类(ArrayList和LinkedList)都将从List接口重写。 So you just need to change the declaration of your list as mentioned here: 因此,您只需要按此处所述更改列表的声明即可:

List al = new LinkedList(); 

Note the usage of List instead of LinkedList for reference. 请注意使用List而不是LinkedList进行参考。 This allows to change the implentation list without changing much of your code. 这样就可以更改授权列表,而无需更改很多代码。

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

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