A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. package com.java1234.chap06;

  2. import java.util.Arrays;

  3. public class Demo2
  4. {
  5.         public static void main(String[] args)
  6.         {
  7.                 int arr[]={1,4,7,2,5,8,3,6,9};
  8.                
  9.                 String str=Arrays.toString(arr);
  10.                 System.out.println(str);
  11.                
  12.                 char c[]=str.toCharArray();
  13.                 System.out.println(c.length);
  14.                 for(int a:c)
  15.                 {
  16.                         System.out.print((char)a);
  17.                 }
  18.         }

  19. }
复制代码




结果如下:
[1, 4, 7, 2, 5, 8, 3, 6, 9]
27
[1, 4, 7, 2, 5, 8, 3, 6, 9]
在这上面看不到空格,但是看长度就知道了。是有的。是什么原因造成的?谢谢!

评分

参与人数 1技术分 +1 收起 理由
itpower + 1

查看全部评分

10 个回复

倒序浏览
Arrrys工具类,它的toString方法 这个方法自动帮你加了空格
public static String toString(int a)返回指定数组内容的字符串表示形式。字符串表示形式由数组的元素列表组成,括在方括号("[]")中。相邻元素用字符 ", "(逗号加空格)分隔。这些元素通过 String.valueOf(short) 转换为字符串。如果 a 为 null,则返回 "null"。
回复 使用道具 举报
如果想去掉空格可以在toString()后面加一个trim()
例:Arrays.toString(arr).trim();

点评

trim是去掉两头的空格 = =  发表于 2014-4-14 21:15
回复 使用道具 举报
菜小徐 发表于 2014-4-14 19:02
如果想去掉空格可以在toString()后面加一个trim()
例:Arrays.toString(arr).trim();

不好意思,测试过,不行,还是有空格:lol。
回复 使用道具 举报
本帖最后由 呆呆沙师妹 于 2014-4-14 20:05 编辑
你为谁归来 发表于 2014-4-14 19:13
不好意思,测试过,不行,还是有空格。


想去掉str中的空格,可使用str = str.replaceAll(" ", "");语句。
你用 System.out.println( "  Hello  World!  ".trim() ); 语句测试就能发现,trim()方法只能去除首尾端的空格。
回复 使用道具 举报
Arrays.toString();  返回的是数组字符串表示形式,如果你是想把数组里面的内容生成字符串的话可以这样
String str = "";
for (int i = 0 ; i < arr.length ; i++){
        str = str + arr[i];
}
回复 使用道具 举报
本帖最后由 Linuxgg 于 2014-4-15 00:12 编辑

查了源码:
  1.     /**
  2.      * Returns a string representation of the contents of the specified array.
  3.      * The string representation consists of a list of the array's elements,
  4.      * enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are
  5.      * separated by the characters <tt>", "</tt> (a comma followed by a
  6.      * space).  Elements are converted to strings as by
  7.      * <tt>String.valueOf(int)</tt>.  Returns <tt>"null"</tt> if <tt>a</tt> is
  8.      * <tt>null</tt>.
  9.      *
  10.      * @param a the array whose string representation to return
  11.      * @return a string representation of <tt>a</tt>
  12.      * @since 1.5
  13.      */
  14.     public static String toString(int[] a) {
  15.         if (a == null)
  16.             return "null";
  17.         int iMax = a.length - 1;
  18.         if (iMax == -1)
  19.             return "[]";

  20.         StringBuilder b = new StringBuilder();
  21.         b.append('[');
  22.         for (int i = 0; ; i++) {
  23.             b.append(a[i]);
  24.             if (i == iMax)
  25.                 return b.append(']').toString();
  26.             b.append(", ");
  27.         }
  28.     }
复制代码


会发现, b.append(", ");, 其本身是自动加空格的。

呵呵,所以,这里就是空格的来源。兄台,了解否?
所以,如果你不想要默认的模式,想搞成自己的也很简单啊,自己写个方法,复制粘贴,修改就ok了。
  1.   public static String toString(int[] a) {
  2.         if (a == null)
  3.             return "null";
  4.         int iMax = a.length - 1;
  5.         if (iMax == -1)
  6.             return "[]";

  7.         StringBuilder b = new StringBuilder();
  8.        // b.append('[');
  9.         for (int i = 0; ; i++) {
  10.             b.append(a[i]);
  11.             if (i == iMax)
  12.          //return b.append(']').toString();
  13.          //b.append(", ");
  14.                  return b.toString(); //收工。
  15.        }
  16.     }
复制代码


评分

参与人数 1技术分 +1 收起 理由
itpower + 1

查看全部评分

回复 使用道具 举报 1 0

亲求救啊  啊
  1. package TestDemo;

  2. import java.util.Arrays;

  3. public class ArraysDemo {

  4.         public static void main(String[] args) {
  5.                 // TODO Auto-generated method stub
  6.                 menth_2();
  7.         }
  8.         public static void menth_2()
  9.         {
  10.                 int arr[]={1,4,7,2,5,8,3,6,9};
  11.                 String str=MyArrays.toString(arr);
  12.                 System.out.println(str);
  13.                 char c[]=str.toCharArray();
  14.                 System.out.println(c.length);
  15.                 for(int a:c)
  16.                 {
  17.                         System.out.print((char)a);
  18.                 }       
  19.         }
  20.         public static String toString(int[] a) {
  21.                 if (a == null)
  22.                         return "null";
  23.                 int iMax = a.length - 1;
  24.                 if (iMax == -1)
  25.                         return "[]";

  26.                 StringBuilder b = new StringBuilder();
  27.                 // b.append('[');
  28.                 for (int i = 0; ; i++) {
  29.                         b.append(a[i]);
  30.                         if (i == iMax)
  31.                                 return b.toString(); //收工。
  32.                 }
  33.         }
  34. }
  35. //继承 Arrays 重新 toString   方法
  36. // 但是遇到一个问题  就是构造函数怎么一直有错误啊 求救
  37. class MyArrays extends Arrays
  38. {
  39.        
  40.         MyArrays()
  41.         {
  42.                
  43.         }
  44.         public static String toString(int[] a) {
  45.                 if (a == null)
  46.                         return "null";
  47.                 int iMax = a.length - 1;
  48.                 if (iMax == -1)
  49.                         return "[]";

  50.                 StringBuilder b = new StringBuilder();
  51.                 // b.append('[');
  52.                 for (int i = 0; ; i++) {
  53.                         b.append(a[i]);
  54.                         if (i == iMax)
  55.                                 return b.toString(); //收工。
  56.                 }
  57.         }
  58. }
复制代码

点评

就是在构造函数上面 出现了问题这个是咋回事啊  发表于 2014-4-15 00:51
回复 使用道具 举报
1.byte 字节型
    byte字节型是JAVA中最小的数据类型,它在内存中占8位(8个bit),取值范围从-128到127
    赋值:byte i = 127
注:byte型在赋值时,一旦超过127或小于-128,则会产生编译错误。
2.char 字符型
    char 类型在内存中占2个字节。表示一个字符,也可以使用ASCII码范围内的值来给char型的变量赋值。由于字符在内存中的取值不存在负数范围,所有取值范围从0到65535。
    赋值:char i = 'a'; 或者 char i = 97;
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马