简体   繁体   English

java中未知长度数组的条件

[英]condition on an unknown length array in java

I have a char array of a length n, which I don't know the value. 我有一个长度为n的char数组,我不知道该值。 I need to write a condition to check if all elements of my array are one by one equal to a given char 'a' . 我需要编写一个条件来检查数组中的所有元素是否都等于给定char'a 'a'一一对应。

For example, with n = 4, I convert the array to a string by doing : 例如,在n = 4的情况下,我通过执行以下操作将数组转换为字符串:

String str = new String(myArray); 

and then I do my condition like : 然后我做我的情况,如:

if (str.equals("aaaa")) {} 

but my problem is that the value of n is unknown. 但我的问题是n的值未知。 I tried to do : 我试着做:

for (int i = 0; i < n; i++) { 
    if (myArray[i].equals('a')) {
        ??
    }
}

But i don't know to do in '??' 但我不知道该怎么做 after the if, as I need to wait the for loop to be finished, because i want that all the elements of my array to be equal to 'a' . 在if之后,因为我需要等待for循环完成,因为我希望数组中的所有元素都等于'a'

A process of checking all items usually goes as follows: 检查所有项目的过程通常如下:

  • Check an individual item 检查单个项目
  • If it matches the condition, continue 如果符合条件,请继续
  • Otherwise, declare the match unsuccessful, and end the loop 否则,声明匹配失败,然后结束循环
  • If the loop ends without declaring the match unsuccessful, you have a successful match 如果循环结束但未声明匹配失败,则说明匹配成功

In terms of Java, declaring the match unsuccessful could mean setting a boolean variable to false : 就Java而言,声明匹配不成功可能意味着将boolean变量设置为false

boolean successfulMatch = true;
for (int i = 0; i <  myArray.length ; i++) { 
//                   ^^^^^^^^^^^^^^
//             Note how we check array length
    if (myArray[i] != 'a') {
    //             ^^
    //        Note != here
        successfulMatch = false;
        break;
    }
}
if (successfulMatch) {
    ...
}

In Java-8 you can do it using Stream#allMatch . 在Java-8中,您可以使用Stream#allMatch做到这Stream#allMatch It will reduce ceremonial code. 它将减少仪式代码。 You don't need to worry about the Array Length and setting the flag and breaking the loop . 您不必担心Array Lengthsetting the flagbreaking the loop

    String[] strs = {"a","a","a"};
    boolean isAllEqual = Arrays.stream(strs).allMatch(e->e.equals("a"));
    System.out.println(isAllEqual);

What about: 关于什么:

boolean matched = true;
for(int i = 0, n=myArray.length; i<n; i++){
    if(!myArray[i].equals("a")){
         matched = false;
    }
}

Then all you have to do is check matched boolean. 然后,您要做的就是检查匹配的布尔值。

You can simply use regular expression. 您可以简单地使用正则表达式。 For example: 例如:

    String s = "aaaaaaaa";
    if(s.matches("[a]*"))
        System.out.println("s only contains a");
    else if(s.matches("[A]*"))
        System.out.println("s only contains A");
    else if(s.matches("[aA]*"))
        System.out.println("s only contains A and a");
    else
        System.out.println("s not match a*");

try this 尝试这个

private static  void n(){

    char[] n = {'a', 'b'};

    String[] myArray = {"a", "a", "a"};

    for(char c : n){
        int i = 0;
        int j = 0;
        for(String s : myArray){
            if((s.equals(String.valueOf(c)))){
                i++;
            }

            if(++j == n.length){

                System.out.println(i + "->" + n.length);

                if(n.length == i){
                    System.out.println("All the elemntes in your array are the same to char array");
                }else{
                    System.out.println("Not the same");
                }
            }
        }
    }
}

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

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