简体   繁体   中英

condition on an unknown length array in java

I have a char array of a length n, which I don't know the value. I need to write a condition to check if all elements of my array are one by one equal to a given char 'a' .

For example, with n = 4, I convert the array to a string by doing :

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. 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' .

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 :

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 . It will reduce ceremonial code. You don't need to worry about the Array Length and setting the flag and breaking 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");
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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