编程技巧系列(4)Java数组转化成String
2016-04-29 11:32
411 查看
1.首先我们来看java.util.Arrays的实现 /** * Returns a string representation of the contents of the specified array. If the array contains other arrays as * elements, they are converted to strings by the {@link Object#toString} method inherited from <tt>Object</tt>, * which describes their <i>identities</i> rather than their contents. * * <p> * The value returned by this method is equal to the value that would be returned by * <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt> is <tt>null</tt>, in which case <tt>"null"</tt> is * returned. * * @param a the array whose string representation to return * @return a string representation of <tt>a</tt> * @see #deepToString(Object[]) * @since 1.5 */ public static String toString(Object[] a) { if (a == null) return "null"; int iMax = a.length - 1; if (iMax == -1) return "[]"; StringBuilder b = new StringBuilder(); b.append('['); for (int i = 0;; i++) { b.append(String.valueOf(a[i])); if (i == iMax) return b.append(']').toString(); b.append(", "); } }
2. 我们看下org.springframework.util.ObjectUtils的实现
private static final String NULL_STRING = "null"; private static final String ARRAY_START = "{"; private static final String ARRAY_END = "}"; private static final String EMPTY_ARRAY = ARRAY_START + ARRAY_END; private static final String ARRAY_ELEMENT_SEPARATOR = ", "; /** * Return a String representation of the contents of the specified array. * <p>The String representation consists of a list of the array's elements, * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated * by the characters {@code ", "} (a comma followed by a space). Returns * {@code "null"} if {@code array} is {@code null}. * @param array the array to build a String representation for * @return a String representation of {@code array} */ public static String nullSafeToString(Object[] array) { if (array == null) { return NULL_STRING; } int length = array.length; if (length == 0) { return EMPTY_ARRAY; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { if (i == 0) { sb.append(ARRAY_START); } else { sb.append(ARRAY_ELEMENT_SEPARATOR); } sb.append(String.valueOf(array[i])); } sb.append(ARRAY_END); return sb.toString(); }
3.最后自己的写了一个toString(array)
public static String nullSafeToString(String[] array) { if (null == array) { return "null"; } int length = array.length; if (0 == length) { return "[]"; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.append(i == 0 ? "[" : ", ").append(array[i]); } sb.append("]"); return sb.toString(); }
相关文章推荐
- 约瑟夫环问题求解(Java代码)
- HiveServe2 报错 java.lang.OutOfMemory PermGen space
- springmvc常用注解标签详解
- Java容器与Hash算法的研究
- Java中Try/catch/finally
- java 倒序排序
- alijdk 8.1.1的优化使ssl性能提升2倍以上
- java笔记(2)
- Java中使用JUnit测试
- spring-data-jpa 中文文档(2)
- 最最基础的java中的小概念,也相当于给自己做个笔记,仅供Java零基础的同学参考,水平有限欢迎批评指正。
- java中MD5和SHA加密
- JSP :java.lang.ClassCastException:
- JDK源码学习(3)-java.util.ArrayList与LinkedList
- 个人学习-java-集合的概述
- 20140429java.lang.NoSuchMethodError com.sun.mail.util.TraceInputStream
- Stanford Parser学习入门(1)-Eclipse中配置
- JAVA获取内部及外部配置
- Java中Double类型的四舍五入
- myeclipse注释模板修改