为什么 String + '\u0000' 与 String 不同?

ylu*_*.ca 2 java string char

我的算法构造一个词并在 TST 中查找与该词相关联的值。

private Node get(Node x, String key, int index) {
    if (key.isEmpty()) {
        return root;
    }
    if (x == null) {
        return null;
    }
    char c = key.charAt(index);
    if (c < x.val) {
        return get(x.left, key, index);
    } else if (c > x.val) {
        return get(x.right, key, index);
    } else if (index < key.length() - 1) {
        return get(x.mid, key, index + 1);
    } else {
        return x;
    }
}
Run Code Online (Sandbox Code Playgroud)

每个节点的构造如下:

private class Node {
    private char val;
    private Node left, mid, right;
    private Double selfWeight;
    private double maxWeight;

    /**
     * Node constructor.
     */
    private Node(char c) {
        val = c;
        maxWeight = 0.0;
        selfWeight = null;
    }
}
Run Code Online (Sandbox Code Playgroud)

一个词的maxWeight是在构建过程中设置的,它是一个TST标准构建的修改版本:

private Node put(Node x, String key, Double weight, int index) {
    char c = key.charAt(index);
    if (x == null) {
        x = new Node();
        x.val = c;
    }
    if (c < x.val) {
        x.left = put(x.left, key, weight, index);
    } else if (c > x.val) {
        x.right = put(x.right, key, weight, index);
    } else if (index < key.length() - 1) {
        x.mid = put(x.mid, key, weight, index + 1);
    } else {
        x.selfWeight = weight;
    }
    if (weight > x.maxWeight) {
        x.maxWeight = weight;
    }
    return x;
}
Run Code Online (Sandbox Code Playgroud)

在运行我的算法时,如果我插入,例如“hello”的权重为 20,并且我搜索get("hello" + '\u0000');该方法将返回 null,而我调用get("hello")该方法将返回 20。这是为什么?

我的逻辑是添加“空”字符不会改变字符串,打印出来"hello" + '\u0000'证实了这一点。怎么了?

Tra*_*nce 5

它们不是同一个字符串,因为它们不包含相同的字符。仅仅因为你看不到一个角色并不意味着它不存在。

如果您转换hello为 unicode 那么您所声称的是

0068 0065 006C 006C 006F 0000 是相同的 0068 0065 006C 006C 006F

如果您需要进一步解释,请查看 equals 方法 String

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.equals%28java.lang.Object%29

/**
     * Compares this string to the specified object.  The result is {@code
     * true} if and only if the argument is not {@code null} and is a {@code
     * String} object that represents the same sequence of characters as this
     * object.
     *
     * @param  anObject
     *         The object to compare this {@code String} against
     *
     * @return  {@code true} if the given object represents a {@code String}
     *          equivalent to this string, {@code false} otherwise
     *
     * @see  #compareTo(String)
     * @see  #equalsIgnoreCase(String)
     */
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = count;
            if (n == anotherString.count) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = offset;
                int j = anotherString.offset;
                while (n-- != 0) {
                    if (v1[i++] != v2[j++])
                        return false;
                }
                return true;
            }
        }
        return false;
    }
Run Code Online (Sandbox Code Playgroud)