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

时间:2014-01-05 20:40:09

标签: java arrays arraylist

我创建了一个容器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);

    }

1 个答案:

答案 0 :(得分:2)

  • 不太可能需要,但也许你的意思是如何处理Person构造函数。
  • 使列表完成(添加,删除,numberOfPersons)并且没有冗余(大小,下一个)。
  • 在SimplePersonDataBasse中,应该使用List,而不是Person[],因为只有部分数组被填充。

据说有像java.util.List,ArrayList等java集合类,它们为动态列表而不是固定大小的数组提供内置功能。<​​/ p>

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);
    }
}