为什么我不能让 indexOf() 等于此代码中除 0 以外的任何值?

Why can't I get indexOf() to equal anything other than 0 in this code?

我的主要目标是每次在要搜索的单词中找不到字母时,为 indexOf 获取 -1 的值。我可以使用此值来确定是否单词是 anagrams 还是不是。然而,问题是我不断得到 indexOf0。如果字母完全不同,我不应该得到 -1 吗?

import java.lang.*;
import java.io.*;
import java.util.*;


public class anagram
{


    public static void main(String args[])
    {

        Scanner kbReader = new Scanner(System.in);
        System.out.println("Enter first word");
        String a = kbReader.next();
        System.out.println("Enter second word");
        String b = kbReader.next(); ///User inputs 2 strings, a and b
        a = a.toLowerCase();
        b = b.toLowerCase();

        int numA = a.length();
        int numB = b.length();

        if (numA != numB)
           System.out.println("NOT AN ANAGRAM"); 



        for(int i = 0;i < numA; i++) ///continues until all letters are used
        {
            String letter = b.substring(i,i++);
            int checker = a.indexOf(letter);///checks word a for every letter of b

            System.out.println(checker); ///always get 0 for this value, why never -1?



       }
   }


}

改为

String letter = b.substring(i,i + 1);

另见 post increment operator java

顺便说一下,大小相等的字符串并不意味着它们是 Anagrams

回答为什么你不能从 indexOf() 得到除 0 以外的任何东西:

在此声明中,String letter = b.substring(i,i++); 假设您处于 for 循环中的第二次迭代(即 i = 2)。 您使用了 post 递增运算符,这意味着首先执行语句,然后递增值。

在那种情况下 b.substring(i,i++); 解析为 b.substring(2,2); 然后你得到 String letter = ""

根据 indexOf() 的实现,“”在任何字符串中的索引都返回为 0,这就是您的逻辑目的失败的地方。

为了便于理解,我将从 String.java class:

粘贴 indexOf() 的实现
/**
 * Code shared by String and StringBuffer to do searches. The
 * source is the character array being searched, and the target
 * is the string being searched for.
 *
 * @param   source       the characters being searched.
 * @param   sourceOffset offset of the source string.
 * @param   sourceCount  count of the source string.
 * @param   target       the characters being searched for.
 * @param   targetOffset offset of the target string.
 * @param   targetCount  count of the target string.
 * @param   fromIndex    the index to begin searching from.
 */
static int indexOf(char[] source, int sourceOffset, int sourceCount,
        char[] target, int targetOffset, int targetCount,
        int fromIndex) {
    if (fromIndex >= sourceCount) {
        return (targetCount == 0 ? sourceCount : -1);
    }
    if (fromIndex < 0) {
        fromIndex = 0;
    }
    if (targetCount == 0) {
        return fromIndex;
    }

    char first = target[targetOffset];
    int max = sourceOffset + (sourceCount - targetCount);

    for (int i = sourceOffset + fromIndex; i <= max; i++) {
        /* Look for first character. */
        if (source[i] != first) {
            while (++i <= max && source[i] != first);
        }

        /* Found first character, now look at the rest of v2 */
        if (i <= max) {
            int j = i + 1;
            int end = j + targetCount - 1;
            for (int k = targetOffset + 1; j < end && source[j]
                    == target[k]; j++, k++);

            if (j == end) {
                /* Found whole string. */
                return i - sourceOffset;
            }
        }
    }
    return -1;
}

此处 targetCount 为 0,代码中返回的值为 0。