String getFormattedArray ( Object attribute , String indent ) { int arrayLength = Array . getLength ( attribute ) ; if ( arrayLength == 0 ) { return "[]" ; } Class < ? > componentType = attribute . getClass ( ) . getComponentType ( ) ; // For the 8 primitive types, a cast is necessary when invoking // Arrays.toString. The array data is then broken into separate // lines if ( componentType . equals ( boolean . class ) ) { return formatPrimitiveArrayString ( Arrays . toString ( ( boolean [ ] ) attribute ) , indent ) ; } else if ( componentType . equals ( byte . class ) ) { return formatPrimitiveArrayString ( Arrays . toString ( ( byte [ ] ) attribute ) , indent ) ; } else if ( componentType . equals ( char . class ) ) { return formatPrimitiveArrayString ( Arrays . toString ( ( char [ ] ) attribute ) , indent ) ; } else if ( componentType . equals ( double . class ) ) { return formatPrimitiveArrayString ( Arrays . toString ( ( double [ ] ) attribute ) , indent ) ; } else if ( componentType . equals ( float . class ) ) { return formatPrimitiveArrayString ( Arrays . toString ( ( float [ ] ) attribute ) , indent ) ; } else if ( componentType . equals ( int . class ) ) { return formatPrimitiveArrayString ( Arrays . toString ( ( int [ ] ) attribute ) , indent ) ; } else if ( componentType . equals ( long . class ) ) { return formatPrimitiveArrayString ( Arrays . toString ( ( long [ ] ) attribute ) , indent ) ; } else if ( componentType . equals ( short . class ) ) { return formatPrimitiveArrayString ( Arrays . toString ( ( short [ ] ) attribute ) , indent ) ; } // Arbitrary strings can have ', ' in them so we iterate here StringBuilder sb = new StringBuilder ( ) ; indent += INDENT ; Object [ ] array = ( Object [ ] ) attribute ; for ( int i = 0 ; i < array . length ; i ++ ) { sb . append ( "\n" ) . append ( indent ) . append ( "[" ) . append ( i ) . append ( "]: " ) ; sb . append ( String . valueOf ( array [ i ] ) ) ; } return sb . toString ( ) ; }
Format an array .