Java 如何初始化Person类型的数组列表

Java 如何初始化Person类型的数组列表,java,arrays,arraylist,Java,Arrays,Arraylist,我创建了一个容器列表,其中包含Person[]类型的数组。SimplepersonDatabase类执行添加、删除、查找和删除人员。我知道我的问题可能离题了,但我已经花了一个多星期在这个问题上,我似乎没有取得任何进展 我的问题是如何初始化刚刚创建的容器?我希望能够执行SimplePersonDataBase中实现的功能。谢谢你抽出时间。我真的被卡住了。其他实现,例如int类型的数组和String类型的数组,是很容易理解的,但是我不能将这些知识应用到创建这个数组中 谢谢 class List {

我创建了一个容器列表,其中包含Person[]类型的数组。SimplepersonDatabase类执行添加、删除、查找和删除人员。我知道我的问题可能离题了,但我已经花了一个多星期在这个问题上,我似乎没有取得任何进展

我的问题是如何初始化刚刚创建的容器?我希望能够执行SimplePersonDataBase中实现的功能。谢谢你抽出时间。我真的被卡住了。其他实现,例如int类型的数组和String类型的数组,是很容易理解的,但是我不能将这些知识应用到创建这个数组中

谢谢

 class List {  // seperate List.java class
            int size;
            Person[] person;
            int next;

        public List(){
            this.size=100;
            this.person = new Person[size];
            this.next=0;
        }
        }

    public class Person {   // Seperate Person.java class
        public String fn;
        public String ln;
        public Date dob;
        public int id;
    }


    public class SimplePersonDataBase {//Seperate SimplePersonDataBase.java class

        public static void go(){

    Person person[] = new Person[4];
    String[] firstname = { "denis", "cyprian", "ben", "albert" };
    String[] lastname = { "Ayuk", "Bisong", "Agbor", "Arrey" };
    int[] id = {120, 154, 547, 563 };


    for (int i = 0; i < firstname.length; i++) {
        person[i] = new Person();
        person[i].fn= firstname[i];
        person[i].ln= lastname[i];
        person[i].dob= new Date();



    }

    person[3] = new Person();
    person[3].fn= firstname[1];
    person[3].ln= lastname[1];
    person[3].dob= new Date();
    person[3].dob= DateFunctions.makeDate(1990, 10, 17);
        }

        public static boolean add(Person[] person, Person personadd){

        }

        /*
         * This function sorts an array of persons. The sort-criterion is specified by the int
    argument and is analogous to the meaning of the int argument in
    int compareTo(Person, Person, int)
         */
        public static void sort(Person[] a, int opt){

            for(int i=1; i<a.length; i++){

                for(int j = 0; j < i; j++){
                    Person temp = new Person();
                    if(PersonFunctions.compareTo(a[j-1], a[j],opt)> 0){
                        temp = a[j];
                        a[j] = a[j+1];
                        a[j + 1] = temp;

                    }

                }


            }
        }


        public static void swap(Person[] person, int i, int j){

            Person tmp = new Person();
            if ((j>=0 && j<person.length) && (i>=0 && i<person.length))
            tmp = person[j];
            person[j] = person[i];
            person[i] = tmp;

        }

        public static int find(Person[] a, String b){

            int keyIndex = -1;

            for(int i = 0; i<a.length; i++){
                if(a[i].ln==b){
                    keyIndex = i;
                }
            }
            return keyIndex;

        }

        /*
         * Removes the Person with index idx from the list. and returns the removed Person,
    or null if idx is out of range.
    This function is tricky, because It is not allowed to leave a gap in the array. That
    means you have to shift all elements after the removed one.
         */
        public static Person remove (Person[] person, int idx){

            Person removed = new Person();

            if(idx<0 || idx>=person.length){
                removed = null;
            }
            if(person[idx]==null){
                removed=null;
                }

            for (int i = 0; i<person.length; i++){
                if(i==idx){
                    removed = person[i];
                }
            }

            for (int k=idx; k<person.length; k++){
                person[k] = person[k+1];
            }

            return removed;

        }

        public static void display(Person[] p) {
            for (int i = 0; i < p.length; i++)
                if (p[i] == null)
                    System.out.printf(null);
                else
                    TextIO.putf("%s %s %s %s %s\n", i, p[i].id, p[i].fn, p[i].ln, p[i].dob);

            TextIO.putln();

    }
    //  public static void display(Person[] p, int personFormat, int dateFormat){
    //      display(p);
    //      switch(personFormat){
    //          case 1: PersonFunctions.display(p, 1); break;
    //          case 2: PersonFunctions.display(p,2); break;
    //      default: display(p);break;
    //      }
    //  }


        public static void main(String[] args) {


            go();

        }

            TextIO.put("Welcome to the SimplePersonDatabase.\n");
            TextIO.putln();

            int option;
            do{

                TextIO.put("available options:\n1) list\n2) add\n3) remove\n4) sort\n5) find\n6) settings\n0) quit\nyour choice:");
                option = TextIO.getInt();

                switch(option){
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    TextIO.putln("sort by:\n1) Firstname\n2) Birth\nall other values: lastname");
                    switch(TextIO.getInt()){
                    case 1:
                        break;
                    case 2:
                        break;
                    default :
                        break;
                    }
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 0:
                    TextIO.put("Thank you for using the SimplePersonDatabase.");
                    break;
                case 99:
                    break;
                default :
                    TextIO.put("illegal option.");
                    break;
                }

            }while(option !=0);

    }
类列表{//separate List.java类
整数大小;
人[]人;
int-next;
公开名单(){
这个。大小=100;
this.person=新人[尺码];
这个.next=0;
}
}
公共类Person{//separate Person.java类
公共字符串fn;
公共字符串ln;
公布日期dob;
公共int id;
}
公共类SimplePersonDataBase{//separate SimplePersonDataBase.java类
公共静态void go(){
人员[]=新人员[4];
String[]firstname={“丹尼斯”、“塞浦路斯人”、“本”、“阿尔伯特”};
字符串[]lastname={“Ayuk”、“Bisong”、“Agbor”、“Arrey”};
int[]id={120154547563};
for(int i=0;i=0&&j=0&&i
  • 不太可能是需要的,但也许你的意思是如何处理个人构造函数
  • 使列表完整(添加、删除、numberOfPersons)且无冗余(大小、下一个)
  • 在SimplePersonDatabase中,应该使用
    列表
    ,而不是
    个人[]
    ,尤其是当数组中只填充了一部分时
也就是说,有一些java集合类,如ArrayList和其他类,它们为动态列表而不是固定大小的数组提供内置功能

class List {

    Person[] persons;
    int count;

    public List() {
        this(100);
    }

    public List(int initialCapacity) {
        persons = new Person[initialCapacity];
    }

    public int numberOfPersons() {
        return count;
    }

    public void add(Person person) {
        checkUniqueId(person);
        if (count >= persons.length) {
            // Enlarge array
            persons = Arrays.copyOf(persons, persons.length + 100);
        }
        persons[count] = person;
        ++count;
    }

    private void checkUniqueId(Person person) {
        for (int i = 0; i < count; ++i) {
            if (persons[i].id == person.id) {
                throw new IllegalArgumentException("Already a person with id "
                    + person.id);
            }
        }
    }

    public void remove(int personId) {
        for (int i = 0; i < count; ++i) {
            if (persons[i].id == personId) {
                --count;
                persons[i] = persons[count];
                persons[count] = null;
                return;
            }
        }
        throw new IllegalArgumentException("No person known with id "
            + personId);
    }
}

public class Person {

    public String fn;
    public String ln;
    public Date dob;
    public int id; // Must be unique

    public Person() {
    }

    public Person(String fn, String ln, Date dob, int id) {
        this.fn = fn;
        this.ln = ln;
        this.dob = dob;
        this.id = id;
    }
}

public class SimplePersonDataBase {

    private List list;
    private int nextPersonId;

    public boolean add(Person personadd) {
        personadd.id = nextPersonId;
        ++nextPersonId;
        list.add(personadd);
    }
}
类列表{
人[]人;
整数计数;
公开名单(){
这(100);
}
公共列表(初始容量){
人员=新人员[初始能力];
}
公众国际电话号码(){
返回计数;
}
公共无效添加(个人){
支票唯一ID(个人);
如果(计数>=人数长度){
//放大阵列
persons=数组.copyOf(persons,persons.length+100);
}
人[计数]=人;
++计数;
}
私人无效支票唯一ID(个人){
对于(int i=0;i
Person[]pers=new Person[]{};?您已经在初始化List()构造函数中的Person数组。我们不理解您的问题。我想知道如何引用List并将Person[]作为参数传递到我的函数中。Person[]如果是persons,则保留列表。我无法使用该实现。我的函数必须在SimplePersonDatabase中接受Person[]类型的参数。您的主方法似乎也不正确(go之后的“{”)但是您尝试调用
go()
实际上没有一个
SimplePersonDataBase
Right Kuba的实例,我对它进行了评论,因为我的实现完全脱离了链接。非常感谢您的建议和帮助