填充对象数组

时间:2013-03-20 20:29:05

标签: java arrays object populate

我目前正在学习Java并且不太确定如何做到这一点。

目前我有一个员工班

public class employee {

private int empID;
private String empName;
private String job;

//constructors;

public employee(){
    this.empID = 0;
    this.empName = "";
    this.job = "";
}
public employee(int empID, String empName, String job){
    this.empID = empID;
    this.empName = empName;
    this. job = job;
}

//gets;

public int getEmpID(){
    return this.empID;
}
public String getEmpName(){
    return this.empName;
}
public String getJob(){
    return this.job;
}

//sets;

public void setEmpID(int empID){
    this.empID = empID;
}
public void setEmpName(String empName){
    this.empName = empName;
}
public void setJob(String job){
    this.job = job;
}

}

在我的主要内容中,我创建了一个类型为employee的对象数组,其大小为2,然后只需增加数组计数,同时让用户输入一些基本信息,然后再使用另一个循环来打印输入的所有信息。

import java.util.Scanner;
public class employeeArray {

public static void p(String s) {System.out.println(s);}

public static void main(String args[]){
    Scanner k = new Scanner(System.in);
    int empID, size=0, x=0;
    String empName;
    String job;
    employee [] employees = new employee[2];

    for(size = 0; size < 2; size++)
    {
        p("Employee "+(size+1));
        p("Please enter employee ID number: ");
        empID = k.nextInt(); k.nextLine();
        p("Please enter your name: ");
        empName = k.nextLine();
        p("Please enter your job role: ");
        job = k.nextLine();

        employees[size] = new employee();

        employees[size].setEmpID(empID);
        employees[size].setEmpName(empName);
        employees[size].setJob(job);


    }

    for(x=0; x<2;x++){
        p("Hello employee: "+employees[x].getEmpName()+" your job role is "+employees[x].getJob()+ 
                " your log in ID is 0800"+employees[x].getEmpID());
    }
}

}

我理解如何通过输入填充对象的每个实例但是我想要将大小从2增加到20但是已经预先输入了前10'然后我可以显示我当前拥有的10个但仍然允许再输入10个。

PS。对不起我的冗长问题,代码块和缺乏/缺乏术语。我是新来的。

4 个答案:

答案 0 :(得分:7)

当你需要动态调整它们时,不要费心使用数组,除非它是绝对必要的(即使在这种情况下你只能转换为ArrayList.toArray(..))。

使用ArrayList<Employee>代替Employee[]。您不必担心容量,您不必担心删除和移动元素。它还支持通用容器。

作为旁注:Java的命名约定建议对类使用Capitalized名称。

答案 1 :(得分:0)

Java数组(带有一个小的“a” - 一个[]对象)在创建时是固定大小的。因此,如果使用10个元素创建数组,它将始终具有10个元素(尽管其中一些元素可能没有非空值)。如果你需要增加阵列的大小超出它当前分配的大小,那么祝你好运! ....呃,我的意思是你必须分配一个新的,更大的数组,然后将较小数组的内容复制到其中。

以这种方式复制数组并不是那么低效,但是它很笨拙且容易出错,所以有几个JDK类(例如java.util.ArrayList)可以容纳一个可变长度列表并管理丑陋的东西给你。 (说实话,一般来说,这些类保留一个内部数组,并在需要时将其复制到更大的数组,就像你一样,所以它们不再有效。但它们简化了编码并减少了错误,这就是重点。 )

答案 2 :(得分:0)

首先将班级名称更改为Employee而不是employee。坚持使用java命名约定。 现在您应该利用java实用程序类。例如ArrayList<Employee>ArrayList实现动态调整数组大小,允许用户在其中插入尽可能多的数据,而不必担心内部array的当前大小,因为ArrayList可以处理它。但在使用ArrayList存储Employee个对象之前,您需要覆盖equals中的Employee方法,因为ArrayList在内部使用此方法来查找外观的存在某些方法中的类似对象。 equals方法在Employee类:

中如下所示
@Override
public boolean equals(Object anObject) 
{
    if (this == anObject) 
    {
        return true;
    }
    if (anObject instanceof Employee) 
    {
        Employee anotherEmployee = (Employee)anObject;
        if ((this.toString()).equals(anotherEmployee.toString()))
        {
            return true;
        }
        return false;
    }
    return false;
}
@Override
public String toString()//Override it so that it could be easier to compare two objects of Employee using toString()
{
    return empID+","+empName+","+job;
}

现在,您可以通过以下方式使用ArrayList

List<Employee> list = new ArrayList<Employee>();
for(size = 0; size < 2; size++)
    {
        p("Employee "+(size+1));
        p("Please enter employee ID number: ");
        empID = k.nextInt(); k.nextLine();
        p("Please enter your name: ");
        empName = k.nextLine();
        p("Please enter your job role: ");
        job = k.nextLine();

        Employee employee = new Employee();

        employee.setEmpID(empID);
        employee.setEmpName(empName);
        employee.setJob(job);
        list.add(employee);
    }

   for(int i = 0 ;i < list.size() ; i++){
    Employee employee = list.get(i);
    p("Hello employee: "+employee.getEmpName()+" your job role is "+employee.getJob()+ 
            " your log in ID is 0800"+employee.getEmpID());
}

答案 3 :(得分:0)

根据你的评论,你实际上不需要动态数组,但是输入了一个固定大小为20和10的数组。

示例:

int[] intArray = new int[20];

intArray[0] = 1
intArray[1] = 2
intArray[2] = 3
intArray[3] = 4
intArray[4] = 5
intArray[5] = 6
intArray[6] = 7
intArray[7] = 8
intArray[8] = 9
intArray[9] = 10

//now to fill the rest of the slots.
for(int i = 10; i < intArray.length; i++) {
    intArray[i] = i + 1;
}

然而,你将是那个负责跟踪数组中的内容以及什么不是内容的人,你不能在数组中添加超过20个项目。

编辑: 你编辑了你的帖子。使用上面的代码,你可以做你想要的。如果您真的想在不使用ArrayList的情况下动态增加数组,可以使用System#arrayCopy

http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,int,java.lang.Object,int,int)

它允许您将现有数组复制到具有新大小的新(空)数组。

相关问题