Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
初学者Java删除ArrayList Java问题中的对象_Java_Oop_Arraylist_Methods - Fatal编程技术网

初学者Java删除ArrayList Java问题中的对象

初学者Java删除ArrayList Java问题中的对象,java,oop,arraylist,methods,Java,Oop,Arraylist,Methods,我很难弄明白为什么,当我从arrayList调用特定对象时,它不会返回我想要的输出?我被要求创建3个新对象,并使用add方法将它们添加到arraylist中,使用get在4调用一个无效参数,使用2调用一个有效参数,显示详细信息,然后删除4和2。我从主方法得到的输出是 Invalid index position 2 The current guests in Puss in Boots Cattery: Garfield Bob John Invalid index position The c

我很难弄明白为什么,当我从arrayList调用特定对象时,它不会返回我想要的输出?我被要求创建3个新对象,并使用add方法将它们添加到arraylist中,使用get在4调用一个无效参数,使用2调用一个有效参数,显示详细信息,然后删除4和2。我从主方法得到的输出是

Invalid index position
2
The current guests in Puss in Boots Cattery:
Garfield
Bob
John
Invalid index position
The current guests in Puss in Boots Cattery:
Garfield
John
我不明白为什么我只得到2作为我的输出,而不是猫的名字约翰应该存储在2

package pkg4;

import java.util.ArrayList;

public class Cattery {  
    private ArrayList<Cat> catList;
    private String businessName;

    public Cattery(String businessName) {
        catList = new ArrayList<Cat>();
        this.businessName = businessName;
    }

    public void addCat(Cat newCat) {

        catList.add(newCat);
    }

    public void getCat(int index) {
        if ((index >= 0) && (index <= catList.size() - 1)) {
            Cat oneCat = catList.get(index);
            System.out.println(index);
        } else {
            System.out.println("Invalid index position");
        }
    }

    public void removeCat(int indexRemove) {
        if ((indexRemove >= 0) && (indexRemove <= catList.size() - 1)) {
            catList.remove(indexRemove);
        } else {
            System.out.println("Invalid index position");
        }
    }

    public void displayAllCats() {
        System.out.println("The current guests in Puss in Boots Cattery:");
        for (Cat catNames : catList) {
            System.out.println(catNames.getName());
        }
    }

    public static void main(String[] args) {
        Cattery allCats = new Cattery("Puss In Boots Cattery");
        Cat c1 = new Cat("Garfield", 2015, 10);
        Cat c2 = new Cat("Bob", 2020, 5);
        Cat c3 = new Cat("John", 2019, 9);
        allCats.addCat(c1);
        allCats.addCat(c2);
        allCats.addCat(c3);
        allCats.getCat(3);
        allCats.getCat(2);
        allCats.displayAllCats();
        allCats.removeCat(3);
        allCats.removeCat(1);
        allCats.displayAllCats();
    }
}

你正在打印索引

public void getCat(int index) {
    if ((index >= 0) && (index < catList.size())) {
        Cat oneCat = catList.get(index);                
        System.out.println(index);  // **** here ****
    }
    else{
        System.out.println("Invalid index position");
    }
}
然后你可以用你的主要方法:

try {
    System.out.println(allCats.getCat(2));
} catch (IllegalArgumentException e) {
    e.printStacktrace();
}
一些要点:

您当前的代码可以分为逻辑类,这些类包含您正在处理的问题的模型,这里是Cat和UI或用户界面类,这些类处理从用户获取信息并将信息返回给用户 逻辑类中不应包含UI代码,这意味着您不应从这些类中的用户获取或返回输入。没有指纹,没有扫描仪,什么都没有。唯一的例外是,如果您使用System.out.println作为临时穷人的调试器—在程序运行时打印出关键变量的状态,最终计划在成品中删除这些语句 用户输入和输出在UI类中应该是分开的,或者对于像这样的非常简单的程序,在main方法中应该是分开的。 任何getter方法都应该这样做——获取并返回请求的信息。您的方法被标记为void并打印出数据,这使得该方法在长期计划中相当无用。 为逻辑类提供一个公共字符串toString覆盖,允许您测试和输出对象的状态。在开始时,您将使用它们作为程序的输出,但在以后的学习中,它们将更多地用于调试目的。
你正在打印索引

public void getCat(int index) {
    if ((index >= 0) && (index < catList.size())) {
        Cat oneCat = catList.get(index);                
        System.out.println(index);  // **** here ****
    }
    else{
        System.out.println("Invalid index position");
    }
}
然后你可以用你的主要方法:

try {
    System.out.println(allCats.getCat(2));
} catch (IllegalArgumentException e) {
    e.printStacktrace();
}
一些要点:

您当前的代码可以分为逻辑类,这些类包含您正在处理的问题的模型,这里是Cat和UI或用户界面类,这些类处理从用户获取信息并将信息返回给用户 逻辑类中不应包含UI代码,这意味着您不应从这些类中的用户获取或返回输入。没有指纹,没有扫描仪,什么都没有。唯一的例外是,如果您使用System.out.println作为临时穷人的调试器—在程序运行时打印出关键变量的状态,最终计划在成品中删除这些语句 用户输入和输出在UI类中应该是分开的,或者对于像这样的非常简单的程序,在main方法中应该是分开的。 任何getter方法都应该这样做——获取并返回请求的信息。您的方法被标记为void并打印出数据,这使得该方法在长期计划中相当无用。 为逻辑类提供一个公共字符串toString覆盖,允许您测试和输出对象的状态。在开始时,您将使用它们作为程序的输出,但在以后的学习中,它们将更多地用于调试目的。
如果要打印cat对象,请通过以下方式重写cat类中的toString方法:

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", yearOfBirth=" + yearOfBirth +
                ", weightInKilos=" + weightInKilos +
                '}';
    }
,并更新此代码以打印cat对象

public void getCat(int index) {
    if ((index >= 0) && (index < catList.size())) {
        Cat oneCat = catList.get(catList.get(index));                
        System.out.println(oneCat);
    }
    else{
        System.out.println("Invalid index position");
    }
}

如果要打印cat对象,请通过以下方式重写cat类中的toString方法:

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", yearOfBirth=" + yearOfBirth +
                ", weightInKilos=" + weightInKilos +
                '}';
    }
,并更新此代码以打印cat对象

public void getCat(int index) {
    if ((index >= 0) && (index < catList.size())) {
        Cat oneCat = catList.get(catList.get(index));                
        System.out.println(oneCat);
    }
    else{
        System.out.println("Invalid index position");
    }
}

是的,没错。1+没错。1+非常感谢气垫船!!toString覆盖确实为arrayList中的第三个对象获得了所需的输出!继续表现出色,教育我们这些睡眠不足的学生:非常感谢气垫船!!toString覆盖确实为arrayList中的第三个对象获得了所需的输出!继续表现出色,教育我们睡眠不足的学生: