解决方案 »

  1.   

    想实现什么 啊? debug 试试看看,就知道了
      

  2.   

    写错了 是length = 9; 正确//          改成10或大于10就会报错
      

  3.   


    实现见到空格,补%20Write a method to replace all spaces in a string with '%20'. You may assume that the
    string has sufficient space at the end of the string to hold the additional characters,
    and that you are given the "true" length of the string. (Note: if implementing in Java,
    please use a character array so that you can perform this operation in place.)
      

  4.   

    改成10
     newLength = length + (spaceCount * 2);----> 这个是12 
            str[newLength] = '\0';  str 中12 个元素 下标0开始 ,最大11
    你 给下标 12 赋值,就越界了。
      

  5.   

    str[newLength] = '\0';当length=10或大于10就会数组下标越界啊!!
      

  6.   

    晕啊,别用这种C风格了吧public void replaseSpaces(char[] str) { // replaCe
      if (str == null || str.length == 0) {
        return;
      }
      int length = str.length;
      ...
    }
      

  7.   

    如果你只是想replace的话,完全可以用String.replace()方法,
    java不是c,很多东西封装了能用就直接用.
    public void replaseSpaces(char[] ch, int length) {
            if (length > ch.length)
                length = ch.length;
            String s = new String(ch, 0, length).replace(" ", "%20");
            System.out.println(s);
            // 如果想返回char[] 把下面放开就可以了
            // return s.toCharArray();
        }
      

  8.   


    为什么这句 会导致 当length=10或大于10就会数组下标越界啊?
      

  9.   


    谢谢 好像明白些了。必须比我的实际字母数少一个?
    比如 char[] ch = {'t', 'h', 'e', ' ', 'd', 'o', 'g', 'k', 'm', 'c',  ' ',' '}; 我就只能length=9
     char[] ch = {'t', 'h', 'e'}我就只能length=2