如何检查整数是否在嵌套数组中?

时间:2022-03-04 15:58:53

I have looked through a lot of various articles and have yet to find a solution that works. I have tried: Array, Arrays, ArrayUtil, and import java.lang.Object.org.apache.commons.lang.ArraysUtils; however nothing has worked. Array, Arrays, and ArrayUtil before "contains()" results in error "cannot find symbol variable (Array/Arrays/ArrayUtil)", import java.lang.Object.org.apache.commons.lang.ArraysUtils results in error "cannot find symbol: class org"

我查看了很多不同的文章,还没有找到有效的解决方案。我试过:Array,Arrays,ArrayUtil和import java.lang.Object.org.apache.commons.lang.ArraysUtils;但没有任何效果。 “contains()”之前的数组,数组和ArrayUtil导致错误“无法找到符号变量(Array / Arrays / ArrayUtil)”,导入java.lang.Object.org.apache.commons.lang.ArraysUtils导致错误“不能找到符号:class org“

http://pastebin.com/Dmwnf8FT

http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/ArrayUtils.html

1 个解决方案

#1


0  

You just need:

您只需要:

import org.apache.commons.lang.ArrayUtils;

rather than the java.lang.Object part.

而不是java.lang.Object部分。

You will also need to make sure reference to the jar is on your classpath. If you are using maven you can add the following dependency into your pom:

您还需要确保jar类的引用位于类路径中。如果您使用maven,可以将以下依赖项添加到您的pom中:

<dependency>
  <groupId>commons-lang</groupId>
  <artifactId>commons-lang</artifactId>
  <version>2.4</version>
</dependency>

Edit:

Here's a code example that will return true/false if the integer is in the nested array or not. Compiled under jdk1.6.0_29 and works fine:

这是一个代码示例,如果整数在嵌套数组中,则返回true / false。在jdk1.6.0_29下编译并正常工作:

import org.apache.commons.lang.ArrayUtils;

public class Main {

    public static void main(String[] args) {

        int[][] myArray = {{1, 4, 5, 6, 9}, {9, 1, 3, 2}};

        System.out.println(arrayContain(myArray, 2));
    }

    public static boolean arrayContain(int[][] myArray, int valueToFind) {
        if (ArrayUtils.contains(myArray, 1)) return true;

        for (int i = 0; i < myArray.length; i++) {
            if (ArrayUtils.contains(myArray[i], valueToFind)) return true;
        }
        return false;
    }
}

#1


0  

You just need:

您只需要:

import org.apache.commons.lang.ArrayUtils;

rather than the java.lang.Object part.

而不是java.lang.Object部分。

You will also need to make sure reference to the jar is on your classpath. If you are using maven you can add the following dependency into your pom:

您还需要确保jar类的引用位于类路径中。如果您使用maven,可以将以下依赖项添加到您的pom中:

<dependency>
  <groupId>commons-lang</groupId>
  <artifactId>commons-lang</artifactId>
  <version>2.4</version>
</dependency>

Edit:

Here's a code example that will return true/false if the integer is in the nested array or not. Compiled under jdk1.6.0_29 and works fine:

这是一个代码示例,如果整数在嵌套数组中,则返回true / false。在jdk1.6.0_29下编译并正常工作:

import org.apache.commons.lang.ArrayUtils;

public class Main {

    public static void main(String[] args) {

        int[][] myArray = {{1, 4, 5, 6, 9}, {9, 1, 3, 2}};

        System.out.println(arrayContain(myArray, 2));
    }

    public static boolean arrayContain(int[][] myArray, int valueToFind) {
        if (ArrayUtils.contains(myArray, 1)) return true;

        for (int i = 0; i < myArray.length; i++) {
            if (ArrayUtils.contains(myArray[i], valueToFind)) return true;
        }
        return false;
    }
}