Tuple tuple = new Tuple();
//將數據庫中的數據保存到tuple結構體類型的數組鏈表
while(rs.next()) {
tuple.setValue(rs.getNString(1));
array.add(tuple);
}
for(Tuple t: array) {
System.out.println(t.getValue());
}結果: 1 1 1 1 1 1 1 1 1 1
解決方法:將 Tuple tuple = new Tuple())我們自定義的結構體對象放到while()裏面
rs = ps.executeQuery();
//將數據庫中的數據保存到tuple結構體類型的數組鏈表
while(rs.next()) {Tuple tuple = new Tuple();
tuple.setValue(rs.getNString(1));
array.add(tuple);
}
for(Tuple t: array) {
System.out.println(t.getValue());
}結果: 1 2 3 4 5 6 7 8 9
因爲若將 Tuple對象在while外面創建,則添加到list中的Tuple對象永遠是那一個,所以,當我們應該將Tuple創建在while裏面,每循環一次就應該創建一次