任何人都可以請幫助我的代碼,如何從控制檯讀取多行並將其存儲在數組列表中? 例子,從控制檯我的輸入是:從控制檯讀取多行並將其存儲在Java中的數組列表中?
12 abc place1
13 xyz place2
,我需要在ArrayList中這個數據。
到目前爲止,我嘗試這種代碼:
Scanner scanner = new Scanner(System.in);
ArrayList informationList = new ArrayList<ArrayList>();
String information = "";
int blockSize = 0, count = 1;
System.out.println("Enter block size");
blockSize = scanner.nextInt();
System.out.println("Enter the Information ");
while (scanner.hasNext() && blockSize >= count) {
scanner.useDelimiter("\t");
information = scanner.nextLine();
informationList.add(information);
count++;
}
任何幫助是極大的讚賞。
控制檯的輸入行是字符串和整數的混合
掃描儀掃描=新的掃描儀(System.in); ArrayList informationList = new ArrayList(); String information =「」; nt blockSize = 0,count = 1; System.out.println(「輸入塊大小」); blockSize = scanner.nextInt(); System.out.println(「輸入信息」); (scanner.hasNext()&& blockSize> = count){ \t scanner.useDelimiter(「\ t」); \t information = scanner.nextLine(); \t informationList.add(information); \t count ++; \t} –
user1547054
2012-07-23 22:20:58
ArrayList的元素應該是什麼類型?你只是存儲原始字符串或解析每一行到一個強類型的數據結構?如果是後者,請提供您正在使用的數據對象的代碼。 – 2012-07-23 22:21:14
來自控制檯的輸入行是字符串和整數的混合 – user1547054 2012-07-23 22:25:04