Wednesday, June 22, 2011

String utility function : Converting int[] to String

Method 1 - Writing our own function

public static String toString(int[] intArray) {

String separator =
",";

StringBuilder sb =
new StringBuilder("");

if (intArray != null && intArray.length > 0) {

for (int i = 0; i < intArray.length; i++) {

sb.append(intArray[i]);

if (i < (intArray.length - 1)) {

    sb.append(separator);

}

}

}

return sb.toString();

}

Method2 - Using Arrays.asList()
Arrays.asList(intArray).toString()

No comments:

Post a Comment

Chitika