我想知道为什么我的矢量会重复?我知道我可以使用ArrayList,但这是我的黑莓应用程序的测试,目前只使用向量。这是我继续得到的结果:
ID:3 and Name:Catty and Price:100 and Msg:Catty is a preety girl
ID:3 and Name:Catty and Price:100 and Msg:Catty is a preety girl
ID:3 and Name:Catty and Price:100 and Msg:Catty is a preety girl
以下是代码 testapp.java
package testing;
import java.util.Vector;
public class testapp {
private static Vector<String> listElements= new Vector<String>();
private static Vector<String> listName= new Vector<String>();
private static Vector<Integer> listPrice= new Vector<Integer>();
private static Vector<String> listDate= new Vector<String>();
private static Vector<String> listAbstract= new Vector<String>();
private static Vector<rec> a = new Vector<rec>();
static rec record = new rec();
public static void main(String[] args){
vectorBuilder();
recordBuilder();
showElements();
}
public static void recordBuilder(){
try{
for(int i=0;i<listElements.size();i++){
record.setId((String) listElements.elementAt(i));
record.setName((String) listName.elementAt(i));
record.setDate((String) listDate.elementAt(i));
record.setPrice(((Integer) listPrice.elementAt(i)).intValue());
record.setAbstract((String) listAbstract.elementAt(i));
a.addElement(record);
}
}catch (Exception e){
System.out.println("Record Builder Error: " + e.getMessage());
}
}
public static void vectorBuilder(){
//Ben
listElements.addElement("1");
listName.addElement("Ben");
listPrice.addElement(502);
listDate.addElement("2012-05-12");
listAbstract.addElement("Ben is a Good Boy");
//Kesha
listElements.addElement("2");
listName.addElement("Kesha");
listPrice.addElement(367);
listDate.addElement("2012-02-24");
listAbstract.addElement("Kesha Works hard in her school work");
//Catty
listElements.addElement("3");
listName.addElement("Catty");
listPrice.addElement(100);
listDate.addElement("2012-01-04");
listAbstract.addElement("Catty is a preety girl");
}
public static void showElements(){
for(int i=0;i<a.size();i++)
{
rec r = (rec) a.elementAt(i);
System.out.println("ID:"+r.getId()+" and Name:"+r.getName()+" and Price:"+r.getPrice()+" and Msg:"+r.getAbstract());
}
}
}
这是 rec.java
package testing;
public class rec {
String name;
String id;
String date;
String strAbstract;
int price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getAbstract() {
return strAbstract;
}
public void setAbstract(String strAbstract) {
this.strAbstract = strAbstract;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
答案 0 :(得分:6)
您需要在调用recordBuilder
时创建新的rec。如果您不创建新record
,则会更新相同的实例,这就是您获得重复项的原因。
for(int i=0;i<listElements.size();i++){
rec record = new rec();
record.setId((String) listElements.elementAt(i));
record.setName((String) listName.elementAt(i));
record.setDate((String) listDate.elementAt(i));
record.setPrice(((Integer) listPrice.elementAt(i)).intValue());
record.setAbstract((String) listAbstract.elementAt(i));
a.addElement(record);
}
注意:Java命名约定建议类名以大写字母开头。 Record
可能比rec
好名。