简体   繁体   English

.contains()方法不起作用-在数组Java中查找一个int

[英].contains() method not working — finding an int in array Java

I have been looking for hours, all over the internet and SO, but cannot find anything that I understand! 我一直在互联网和SO上找了几个小时,但找不到我能理解的任何东西! ( Very new at Java ) (Java的新手)

Upon compiling, it cannot find symbol of the contain method. 编译后,它找不到contain方法的符号。 Here is the code: 这是代码:

public class LotteryTicket {
  private String nameOfBuyer;
  private int[] numberList;

  private boolean search(int val) {
    if (val >= 1 && val <= 50) {
      if (numberList.contains(val)) {
        return true;
      } else {
        return false;
      }
    }
  }

I am very new at learning, and I do not know why this is happening. 我在学习方面很陌生,我不知道为什么会这样。

int[] is a primitive array and does not have a method .contains() . int[]是一个原始数组,没有方法.contains() If you used List<Integer> instead, that would give you a .contains() method to call. 如果改用List<Integer> ,则将为您提供.contains()方法进行调用。

Also, your search method must return a value even when val < 1 or val > 50 . 另外,即使val < 1val > 50 ,您的搜索方法也必须返回一个值。

If you need numberList to be an int[] , you could try this: 如果您需要numberList成为int[] ,则可以尝试以下操作:

private boolean search(int val) {
    if (numberList != null && val >= 1 && val <= 50) {
        for(int number : numberList) {
            if (number == val) {
                return true;
            }
        }
    }
    return false;
}

Or, you could do this: 或者,您可以这样做:

private boolean search(int val) {
    if (numberList != null && val >= 1 && val <= 50) {
        return Arrays.asList(numberList).contains(val);
    }
    return false;
}

The List interface defines the method contains . List接口定义方法contains Think of an interface as a contract that classes can "sign" (in Java this is done with the keyword implements ) which says that the class must have certain things in its implementation. 将接口视为类可以“签名”的协定(在Java中,这是通过关键字implements来完成的),该约定表示类在其实现中必须具有某些条件。 A very common implementation of the List interface is ArrayList , but Lists do not work very well with the primitive int type, so what you want to do is make an ArrayList of Integers. List接口的一个非常常见的实现是ArrayList ,但是Lists与原始int类型不能很好地配合,因此您要做的是制作一个ArrayList of Integers。

The simplest way to make an ArrayList of Integers is to make an array of Integers first (I know, Java has a lot of weird steps required to get things working). 制作Integer的ArrayList的最简单方法是首先制作Integer的数组(我知道,Java有很多奇怪的步骤才能使事情起作用)。

In addition, you want to make sure that boolean methods always return a boolean value or you will get a compiler error. 另外,您要确保布尔方法始终返回布尔值,否则会出现编译器错误。

Here's a working example: 这是一个工作示例:

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

public class LotteryTicket {
    private String nameOfBuyer;
    private List<Integer> numberList;

    private boolean search(int val) {
        return (val >= 1 && val <=50) && numberList.contains(val);
    }

    public static void main(String[] args) {
        LotteryTicket lt = new LotteryTicket();
        Integer[] numberList = new Integer[] {2, 3, 4, 5, 42, 6};
        lt.numberList = new ArrayList<Integer>(Arrays.asList(numberList));
        System.out.println(lt.search(42)); // prints "true\n"
        System.out.println(lt.search(25)); // prints "false\n"
    }
}

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

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