我想用此功能編寫程序: 用戶將輸入他有多少事情。他會輸入這些東西,東西將被添加到列表中。 我做了這個代碼:LinkedList中的list.add(String)對我無法正常工作
public class lists {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
LinkedList<String> list= new LinkedList<String>();
System.out.println("How many things you have?");
int size=input.nextInt();
LinkedList<String> list= new LinkedList<String>();
System.out.println("Enter those things");
for(int c=1;c<=size;c++) list.add(input.nextLine().toString());
System.out.printf("%s",list);
}
}
例如輸出爲5號看起來是這樣的:
[, 1st Inputed, 2nd Inputed,3rd Inputed, 4nd inputed]
我想知道爲什麼在列表中的第一個字符串是空的,它讓我輸入更少的東西,我想要的。感謝您的幫助。
你的程序不會編譯,因爲變量'list'被聲明瞭兩次.. – 2013-03-23 18:16:59