如何查找 arraylist 是否包含类字段中的值?

发布于 2024-11-19 03:14:11 字数 1234 浏览 6 评论 0原文

我有课:

static class AnotherClass {
        int number;
        String isPrime;

        public int getNumber() {
            return number;
        }

        public String getPrime() {
            return isPrime;
        }

        public void setNumber(int number) {
            this.number = number;
        }

        public void setPrime(String prime) {
            isPrime = prime;
        }

    }

在主课上我有:

    List<AnotherClass> listx = new ArrayList<AnotherClass>();//just a arraylist

for (int z = 0; z < howManyQuestions; z++) {//in loop i add class with fields
            AnotherClass classx = new AnotherClass();
            int valuex = Integer.parseInt(keyboardkey.readLine());

            classx.setNumber(valuex);//save value in this class

            String answer = Check(valuex);//i just get here string answer YES NO
            classx.setPrime(answer);

            listx.add(classx);//and i add this two fields of class to list

            System.out.println();

        }
INPUT: (i add value and check if it was input before)
3
4
3

OUTPUT
NO
NO
YES

How can i check if, for example value "3" is containing by list?

I have class:

static class AnotherClass {
        int number;
        String isPrime;

        public int getNumber() {
            return number;
        }

        public String getPrime() {
            return isPrime;
        }

        public void setNumber(int number) {
            this.number = number;
        }

        public void setPrime(String prime) {
            isPrime = prime;
        }

    }

and in main class i have:

    List<AnotherClass> listx = new ArrayList<AnotherClass>();//just a arraylist

for (int z = 0; z < howManyQuestions; z++) {//in loop i add class with fields
            AnotherClass classx = new AnotherClass();
            int valuex = Integer.parseInt(keyboardkey.readLine());

            classx.setNumber(valuex);//save value in this class

            String answer = Check(valuex);//i just get here string answer YES NO
            classx.setPrime(answer);

            listx.add(classx);//and i add this two fields of class to list

            System.out.println();

        }
INPUT: (i add value and check if it was input before)
3
4
3

OUTPUT
NO
NO
YES

How can i check if, for example value "3" is containing by list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与往事干杯 2024-11-26 03:14:11

1 AnotherClass 必须实现 equals()(并相应地实现 hashCode())。

2 使用 listx 中的 contains(Object o) 方法。

1 AnotherClass must implement equals() (and implement hashCode() accordingly).

2 Use method contains(Object o) from listx.

故乡的云 2024-11-26 03:14:11
private boolean contains(int i)
{
    for(int j: listx.getNumber()) { if(i == j) return true; }
    return false;
}
private boolean contains(int i)
{
    for(int j: listx.getNumber()) { if(i == j) return true; }
    return false;
}
栀梦 2024-11-26 03:14:11

一些注意事项 -

你的类不需要静态。如果您要声明内部类,那么这很有用。

您有一个容器类,其中包含一个依赖于 int 的字符串以及 int。在类中进行检查会更惯用,例如,

class AnotherClass {
        int number;


        public int getNumber() {
            return number;
        }

        public String getPrime() {
            return check(number)
        }

       private boolean check() { ... whatever logic you had .. }
}

如果您正在寻找“包含”功能,则可能会使用 HashSet 或 LinkedHashSet(如果您想保留排序)。如果您想对创建的类执行此操作,则需要实现 hashCode() 方法来告诉 hashSet 如何知道它是否具有重复值。

或者您可以迭代您的列表。

A Few notes -

your class doesn't require static. That has a use if you're declaring an inner class.

You have a container class holding a string that's dependant on an int, as well as the int. It'd be more idiomatic to have the check inside your class, e.g.

class AnotherClass {
        int number;


        public int getNumber() {
            return number;
        }

        public String getPrime() {
            return check(number)
        }

       private boolean check() { ... whatever logic you had .. }
}

If you're looking for "contains" functionality, you'd probably use a HashSet, or a LinkedHashSet( if you want to preserve the ordering ). If you want to do this with your created class you'll need to implement a hashCode() method to tell the hashSet how to know if it has a duplicate value.

Or you can just iterate over your list.

真心难拥有 2024-11-26 03:14:11

您必须为 AnotherClass 实现 equals()。默认的 equals() 比较同一性而不是值相等性。

List.contains() 的 javadoc 表示:

如果此列表包含指定元素,则返回 true。更正式地说,当且仅当此列表包含至少一个元素 e 且满足 (o==null ? e==null : o.equals(e)) 时,才返回 true。

You have to implement equals() for AnotherClass. The default equals() compares identity instead of value equality.

The javadoc for List.contains() says:

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文