从键盘输入到数组人员列表

时间:2014-01-06 16:50:15

标签: java arraylist arrayobject

我想接受来自用户的输入以填充Person的Array列表。由于某些原因。我无法让它发挥作用。我可以在下面创建的列表中添加一个项目作为我的代码供参考。在交换机功能案例2中的SimplepersonDatabase类中,我想接受一个名称的输入,来自用户的出生日期和程序应该从

开始自动分配位置编号

e.g

001. Damien Kluk September, 12.09.1975
002. James  Hunt January , 12.09.2000

我应该能够删除一个人并对人员列表进行排序。这是我到目前为止所实施的内容。

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

   public Person() {
    }

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

class List {//List.java

int MAX_LIST = 20;
Person[] persons;
int count;


public List() {
    persons = new Person[MAX_LIST];
    count=0;
}

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 SimplePersonDataBase { //SimplePersonDataBase.java



private static List list;
private static int nextPersonId;



public static void main(String[] args) {

    go();

}

public static void go() {

    List link = new List();

    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:
            PersonFunctions.display();
            break;
        case 2:   // Should accept inputs from a user and update the Persons database
            TextIO.put("Firstname:");
            String fn = TextIO.getlnWord();
            TextIO.put("Lastname:");
            String ln = TextIO.getlnWord();
            Date date = DateFunctions.scanDate();
            int pos = link.count;
            Person item = new Person(fn,ln,date,pos);
            add(item);
            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);




}

   public static boolean add(Person personadd) {
       personadd.id = nextPersonId;
       ++nextPersonId;
       list.add(personadd);
       return true;
   }
}

1 个答案:

答案 0 :(得分:0)

你的清单运作良好(我试过+或 - )

import java.util.Arrays;
import java.util.Date;

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

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 MyList {

int MAX_LIST = 20;
Person[] persons;
int count;

public MyList() {
    persons = new Person[MAX_LIST];
    count = 0;
}

public int numberOfPersons() {
    return count;
}

public void add(Person person) {
    checkUniqueId(person);
    if (count >= persons.length) {
        // Enlarge array
        System.out.println("enlarging");
        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 Person get(int i) {
    return persons[i];
}

public static void main(String[] args) {

    MyList list = new MyList();
    for (int i=0; i<1000; i++) {

        list.add(new Person("fn"+i,"sn"+i,new Date(),i));
        System.out.println(list.get(i) + " " + list.count);
    }


}

}

问题在于“go”功能,为什么使用链接然后添加列表