Java向量错误输出
Java Vector wrong output
我的问题是,当我在 Vector 中添加例如 3 个输入时,当我显示结果时,我只找到最后一个输出。
XMLTable, Table, 列 = 类
cols :包含问题的向量
代码:
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
XMLTable xt= new XMLTable();
Table table = new Table();
Column col = new Column();
Vector<String> txtF = new Vector<String>();
Vector<String> comboF = new Vector<String>();
Vector<Column> cols = new Vector<Column>();
Component[] comp = columns.getComponents();
for(Component c : comp){
if (c instanceof JTextField){
txtF.add(((JTextField) c).getText());
//col.setName(((JTextField) c).getText());
}
else if(c instanceof JComboBox){
comboF.add(((JComboBox<String>) c).getSelectedItem().toString());
//col.setType(((JComboBox<String>) c).getSelectedItem().toString());
}
}
for(int i=0; i<txtF.size();i++){
col.setName(txtF.get(i));
//System.out.println("NAMEE: "+txtF.get(i));
col.setType(comboF.get(i));
//System.out.println("Type: "+comboF.get(i));
cols.add(col);
}
for(int i=0;i<cols.size();i++){
System.out.println("Column : "+i);
System.out.println("name : "+cols.elementAt(i).getName());
System.out.println("type : "+cols.elementAt(i).getType());
}
输入:
name : a Type : String
name : b Type : Numeric
name : c Type : String
输出:
Column : 0
name : c
type : String
Column : 1
name : c
type : String
Column : 2
name : c
type : String
您一遍又一遍地向 cols
添加相同的对象,因为 col
始终是相同的对象。请记住,您通过引用处理对象(即使 Java is always pass-by-value,如果您将对象传递给方法,您实际上传递的是对象引用,而不是对象本身)。要解决此问题,请删除第五行中 Column col = ...
的声明并将第二行 for loop
更改为:
[...]
for(int i=0; i<txtF.size();i++){
Column col = new Column(); // Declaration moved to here
col.setName(txtF.get(i));
//System.out.println("NAMEE: "+txtF.get(i));
col.setType(comboF.get(i));
//System.out.println("Type: "+comboF.get(i));
cols.add(col);
}
[...]
这应该可以解决您的问题。
我的问题是,当我在 Vector 中添加例如 3 个输入时,当我显示结果时,我只找到最后一个输出。
XMLTable, Table, 列 = 类
cols :包含问题的向量
代码:
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
XMLTable xt= new XMLTable();
Table table = new Table();
Column col = new Column();
Vector<String> txtF = new Vector<String>();
Vector<String> comboF = new Vector<String>();
Vector<Column> cols = new Vector<Column>();
Component[] comp = columns.getComponents();
for(Component c : comp){
if (c instanceof JTextField){
txtF.add(((JTextField) c).getText());
//col.setName(((JTextField) c).getText());
}
else if(c instanceof JComboBox){
comboF.add(((JComboBox<String>) c).getSelectedItem().toString());
//col.setType(((JComboBox<String>) c).getSelectedItem().toString());
}
}
for(int i=0; i<txtF.size();i++){
col.setName(txtF.get(i));
//System.out.println("NAMEE: "+txtF.get(i));
col.setType(comboF.get(i));
//System.out.println("Type: "+comboF.get(i));
cols.add(col);
}
for(int i=0;i<cols.size();i++){
System.out.println("Column : "+i);
System.out.println("name : "+cols.elementAt(i).getName());
System.out.println("type : "+cols.elementAt(i).getType());
}
输入:
name : a Type : String
name : b Type : Numeric
name : c Type : String
输出:
Column : 0
name : c
type : String
Column : 1
name : c
type : String
Column : 2
name : c
type : String
您一遍又一遍地向 cols
添加相同的对象,因为 col
始终是相同的对象。请记住,您通过引用处理对象(即使 Java is always pass-by-value,如果您将对象传递给方法,您实际上传递的是对象引用,而不是对象本身)。要解决此问题,请删除第五行中 Column col = ...
的声明并将第二行 for loop
更改为:
[...]
for(int i=0; i<txtF.size();i++){
Column col = new Column(); // Declaration moved to here
col.setName(txtF.get(i));
//System.out.println("NAMEE: "+txtF.get(i));
col.setType(comboF.get(i));
//System.out.println("Type: "+comboF.get(i));
cols.add(col);
}
[...]
这应该可以解决您的问题。