Java 填充对象数组

Java 填充对象数组,java,arrays,object,populate,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,

我目前正在学习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;
}
}

在我的main中,我创建了一个大小为2的employee类型的对象数组,然后增加数组计数,同时允许用户输入一些基本信息,然后再进行另一个循环以打印输入的所有信息

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());
    }
}
import java.util.Scanner;
公共类employeeArray{
公共静态void p(字符串s){System.out.println(s);}
公共静态void main(字符串参数[]){
扫描仪k=新的扫描仪(System.in);
int-empID,size=0,x=0;
字符串名称;
字符串作业;
员工[]员工=新员工[2];
用于(大小=0;大小<2;大小++)
{
p(“员工”+(人数+1));
p(“请输入员工ID号:”);
empID=k.nextInt();k.nextLine();
p(“请输入您的姓名:”);
empName=k.nextLine();
p(“请输入您的工作角色:”);
job=k.nextLine();
员工[人数]=新员工();
员工[size].setEmpID(empID);
员工[size].setEmpName(empName);
员工[size].setJob(作业);
}

对于(x=0;x当需要动态调整数组大小时,不要麻烦使用数组,除非这是严格必要的(即使在这种情况下,也可以通过
ArrayList.toArray(…)
)进行转换)

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

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

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


以这种方式复制数组并非效率低下,但它很笨拙且容易出错,因此有几个JDK类(如java.util.ArrayList)可以保存可变长度的列表并为您管理难看的内容。(说实话,一般来说,这些类保留一个内部数组,并在需要时将其复制到一个更大的数组中,正如您所希望的那样,因此它们没有更高的效率。但它们简化了编码并减少了错误,这一点很重要。)

首先,将类名改为
Employee
,而不是
Employee
。遵守java命名约定。 现在您应该利用java实用程序类,例如
ArrayList
ArrayList
实现动态调整数组大小,这样用户就可以在其中插入尽可能多的数据,而不用担心内部
数组的当前大小,因为
ArrayList
是用来获取c的但是在使用
ArrayList
存储
Employee
对象之前,您需要重写
Employee
中的
equals
方法,因为
ArrayList
内部使用此方法来查找某些方法中是否存在类似的对象。
equals
方法在中如下所示ode>员工
类别:

@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());
}
List List=new ArrayList();
用于(大小=0;大小<2;大小++)
{
p(“员工”+(人数+1));
p(“请输入员工ID号:”);
empID=k.nextInt();k.nextLine();
p(“请输入您的姓名:”);
empName=k.nextLine();
p(“请输入您的工作角色:”);
job=k.nextLine();
员工=新员工();
employee.setEmpID(empID);
employee.setEmpName(empName);
employee.setJob(job);
列表。添加(员工);
}
对于(int i=0;i
根据您的评论,您实际上并不想要动态数组,而是想要一个固定大小为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;
}
int[]intArray=newint[20];
整数[0]=1
intArray[1]=2
intArray[2]=3
intArray[3]=4
整数[4]=5
整数[5]=6
intArray[6]=7
intArray[7]=8
intArray[8]=9
整数[9]=10
//现在填充剩余的插槽。
对于(int i=10;i
但是,您将负责跟踪阵列中的项目和非项目,并且您不能向阵列中添加超过20个项目

编辑: 您编辑了您的帖子。使用上述代码,您可以随心所欲。如果您确实希望在不使用ArrayList的情况下动态增加数组,则可以使用System#arrayCopy

,int,java.lang.Object,int,int)


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

我无法理解您的问题。您能用其他方式解释一下吗?最好是
List lstEmployess=new ArrayList()
。更多信息:谢谢你的回复。我有点理解你的意思。唯一的问题是,对象数组是学习的主题,所以我现在正努力想弄清楚,我实际上想把数组的大小硬编码为20,并且已经预先输入了10个,然后从用户输入中再添加10个,就像Jack说的,如果你想要一个dy的话Java中的namic数组使用ArrayList。简单数组的大小是固定的。在您的示例中,您实例化了一个大小为2的数组。如果要向其中添加两个以上的项,则必须分配一个大小为3的新数组。然后复制旧数组的内容