Convert array to string : Array Convert « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Collections Data Structure » Array ConvertScreenshots 
Convert array to string
  
/*  ArraysX.java

{{IS_NOTE

  Purpose:
  Description:
  History:
    2001/11/13, Henri Chen: Created.

}}IS_NOTE

Copyright (C) 2001 Potix Corporation. All Rights Reserved.

{{IS_RIGHT
  This program is distributed under GPL Version 3.0 in the hope that
  it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
*/

import java.lang.reflect.Array;

/**
 * Utilities for handling arrays.
 *
 @author henrichen
 */
public class ArraysX {
  /** Converts an array to a readable string (for debugging purpose).
   */
  public final static String toString(Object[] array) {
    if (array == null)
      return "null";

    StringBuffer sb = new StringBuffer(128).append('[');
    for (int j = 0; j < array.length; ++j) {
      if (array[j== array)
        sb.append("(this array)");
      else
        sb.append(array[j]);
      if (j != array.length - 1)
        sb.append(", ");
    }
    return sb.append(']').toString();
  }
  /** Converts an array of int to a readable string (for debugging purpose).
   */
  public final static String toString(int[] array) {
    if (array == null)
      return "null";

    StringBuffer sb = new StringBuffer(128).append('[');
    for (int j = 0; j < array.length; ++j) {
      sb.append(array[j]);
      if (j != array.length - 1)
        sb.append(", ");
    }
    return sb.append(']').toString();
  }
  /** Converts an array of long to a readable string (for debugging purpose).
   */
  public final static String toString(long[] array) {
    if (array == null)
      return "null";

    StringBuffer sb = new StringBuffer(128).append('[');
    for (int j = 0; j < array.length; ++j) {
      sb.append(array[j]);
      if (j != array.length - 1)
        sb.append(", ");
    }
    return sb.append(']').toString();
  }
  /** Converts an array of short to a readable string (for debugging purpose).
   */
  public final static String toString(short[] array) {
    if (array == null)
      return "null";

    StringBuffer sb = new StringBuffer(128).append('[');
    for (int j = 0; j < array.length; ++j) {
      sb.append(array[j]);
      if (j != array.length - 1)
        sb.append(", ");
    }
    return sb.append(']').toString();
  }
  /** Converts an array of byte to a readable string (for debugging purpose).
   */
  public final static String toString(byte[] array) {
    if (array == null)
      return "null";

    StringBuffer sb = new StringBuffer(128).append('[');
    for (int j = 0; j < array.length; ++j) {
      sb.append(array[j]);
      if (j != array.length - 1)
        sb.append(", ");
    }
    return sb.append(']').toString();
  }
  /** Converts an array of char to a readable string (for debugging purpose).
   */
  public final static String toString(char[] array) {
    if (array == null)
      return "null";

    StringBuffer sb = new StringBuffer(128).append('[');
    for (int j = 0; j < array.length; ++j) {
      sb.append(array[j]);
      if (j != array.length - 1)
        sb.append(", ");
    }
    return sb.append(']').toString();
  }
  /** Converts an array of boolean to a readable string (for debugging purpose).
   */
  public final static String toString(boolean[] array) {
    if (array == null)
      return "null";

    StringBuffer sb = new StringBuffer(128).append('[');
    for (int j = 0; j < array.length; ++j) {
      sb.append(array[j]);
      if (j != array.length - 1)
        sb.append(", ");
    }
    return sb.append(']').toString();
  }
  /** Converts an array of float to a readable string (for debugging purpose).
   */
  public final static String toString(float[] array) {
    if (array == null)
      return "null";

    StringBuffer sb = new StringBuffer(128).append('[');
    for (int j = 0; j < array.length; ++j) {
      sb.append(array[j]);
      if (j != array.length - 1)
        sb.append(", ");
    }
    return sb.append(']').toString();
  }
  /** Converts an array of char to a readable string (for debugging purpose).
   */
  public final static String toString(double[] array) {
    if (array == null)
      return "null";

    StringBuffer sb = new StringBuffer(128).append('[');
    for (int j = 0; j < array.length; ++j) {
      sb.append(array[j]);
      if (j != array.length - 1)
        sb.append(", ");
    }
    return sb.append(']').toString();
  }
  /**
   * Returns the hex String representation of a byte array without prefix 0x.
   * The String is formed by making value[0] the leftmost two digits and
   * value[value.length-1] the rightmost two digits.
   *
   @param array the byte array
   */
  public final static String toHexString(byte[] array) {
    StringBuffer sb = new StringBuffer(array.length*8);
    char ch;
    for (int i=0; i< array.length; i++) {
      // byte will be promote to integer first, mask with 0x0f is a must.
      ch = Character.forDigit(array[i>>> 0x0f16);
      sb.append(ch);
      ch = Character.forDigit(array[i0x0f16);
      sb.append(ch);
    }

    return sb.toString();
  }
  /**
   * Returns the octal String representation of a byte array with optional
   * prefix. The String is formed by making value[0] the leftmost three digits
   * and value[value.length-1] the rightmost three digits.
   *
   @param array the byte array
   */
  public final static String toOctalString(byte[] array, String prefix) {
    StringBuffer sb = new StringBuffer(array.length*(3+prefix.length()) 8);
    if (prefix == null) {
      for (int i=0; i< array.length; i++) {
        appendOctalDigits(sb, array[i]);
      }
    else {
      for (int i=0; i< array.length; i++) {
        sb.append(prefix);
        appendOctalDigits(sb, array[i]);
      }
    }
    return sb.toString();
  }
  /**
   * Returns the octal digit String buffer representation of a byte.
   @param byte the byte
   */
  private final static StringBuffer appendOctalDigits(StringBuffer sb, byte b) {
    // b will be promote to integer first, mask with 0x07 is a must.
    return sb.append(Character.forDigit(b >>> 0x078))
      .append(Character.forDigit(b >>> 0x078))
      .append(Character.forDigit(b & 0x078));
  }

  /**
   * Duplicates the specified array.
   *
   * <p>The array could be an array of objects or primiitives.
   *
   @param ary the array
   @param jb the beginning index (included)
   @param je the ending index (excluded)
   @return an array duplicated from ary
   @exception IllegalArgumentException if ary is not any array
   @exception IndexOutOfBoundsException if out of bounds
   */
  public static final Object duplicate(Object ary, int jb, int je) {
    int len = Array.getLength(ary);
    if (jb<|| je>len || jb>je)
      throw new IndexOutOfBoundsException(jb + " or " + je + " exceeds " + len);

    len = je - jb;
    Object dst = Array.newInstance(ary.getClass().getComponentType(), len);
    System.arraycopy(ary, jb, dst, 0, len);
    return dst;
  }
  /**
   * Duplicates the specified array.
   @param ary the array
   @return an array duplicated from ary
   @exception IllegalArgumentException if ary is not any array
   @exception IndexOutOfBoundsException if out of bounds
   */
  public static final Object duplicate(Object ary) {
    return duplicate(ary, 0, Array.getLength(ary));
  }

  /**
   * Concat the two specified array.
   *
   * <p>The array could be an array of objects or primiitives.
   *
   @param ary the array
   @param ary1 the array
   @return an array concat the ary and ary1
   @exception IllegalArgumentException if ary and ary1 component type are different
   */
  public static final Object concat(Object ary, Object ary1) {
    int len = Array.getLength(ary+ Array.getLength(ary1);
    
    if (!ary.getClass().getComponentType().equals(ary1.getClass().getComponentType()))
      throw new IllegalArgumentException("These concated array component type are different.");
    Object dst = Array.newInstance(ary.getClass().getComponentType(), len);
    
    System.arraycopy(ary, 0, dst, 0, Array.getLength(ary));
    System.arraycopy(ary1, 0, dst, Array.getLength(ary), Array.getLength(ary1));
    
    return dst;
  }
  
  /**
   * Shrink the specified array. It is similar to duplicate, except
   * it returns the previous instance if je==length && jb==0.
   *
   @param ary the array
   @param jb the beginning index (included)
   @param je the ending index (excluded)
   @return ary or an array duplicated from ary
   @exception IllegalArgumentException if ary is not any array
   @exception IndexOutOfBoundsException if out of bounds
   */
  public static final Object shrink(Object ary, int jb, int je) {
    if (jb == && je == Array.getLength(ary))
      return ary; //nothing changed
    return duplicate(ary, jb, je);
  }
  /**
   * Resizes the specified array. Similar to {@link #shrink}, but
   * it can enlarge and it keeps elements from the first.
   */
  public static final Object resize(Object ary, int size) {
    final int oldsz = Array.getLength(ary);
    if (oldsz == size)
      return ary;

    final Object dst = Array.newInstance(ary.getClass().getComponentType(), size);
    System.arraycopy(ary, 0, dst, 0, oldsz > size ? size: oldsz);
    return dst;
  }
  /** Clones an array.
   */
  public static final Object clone(Object ary) {
    final int size = Array.getLength(ary);
    final Object dst = Array.newInstance(ary.getClass().getComponentType(), size);
    System.arraycopy(ary, 0, dst, 0, size);
    return dst;
  }
}

   
    
  
Related examples in the same category
1. Convert byte array to Integer and Long
2. Return a new byte array containing a sub-portion of the source array
3. byte array to int array
4. int array to byte array
5. Turn an array of ints into a printable string.
6. Array Converter
7. Convert array to string (from c3p0)
8. Convert byte array to string
9. Converts a Array to an Enumeration and allows it to be serializedConverts a Array to an Enumeration and allows it to be serialized
10. Array helper
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.