检查原始数组是否包含另一个数组

时间:2022-04-12 00:58:18

Is there any easy way to check if one array contains another array in Java?

有没有简单的方法来检查一个数组是否包含Java中的另一个数组?

Essentially, I want to do something like this:

基本上,我想做这样的事情:

private static final String NOT_ALLOWED;

public boolean isPasswordOkay(char[] password){
    return new String(password).contains(NOT_ALLOWED);
}

...but without converting the password to a String, which Sun indicates could be a security risk. Is there a neater method than manually iterating over every element of the array?

...但是没有将密码转换为字符串,Sun表示可能存在安全风险。是否有一种更简洁的方法,而不是手动迭代数组的每个元素?

5 个解决方案

#1


4  

If you use Guava, you can define a method like this:

如果您使用Guava,您可以定义如下方法:

public static boolean contains(final char[] array, final char[] target){
    return Chars.indexOf(array, target)>=0;
}

Reference:

Chars.indexOf(char[], char[])


And if you don't want to use Guava, here's the merged version of my method and Guava's:

如果你不想使用Guava,这里是我的方法和Guava的合并版本:

public static boolean contains(final char[] array, final char[] target){
    // check that arrays are not null omitted
    if (target.length == 0) {
      return true;
    }
    outer:
    for (int i = 0; i < array.length - target.length + 1; i++) {
      for (int j = 0; j < target.length; j++) {
        if (array[i + j] != target[j]) {
          continue outer;
        }
      }
      return true;
    }
    return false;
}

#2


1  

private static final String NOT_ALLOWED="...";

public boolean isPasswordOkay(char[] password){
    StringBuilder sb = new StringBuilder(password);
    boolean ret = sb.indexOf(NOT_ALLOWED) != -1;
    sb.replace(0, sb.length(), " ");
    return ret;
}

#3


0  

There is a solution that sounds like what you want at http://www.coderanch.com/t/35439/Programming/Intersection-two-arrays - Intersection is the maths-y term for this kind of thing!

有一个听起来像你想要的解决方案http://www.coderanch.com/t/35439/Programming/Intersection-two-arrays-交叉是这类事情的数学术语!

#4


0  

I can't find anything that would do this. one option may be to use Apache Collections and use the ArrayUtils subarray methods to create sub-arrays and then compare on each one that you create, iterating through the original array.

我找不到任何可以做到这一点的事情。一个选项可能是使用Apache Collections并使用ArrayUtils子阵列方法创建子数组,然后在您创建的每个子数组上进行比较,迭代原始数组。

#5


-1  

new String(password).matches(".*["+NOT_ALLOWED.replace(']','\\').replace("\\","\\\\\")+"].*");

Beware... you have to escape some of your not allowed characters, like ] and \!

要小心......你必须逃避一些不允许的角色,比如]和\!

#1


4  

If you use Guava, you can define a method like this:

如果您使用Guava,您可以定义如下方法:

public static boolean contains(final char[] array, final char[] target){
    return Chars.indexOf(array, target)>=0;
}

Reference:

Chars.indexOf(char[], char[])


And if you don't want to use Guava, here's the merged version of my method and Guava's:

如果你不想使用Guava,这里是我的方法和Guava的合并版本:

public static boolean contains(final char[] array, final char[] target){
    // check that arrays are not null omitted
    if (target.length == 0) {
      return true;
    }
    outer:
    for (int i = 0; i < array.length - target.length + 1; i++) {
      for (int j = 0; j < target.length; j++) {
        if (array[i + j] != target[j]) {
          continue outer;
        }
      }
      return true;
    }
    return false;
}

#2


1  

private static final String NOT_ALLOWED="...";

public boolean isPasswordOkay(char[] password){
    StringBuilder sb = new StringBuilder(password);
    boolean ret = sb.indexOf(NOT_ALLOWED) != -1;
    sb.replace(0, sb.length(), " ");
    return ret;
}

#3


0  

There is a solution that sounds like what you want at http://www.coderanch.com/t/35439/Programming/Intersection-two-arrays - Intersection is the maths-y term for this kind of thing!

有一个听起来像你想要的解决方案http://www.coderanch.com/t/35439/Programming/Intersection-two-arrays-交叉是这类事情的数学术语!

#4


0  

I can't find anything that would do this. one option may be to use Apache Collections and use the ArrayUtils subarray methods to create sub-arrays and then compare on each one that you create, iterating through the original array.

我找不到任何可以做到这一点的事情。一个选项可能是使用Apache Collections并使用ArrayUtils子阵列方法创建子数组,然后在您创建的每个子数组上进行比较,迭代原始数组。

#5


-1  

new String(password).matches(".*["+NOT_ALLOWED.replace(']','\\').replace("\\","\\\\\")+"].*");

Beware... you have to escape some of your not allowed characters, like ] and \!

要小心......你必须逃避一些不允许的角色,比如]和\!