0

下面是我的问题

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...? 

代码:

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); 
}
4

2 回答 2

2

这就是你所需要的:

LinkedList al = new LinkedList(); 
于 2013-11-06T05:46:03.933 回答
1

LinkedList 还提供了add方法,这两个类(ArrayList 和 LinkedList)都从List接口覆盖。因此,您只需要按照此处所述更改列表的声明:

List al = new LinkedList(); 

List注意代替的用法,以LinkedList供参考。这允许在不更改大部分代码的情况下更改实施列表。

于 2013-11-06T05:47:10.163 回答