我想知道为什么我的向量会重复?我知道我可以使用 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;
}
}