简体   繁体   English

我的 isEmpty 方法不起作用,我不知道为什么

[英]My isEmpty method isn’t working and I'm not sure why

I am just trying to check whether an ArrayList is empty or not so I looked into java libraries and found that there is an isEmpty method in java that does this for me but my code won't compile here.我只是想检查 ArrayList 是否为空,所以我查看了 java 库,发现 java 中有一个 isEmpty 方法,但我的代码不会在这里编译。 It keeps saying it can't find the method.一直说找不到方法。 I haven't defined the method in my code but I didn't think I had to since it is a java defined method and I already imported java.util.ArrayList.我没有在我的代码中定义该方法,但我认为我不必这样做,因为它是 java 定义的方法,并且我已经导入了 java.util.ArrayList。 If this method isn't used anymore is there any other way I can check if an ArrayList is empty?如果不再使用此方法,是否还有其他方法可以检查 ArrayList 是否为空?

else if((userChoice.equals("S")) || (userChoice.equals("s"))){
    boolean empty = this.cat.isEmpty();
    if (empty == true){
        System.out.println("there is nothing i this catalog");
    }
    else{
        this.cat.printList();
    }
}

Here is the cat that im calling it on这是我打电话给它的猫

this.name = name;
this.cat = new Catalog();
//CREATE ARRAYLIST HERE
cat.addItem(new Music(1111,"White and Nerdy",2.50,"\"Weird Al\" Yankovic"));
cat.addItem(new Music(2222,"Amish Paradise",2.22,"\"Weird Al\" Yankovic"));
cat.addItem(new Music(3333,"The Saga Begins",2.00,"\"Weird Al\" Yankovic"));
cat.addItem(new Movie(4444,"UHF",9.99,"\"Weird Al\" Yankovic"));
cat.addItem(new Movie(5555,"The Dark Crystal",8.99,"\"Jim Henson"));
cat.addItem(new Movie(6666,"Die Hard",13.99,"Bruce Willis"));
cat.addItem(new Book(7777,"When I Grow Up",7.98,"\"Weird Al\" Yankovic"));
cat.addItem(new Book(8888,"The Chronicles of Pern: First Fall",5.99,"\"Anne McCaffrey"));
cat.addItem(new Book(9999,"Get gud you scrub",2.50,"Steve \"Troll\" Rathier"));

EDIT: Here is the constructor for my catalog class编辑:这是我的目录 class 的构造函数

public Catalog()
    {
        //makes empty arraylist
        // initialise instance variables
        items = new ArrayList<>(MAX);
        size = 0;

    }

There is an easy way for this:有一个简单的方法:

public class MyClass {
    List<Object> myObjects;
    Object[] myArrayObjects;

    void myMethod() {
        if (myArrayObjects == null || myArrayObjects.length == 0) {
            //Array is empty
        } else {
            //Array has some data
        }
        if (myObjects == null || myObjects.size() == 0) {
            //List is empty
        } else {
            //List has some data
        }
    }
}

another solution which is better I think, is something like this我认为另一个更好的解决方案是这样的

    public class Catalog {
        List<Object> items;

        public Catalog() {
            items = new ArrayList<>();
        }

        public void addItem(Object object) {
            items.add(object);
        }

        public boolean isEmpty() {
            return items.isEmpty();
        }

        public int size() {
            return items.size();
        }
    }

and usage of the class is like this class 的用法是这样的

public class MyClass {

    Catalog catalog;

    public MyClass() {
        this.catalog = new Catalog();
    }

    void printCatalog() {
        if (catalog.isEmpty()) {
            //catalog is empty
        } else {
            //catalog has some data you can print them
        }
    }

    void addItems(Object object) {
        catalog.addItem(object);
    }
}

java.util.ArrayList also has size() method. java.util.ArrayList 也有size()方法。 You can use that to check for the list size, if it's 0, your list is empty.您可以使用它来检查列表大小,如果为 0,则您的列表为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM