我正在嘗試練習 LinkedList。簡單的代碼,用戶輸入 x 個整數,程序將它們輸出到屏幕上。當我輸入 LinkedList 的長度時,出現錯誤:“構造函數 LinkedList(int) 未定義?!?當我刪除該號碼時,代碼會執行,但屏幕上不會顯示任何內容。package practiceProject;import java.util.*;public class PracticeProject{ public static void main(String[] args) { // TODO Auto-generated method stub List<Integer> list1 = new LinkedList<Integer>(); Scanner userInput = new Scanner(System.in); for (int i = 0; i < list1.size(); i++) { System.out.println("Please enter a number"); list1.add(userInput.nextInt()); } for (Integer x: list1) System.out.print(x + " "); userInput.close(); }}如何請求用戶輸入并使用 LinkedList 將其打印在屏幕上?謝謝!
1 回答

慕虎7371278
TA貢獻1802條經驗 獲得超4個贊
LinkedListint在其定義的任何構造函數中都不接受 as參數。
您將需要定義輸入的數量,而不管 的大小LinkedList,也許需要使用單獨的變量。
int numInputs = 5;
List<Integer> list1 = new LinkedList<Integer>();
...
for (int i = 0; i < numInputs; i++) {
...
}
添加回答
舉報
0/150
提交
取消