Java从列表数组中删除条目

时间:2013-03-08 09:23:37

标签: java arraylist

我有一个创建通讯录的任务。我基于良好的可用性创建了我的程序。例如,在创建新条目时,用户无法移动到下一个输入,直到给出有效条目或者他们选择取消。 创建程序后,我正在阅读导师给出的大学示例,而不是在添加条目之前检查有效输入,而是发送.add请求,然后在检测到错误数据时引发异常。

我问我的导师我是否应该这样做,尽管我认为我的设计更好我不想失去标记,因为我没有按照他们的方式去做。他说我应该坚持下面的例子:

public AddressBook()
{
    entries = new ArrayList < Entry >();
}

public void addPerson(Entry addPerson)
{
    entries.add(addPerson);
}

private void addCommand()
   {      

    System.out.print("Enter Full Name : ");
    String name = myScanner.nextLine();
    System.out.print("Enter House No and Street name: ");
    String street = myScanner.nextLine();
    System.out.print("Enter Town: ");
    String town = myScanner.nextLine();
    System.out.print("Enter Postcode: ");
    String postcode = myScanner.nextLine();
    postcode = postcode.toUpperCase();

    addressBook.addPerson(new Entry(name, street, town, postcode));        
   }

public Entry(String strName, String strStreet, String strTown, String strPostcode)
    {
       name = strName;
       street = strStreet;
       town = strTown;
       postcode = strPostcode;
       try
       {
          checkDetails();
       }
       catch ( BadDataException e)
        {
            throw new RuntimeException( e.getMessage());             }
    }

我试过这种方式改变了:

throw new RuntimeException(e.getMessage()); 

行阅读

System.out.println( e.getMessage());

因此它不会退出程序,但是这样做已经添加了条目,所以在给出相应的错误后我需要删除已添加的条目。我怎样才能做到这一点?它有某种索引吗?我不知道为什么导师会希望你这样做,或者我错过了这一点?

3 个答案:

答案 0 :(得分:3)

如果在此处调用的Entry构造函数中抛出异常:

addressBook.addPerson(new Entry(name, street, town, postcode));

它不会添加到您的列表中。只需将代码保留原样并在此处捕获异常:

try{
   addressBook.addPerson(new Entry(name, street, town, postcode));
catch(Exception e){
   //Tell the user what he did wrong and let him reenter
}

答案 1 :(得分:0)

您可以在构造函数中声明已检查的异常,即扩展Exception而不是RuntimeException的异常。然后,您将被迫使用addCommand方法处理它。

public AddressBook() {
    entries = new ArrayList< Entry>();
}

public void addPerson(Entry addPerson) {
    entries.add(addPerson);
}

private void addCommand() {

    System.out.print("Enter Full Name : ");
    String name = myScanner.nextLine();
    System.out.print("Enter House No and Street name: ");
    String street = myScanner.nextLine();
    System.out.print("Enter Town: ");
    String town = myScanner.nextLine();
    System.out.print("Enter Postcode: ");
    String postcode = myScanner.nextLine();
    postcode = postcode.toUpperCase();
    final Entry entry;
    try {
        entry = new Entry(name, street, town, postcode);
    } catch  (BadDataException ex) {
        System.out.println(ex.getMessage());
        return;
    }        
    addressBook.addPerson(new Entry(name, street, town, postcode));
}


public Entry(String strName, String strStreet, String strTown, String strPostcode) throws BadDataException {
    name = strName;
    street = strStreet;
    town = strTown;
    postcode = strPostcode;
    try {
       checkDetails();
    } catch(Exception ex) {
       throw new BadDataException(ex);
    }
}

private static class BadDataException extends Exception {

    public BadDataException(final Throwable cause) {
         super(cause);
    }
}

答案 2 :(得分:0)

为什么不将您的参赛作品添加到try-catch

try{
    Entry entry = new Entry(name, street, town, postcode);
    addressBook.addPerson(entry);
}catch(Exception e){
    // Entry is not added into the List.
    // do something and make the user re-enter the details.
}