1

嗨,我是 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();
    }
}

我的问题是,如果我想添加千人的数据,最简单有效的方法是什么

4

1 回答 1

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 个不同元素的 main 方法:

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使测试套件更加不同。

于 2013-10-18T11:13:58.697 回答