允许用户在Java中搜索和删除ArrayList中的对象

时间:2017-12-12 15:28:24

标签: java class object arraylist

我有一份作业,我已经到了一个我不知道如何继续的地方。

我遇到了一些问题,但我现在关注的是如何让用户删除ArrayList中的对象。

这就是我的代码。 这是我的主要内容:

import java.util.Scanner;
import java.util.ArrayList;


public class Kennel {
    Scanner scan = new Scanner(System.in);
    ArrayList<Dog> dogList = new ArrayList<Dog>();

public static void main(String[] args) {
    Kennel kenneln = new Kennel();
    kenneln.whenStart();
}

public void mainMenu() {
    System.out.println("Välj åtgärd: \n" + "[1] Registrera hund \n" + "[2] 
    Öka 
    ålder \n" + "[3] Lista svanslängd \n" + "[4] Ta bort hund \n" + "[5] 
    Avsluta \n" + "Gör ett val: ");
}

public void menuAlt() {
    System.out.println();
    int menuAlt = scan.nextInt();

    switch (menuAlt) {
    case 1:
        registerDog();
        break;
    case 2: 
        increaseAge();
        break;
    case 3:
        listTailLenght();
        break;
    case 4:
        deleteDog();
        break;
    case 5:
        programExit();
        break;
    default:
        System.out.println("Välj en siffra mellan 1 - 5");
        whenStart();
}

}
public void registerDog() {
    System.out.println("Namn: ");
    scan.nextLine();
    String name = scan.nextLine();

    System.out.print("Ras: ");
    String race = scan.nextLine();

    System.out.print("Ålder: ");
    int age = scan.nextInt();

    System.out.print("Vikt: ");
    int weight = scan.nextInt();

    Dog doggy = new Dog(name, race, age, weight);
    dogList.add(doggy);
    System.out.println(name + " är nu registrerad!");
    }

public void increaseAge() {
    System.out.println("Vilken hund har fyllt år? \n" + "Ange namn: ");
    int age = scan.nextInt();
}


public void listTailLenght() {
    System.out.print("Ange minsta svanslängd: ");
    double tailLenght = scan.nextDouble();
    System.out.println("Hundar med svanslängd längre än " + tailLenght + ":  
    ");

    for (int i = 0; i < dogList.size(); i++) {
        if (dogList.get(i).getTailLenght() >= tailLenght) {

            System.out.println(dogList.get(i).toString());
    }
  } 
}
public void deleteDog() {
    boolean search=true;
    while(search) {
      System.out.print("Mata in namn på hund som ska tas bort: ");
      String searchFor = scan.nextLine();
      int ind = dogList.indexOf(searchFor);
      if (ind == -1) {
        System.out.println("Hund med det namnet fanns ej i registret");
      } else {
        dogList.remove(ind);
        System.out.print("Hunden med det angivna namnet är borttagen ");
      }
      System.out.print("Fortsätt radera? J/N ?");
      String ans = scan.nextLine();
      if (!ans.equalsIgnoreCase("J"))
        search = false;
}



/*  
System.out.println("Mata in namn på hund som ska tas bort: ");
String delete = scan.nextLine();

        System.out.println("Hunden med det angivna namnet är borttagen"); }
    else
        System.out.println("Hund med det namnet fanns ej i registret");
}
*/
}

public void programExit() {
    System.out.println("Välkommen åter!");
    System.exit(0);
}

public void whenStart() {

    while (true) {
        mainMenu();
        menuAlt();
    }
  }
}

这是我的狗类:

public class Dog {

    private String name;
    private String race;
    private int age;
    private double weight;
    private double tailLenght;

    public Dog(String name, String race, int age, double weight) {
        this.name = name;
        this.race = race;
        this.age = age;
        this.weight = weight;
        if (race.equalsIgnoreCase("Tax")) if             ´ 
        (race.equalsIgnoreCase("Dachshund")) {
            this.tailLenght = 3.7;
        } else { 
            this.tailLenght = (age * weight) / 10;
    }

    }

    public String toString() {
        return name + " " + race + " " + age + "år " + weight + " kg " + " svans=" + tailLenght;

}


    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getRace() {
        return race;
    }
    public void setRace(String race) {
        this.race = race;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public double getTailLenght() {
        return tailLenght;
    }
    public void setTailLenght(double tailLenght) {
        this.tailLenght = tailLenght;
    }


}

该程序有5个选项或命令 1.注册狗 2.增加年龄 3.按尾长排序 4.删除狗 5.退出

This is what happens when I try to delete a dog from the ArrayList

为什么程序不允许用户搜索狗,而是直接询问他们是否要删除其他狗?

1 个答案:

答案 0 :(得分:5)

您没有覆盖equals课程的hashcodeDog方法。您将始终通过调用#ArrayList<Dog>.indexOf(String)返回-1。请参阅JavaDocs indexOf的工作原理。要解决您的问题,您可以迭代dogList并比较/搜索String之类的内容

public void deleteDog() {
    ...
    Iterator<Dog> it = dogList.iterator();
    while(it.hasNext()){
        Dog dog = it.next();
        if(dog.getName().equals(searchFor)){
            it.remove();
            break;
        }
    }
    ...
    }

这里需要Iterator的原因是,因为在循环时你无法使用增强型循环删除项目。