Why does Arrays.asList (charArray) .indexOf always return -1?

So, I'm trying to make a method by which you send a simple string, which is then split into char on char in a for loop, each char from the array "alpha" is converted to its equivalent value in "beta", they are then combined using StringBuilder and sent back as an "exit" as shown below:

Code: I simplified the entire class and all variable names to improve readability.

import java.util.Arrays;

public class Test {
    private final char[] alpha = {  'A', 'B', 'C', 'D', 'E',
                                    'F', 'G', 'H', 'I', 'J',
                                    'K', 'L', 'M', 'N', 'O',
                                    'P', 'Q', 'R', 'S', 'T',
                                    'U', 'V', 'W', 'X', 'Y', 'Z'};

    public String getAlpha(String input) {

        StringBuilder output = new StringBuilder();

        for (int i = 0; i < input.length(); i++) {

            char c = input.charAt(i);
            int index;

            if (Character.isLetter(c)) {
                try {
                    index = Arrays.asList(alpha).indexOf(Character.toUpperCase(c));

                    System.out.println(c); //debug
                    System.out.println(index); //debug

                    //output.append(beta[index]); //gets the index and applies it to beta (unnecessary for this example)
                } catch(ArrayIndexOutOfBoundsException e) {
                    e.printStackTrace();
                }
            } else {
                output.append(c);
            }
        }

        return output.toString();
    }

    public static void main(String[] args) {
        Test test1 = new Test();

        test1.getAlpha("Hello");
    }
}

The problem is that my index for each char sequentially throws an ArrayIndexOutOfBounds exception due to the fact that indexOf cannot find the equivalent of characters in the alpha array.

- ? , , / char. .

:

"Hello"

:

H
-1
e
-1
l
-1
l
-1
o
-1
+4
4
char[] alpha = {...};
index = Arrays.asList(alpha).indexOf(Character.toUpperCase(c));

. Arrays.asList(T...) T, T extends Object. , , . T = char[] List<char[]>, , alpha.

A List<char[]> char , , indexOf -1.

, . indexOf char[], List<char[]>? , , . indexOf int indexOf(Object): , , T.

Arrays.binarySearch. . String char[].

, .

char uc = Character.toUpperCase(c);
if (uc >= 'A' && uc <= 'Z') {
    index = uc - 'A';
}
+13

, ?

OutOfBounds ( / ), , char ASCII int, , .

H
8
e
5
l
12
l
12
o
15

private final String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public String getAlpha(String input) {

    StringBuilder output = new StringBuilder();

    for (int i = 0; i < input.length(); i++) {

        char c = input.charAt(i);

        if (Character.isLetter(c)) {
            int index = Character.toUpperCase(c) - 'A' + 1;
            System.out.println(c); //debug
            System.out.println(index); //debug

            //output.append(beta[index]); //gets the index and applies it to beta (unnecessary for this example)

        } else {
            output.append(c);
        }
    }

    return output.toString();
}
+2

indexOf () performs a comparison of objects. The Character.toUpperCase () function returns a character, but each element of the array is char, so no one matches.

If you change char [] to the character [], you get the expected behavior.

0
source

Try the following: index = new String (alpha) .indexOf (Character.toUpperCase (c));

-1
source

Source: https://habr.com/ru/post/1629096/


All Articles