嗨我是java的新手,我现在正在向量中添加对象我正在添加两个对象但是如果我想通过文件或数据库输入千人的数据我现在怎么办呢有三节课。和代码如下: -
public class Customer {
private int custid;
private String custname;
private float custsalary;
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("Customer Id :=" + custid+"\n");
buffer.append("Customer Name :=" + custname+"\n");
buffer.append("Customer Salary :=" + custsalary+"\n");
return buffer.toString();
}
public int getCustid() {
return custid;
}
public void setCustid(int custid) {
this.custid = custid;
}
public String getCustname() {
return custname;
}
public void setCustname(String custname) {
this.custname = custname;
}
public float getCustsalary() {
return custsalary;
}
public void setCustsalary(float custsalary) {
this.custsalary = custsalary;
}
}
和第二堂课是
package com.test.collection.list.vector.custom;
import java.util.Enumeration;
import java.util.Vector;
public class CustomerVector {
public void custDetails(){
Customer c1Obj = new Customer();
c1Obj.setCustid(123);
c1Obj.setCustname("Kb");
c1Obj.setCustsalary(1000.34f);
Customer c2Obj = new Customer();
c2Obj.setCustid(456);
c2Obj.setCustname("nv");
c2Obj.setCustsalary(2000.34f);
Vector<Customer> v = new Vector<Customer>();
v.addElement(c1Obj);
v.addElement(c2Obj);
Enumeration<Customer> custEnum = v.elements();
while (custEnum.hasMoreElements()) {
Customer customer = custEnum.nextElement();
System.out.println(customer);
}
}
}
这是我的客户代码
package com.test.collection.list.vector.custom;
public class ClientCode {
public static void main(String[] args) {
CustomerVector custVectObj = new CustomerVector();
custVectObj.custDetails();
}
}
我的问题是,如果我想添加千人的数据,这将是最简单有效的方法
答案 0 :(得分:1)
使用以下命令更改CustomVector:
public class CustomerVector {
Vector<Customer> v = null;
public CustomVector(){
v = new Vector<Customer>();
}
public void createCustomer(int id, String name, float salary){
Customer c1Obj = new Customer();
c1Obj.setCustid(id);
c1Obj.setCustname(name);
c1Obj.setCustsalary(salary);
v.addElement(c1Obj);
}
public void custDetails(){
Enumeration<Customer> custEnum = v.elements();
while (custEnum.hasMoreElements()) {
Customer customer = custEnum.nextElement();
System.out.println(customer);
}
}
}
并在您的主要内容中添加矢量
package com.test.collection.list.vector.custom;
public class ClientCode {
public static void main(String[] args) {
CustomerVector custVectObj = new CustomerVector();
// Add Customers
custVectObj.createCustomer(1,"joan",10.03f);
custVectObj.custDetails();
}
}
生成1000个不同元素的自动主方法:
package com.test.collection.list.vector.custom;
public class ClientCode {
public static void main(String[] args) {
CustomerVector custVectObj = new CustomerVector();
// Add Customers
for(int i = 0; i < 1000; i++){
custVectObj.createCustomer(i,"name"+i,i);
}
custVectObj.custDetails();
}
}
您可以使用Random class
来使测试套件更加不同。