Java 我如何允许它通过每个元素来检查它是否相等?

Java 我如何允许它通过每个元素来检查它是否相等?,java,arrays,Java,Arrays,因此,我试图建立一个纸牌加密程序,但我一直遇到一个问题,当谈到这个方法。字符数组a表示用户输入的单词(将其转换为数组以使其更容易),字符数组b表示字母表,因此它有25个索引。我想做的是把字母表和数字匹配起来。它看起来很简单,但我很难接受,因为它一直抛出数组索引OutofBoundsException。我曾尝试使用for循环、嵌套for循环和其他测试,但它不断抛出异常或只是输出意外结果,如[0,0,0,0,0]。我已经调试过了,它似乎永远不等于a[j]所以j总是0 public static in

因此,我试图建立一个纸牌加密程序,但我一直遇到一个问题,当谈到这个方法。字符数组
a
表示用户输入的单词(将其转换为数组以使其更容易),字符数组
b
表示字母表,因此它有25个索引。我想做的是把字母表和数字匹配起来。它看起来很简单,但我很难接受,因为它一直抛出
数组索引OutofBoundsException
。我曾尝试使用for循环、嵌套for循环和其他测试,但它不断抛出异常或只是输出意外结果,如
[0,0,0,0,0]
。我已经调试过了,它似乎永远不等于a[j]所以j总是0

public static int[] converter(char[] a, char[] b){
    int[] res = new int[a.length];
    int i = 0;
    int j = 0;
    while(i < a.length){
        if(b[i] == Character.toUpperCase(a[j])){ //Does not run through the first loop at all
            res[j] = i + 1;
            j = j + 1;
        } else {
            i = i + 1;
        }
    }
    return res;
}
公共静态int[]转换器(char[]a,char[]b){
int[]res=新的int[a.长度];
int i=0;
int j=0;
while(i

请不要链接类似的问题。它没有回答我的问题。

下面的代码是一个解决方案。我们希望wordCharacterIndex在单词中迭代以查看字符所在的位置。characterIndex遍历字符以与出现在wordCharacterIndex中的单词字符进行比较。设置结果后,我们需要重置characterIndex,使其返回到字符数组中的第一个字符,以便与其他单词字符进行比较。如果不这样做,单词的以下字符将需要处于更高的字符索引,这不是我们想要的。将变量命名为实际单词对于更好地理解代码中要做的事情非常重要。在迭代b[i]时,您正在比较i
public static int[] converter(char[] word, char[] characters){
    int[] result = new int[word.length];
    int characterIndex = 0;
    int wordCharacterIndex = 0;
    while(wordCharacterIndex < word.length){
        if(characters[characterIndex] == Character.toUpperCase(word[wordCharacterIndex])){
            result[wordCharacterIndex] = characterIndex + 1;
            wordCharacterIndex++;
            characterIndex = 0;
        } else {
            characterIndex++;
        }
    }
    return result;
}
公共静态int[]转换器(字符[]字,字符[]字符){
int[]结果=新int[word.length];
int characterIndex=0;
int-wordCharacterIndex=0;
while(wordCharacterIndex
Java是编程语言吗?也许可以添加标签
i
b[i]
您正在检查
i
vs
a
,然后根据
b
获取它。将变量标记为实际单词,而不是使用字母,并且代码是自注释的。那么我如何能够检查这两种情况?