Convert array to string (from c3p0) : Array Convert « Collections Data Structure « Java

Home
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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Array Convert 




Convert array to string (from c3p0)
    
/**
 * Distributed as part of c3p0 v.0.9.1.2
 
 * Copyright (C) 2005 Machinery For Change, Inc.
 
 * Author: Steve Waldman <[email protected]>
 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License version 2.1, as published
 * by the Free Software Foundation.
 
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; see the file LICENSE. If not, write to the Free
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
 * USA.
 */

public final class ArrayUtils {
  /**
   * The array may contain nulls, but <TT>o</TT> must be non-null.
   */
  public static int indexOf(Object[] array, Object o) {
    for (int i = 0, len = array.length; i < len; ++i)
      if (o.equals(array[i]))
        return i;
    return -1;
  }

  public static int identityIndexOf(Object[] array, Object o) {
    for (int i = 0, len = array.length; i < len; ++i)
      if (o == array[i])
        return i;
    return -1;
  }

  public static boolean startsWith(byte[] checkMe, byte[] maybePrefix) {
    int cm_len = checkMe.length;
    int mp_len = maybePrefix.length;
    if (cm_len < mp_len)
      return false;
    for (int i = 0; i < mp_len; ++i)
      if (checkMe[i!= maybePrefix[i])
        return false;
    return true;
  }

  /**
   * returns a hash-code for an array consistent with Arrays.equals( ... )
   */
  public static int hashArray(int[] ii) {
    int len = ii.length;
    int out = len;
    for (int i = 0; i < len; ++i) {
      // we rotate the bits of the element hashes
      // around so that the hash has some loaction
      // dependency
      int elem_hash = ii[i];
      int rot = i % 32;
      int rot_hash = elem_hash >>> rot;
      rot_hash |= elem_hash << (32 - rot);
      out ^= rot_hash;
    }
    return out;
  }

  public static int hashOrZeroArray(int[] ii) {
    return (ii == null : hashArray(ii));
  }

  // these methods are obsoleted by Arrays.toString() in jdk1.5, but
  // for libs that support older VMs...
  private static String toString(String[] strings, int guessed_len) {
    StringBuffer sb = new StringBuffer(guessed_len);
    boolean first = true;
    sb.append('[');
    for (int i = 0, len = strings.length; i < len; ++i) {
      if (first)
        first = false;
      else
        sb.append(',');
      sb.append(strings[i]);
    }
    sb.append(']');
    return sb.toString();
  }

  public static String toString(boolean[] arr) {
    String[] strings = new String[arr.length];
    int chars = 0;
    for (int i = 0, len = arr.length; i < len; ++i) {
      String str = String.valueOf(arr[i]);
      chars += str.length();
      strings[i= str;
    }
    return toString(strings, chars + arr.length + 1);
  }

  public static String toString(byte[] arr) {
    String[] strings = new String[arr.length];
    int chars = 0;
    for (int i = 0, len = arr.length; i < len; ++i) {
      String str = String.valueOf(arr[i]);
      chars += str.length();
      strings[i= str;
    }
    return toString(strings, chars + arr.length + 1);
  }

  public static String toString(char[] arr) {
    String[] strings = new String[arr.length];
    int chars = 0;
    for (int i = 0, len = arr.length; i < len; ++i) {
      String str = String.valueOf(arr[i]);
      chars += str.length();
      strings[i= str;
    }
    return toString(strings, chars + arr.length + 1);
  }

  public static String toString(short[] arr) {
    String[] strings = new String[arr.length];
    int chars = 0;
    for (int i = 0, len = arr.length; i < len; ++i) {
      String str = String.valueOf(arr[i]);
      chars += str.length();
      strings[i= str;
    }
    return toString(strings, chars + arr.length + 1);
  }

  public static String toString(int[] arr) {
    String[] strings = new String[arr.length];
    int chars = 0;
    for (int i = 0, len = arr.length; i < len; ++i) {
      String str = String.valueOf(arr[i]);
      chars += str.length();
      strings[i= str;
    }
    return toString(strings, chars + arr.length + 1);
  }

  public static String toString(long[] arr) {
    String[] strings = new String[arr.length];
    int chars = 0;
    for (int i = 0, len = arr.length; i < len; ++i) {
      String str = String.valueOf(arr[i]);
      chars += str.length();
      strings[i= str;
    }
    return toString(strings, chars + arr.length + 1);
  }

  public static String toString(float[] arr) {
    String[] strings = new String[arr.length];
    int chars = 0;
    for (int i = 0, len = arr.length; i < len; ++i) {
      String str = String.valueOf(arr[i]);
      chars += str.length();
      strings[i= str;
    }
    return toString(strings, chars + arr.length + 1);
  }

  public static String toString(double[] arr) {
    String[] strings = new String[arr.length];
    int chars = 0;
    for (int i = 0, len = arr.length; i < len; ++i) {
      String str = String.valueOf(arr[i]);
      chars += str.length();
      strings[i= str;
    }
    return toString(strings, chars + arr.length + 1);
  }

  public static String toString(Object[] arr) {
    String[] strings = new String[arr.length];
    int chars = 0;
    for (int i = 0, len = arr.length; i < len; ++i) {
      String str;
      Object o = arr[i];
      if (instanceof Object[])
        str = toString((Object[]) o);
      else if (instanceof double[])
        str = toString((double[]) o);
      else if (instanceof float[])
        str = toString((float[]) o);
      else if (instanceof long[])
        str = toString((long[]) o);
      else if (instanceof int[])
        str = toString((int[]) o);
      else if (instanceof short[])
        str = toString((short[]) o);
      else if (instanceof char[])
        str = toString((char[]) o);
      else if (instanceof byte[])
        str = toString((byte[]) o);
      else if (instanceof boolean[])
        str = toString((boolean[]) o);
      else
        str = String.valueOf(arr[i]);
      chars += str.length();
      strings[i= str;
    }
    return toString(strings, chars + arr.length + 1);
  }

  private ArrayUtils() {
  }

  /*
   * public static void main(String[] argv) { int[] is = {1,2,3,4}; String[] ss =
   * {"Hello", "There"}; Object[] os = {"Poop", is, ss, new Thread()};
   
   * System.out.println( toString(is) ); System.out.println( toString(ss) );
   * System.out.println( toString(os) ); }
   */
}

   
    
    
    
  














Related examples in the same category
1.Convert array to string
2.Convert byte array to Integer and Long
3.Return a new byte array containing a sub-portion of the source array
4.byte array to int array
5.int array to byte array
6.Turn an array of ints into a printable string.
7.Array Converter
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
11.Return a String representation of the given two-dimensional object array
12.Return a string representation of the given native two-dimensional long array.
13.Concatenates the given int[] array into one String, inserting a delimiter between each pair of elements.
14.Concatenates the given long[] array into one String, inserting a delimiter between each pair of elements.
15.Converts an array of Strings to a comma-sperated-list.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.