【问题标题】:input from Keyboard into Array list of persons从键盘输入到人员数组列表
【发布时间】:2014-01-06 16:50:15
【问题描述】:

我想接受用户的输入来填充 Person 的 Array 列表。由于某些原因。我无法让它工作。我可以将一个项目添加到我创建的列表中,下面是我的代码供参考。在 switch 函数案例 2 的 SimplepersonDatabase 类中:,我想接受用户输入的姓名、出生日期,并且程序应该自动分配从

开始的位置编号

例如

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

【问题讨论】:

  • 如果这是家庭作业,可能实现您自己的列表。如果没有,请使用Java's ArrayList
  • “我无法让它工作”不是很具有描述性。你能告诉我们你得到的例外吗?请描述您期望的确切行为。
  • 请注意:由于您希望从列表中删除对象,因此您在尝试使用 int nextPersonId 实现它时会遇到逻辑错误。例如,如果有 10 个 id,而您要删除的人的 id 为 5,按照您实现的方式,要么根本不会再次使用 5,要么您将执行 nextPersonId-1,这将导致逻辑​​错误(添加新对象时 2 个具有相同 ID 的对象)。我建议您生成一些 ID,将它们添加到队列中,并在添加/删除用户时入队/出队。
  • 好的。我期望的行为是将用户添加到列表中并显示它们。随着添加用户。我还想从列表中删除对象。该人员应自动分配给将添加到列表中的新用户

标签: java arraylist arrayobject


【解决方案1】:

您的列表运行良好(我尝试过 + 或 -)

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”函数中,为什么要使用链接然后添加到列表中

【讨论】:

  • 您分配给 person.id = link.count 但 link.count 永远不会改变....删除链接变量并使用 list.count
  • 我试过了。但我无法显示人员列表。你能看看我的显示功能。也许是问题或其他原因
猜你喜欢
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多