Java中怎样将逐个输入的数据保存到List 或者Hashtable中,表是多行多列的,如图所示:
2个回答
展开全部
数据保存在文本文件中如:
CODE NUM
0011 10
0024 5
0035 2
读取时:
Scanner scan = new Scanner(new FileInputStream("文件名"));
String[] columnNames = scan.nextLine().split(" ");
String line = null;
List<Map<String, String>> data = new LinkedList<Map<String,String>>();
while((line = scan.nextLine()) != null) {
String[] values = line.split(" ");
Map<String, String> map = new HashMap<String, String>();
for(int i = 0; i < columnNames.length(); i ++) {
map.put(columnNames[i], values[i]);
}
data.add(map);
}
CODE NUM
0011 10
0024 5
0035 2
读取时:
Scanner scan = new Scanner(new FileInputStream("文件名"));
String[] columnNames = scan.nextLine().split(" ");
String line = null;
List<Map<String, String>> data = new LinkedList<Map<String,String>>();
while((line = scan.nextLine()) != null) {
String[] values = line.split(" ");
Map<String, String> map = new HashMap<String, String>();
for(int i = 0; i < columnNames.length(); i ++) {
map.put(columnNames[i], values[i]);
}
data.add(map);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询