【问题标题】:Why is String + '\u0000' different than String?为什么 String + '\u0000' 与 String 不同?
【发布时间】:2015-05-02 01:57:33
【问题描述】:

我的算法构造一个单词并在 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;
    }
}

每个节点都是这样构造的:

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;
    }
}

构建时设置单词的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;
}

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

我的逻辑是添加“null”字符不会更改字符串,打印出"hello" + '\u0000' 可以确认这一点。发生了什么?

【问题讨论】:

    标签: java string char


    【解决方案1】:

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

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

    0068 0065 006C 006C 006F 00000068 0065 006C 006C 006F 相同

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

    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;
        }
    

    【讨论】:

    • 谢谢,这很有道理。有没有真正的空字符这样的东西?
    • 如果你问是否有charnull 那么没有
    • 无论如何我都不知道你说的“空”是什么意思
    • 在conacenation中为空不会改变字符串的位值
    【解决方案2】:

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

    因为 '/u0000'(或 NUL)是有效字符,而不是字符串终止符。

    Java 中的字符串是一个字符序列,而不是由 NUL(或零)终止的字符序列。


    (实际上,它在技术上比这更复杂。/u0000 是一个 16 位 UTF-16 代码单元,也恰好是一个 Unicode 代码点。字符串是一个序列16 位 char 值可能是也可能不是有效的 UTF-16 代码单元,可能是也可能不是格式良好的 Unicode 代码点序列。但无论哪种方式,零字符/代码点/代码- unit 不是字符串终止符。)


    有没有真正的空字符这样的东西?

    没有。当然,不是在 Java 中。

    【讨论】:

      猜你喜欢
      • 2019-02-12
      • 2012-11-14
      • 2015-01-13
      • 1970-01-01
      • 2021-11-01
      • 2019-02-23
      • 2012-08-05
      • 2021-10-13
      • 2011-01-17
      相关资源
      最近更新 更多