I have to make a getArrayString(int[] array, char separator) method which return a string where each array entry (except the last one) is followed by the specified separator.
I know the structor of the arrays methods but in this one first I have to fined a formula for returning array except the last one and second convert my int[] array to String and return it with separator.
public static getArrayString(int[] array, char separator) {
if(array == null ||array.length == 0) {
return null;
}
int size =0;
for(int i =0; i < array.length; i++) {
size++;
}
int[] newArray2 = new int[size];
for(int i= 0, position = 0; i < array.length; i++) {
newArray2[position]=i;
position++;
System.out.print(i+',');
}
// String StArray = Arrays.toString(newArray2);
// return StArray + separator;
// newArray2 = convertStringArrayToString(StArray,separator);
// return StArray;
//
//return Arrays.toString(newArray2)+ separator;
}
// private static String convertStringArrayToString(String[] newArray,char separator) {
// StringBuilder sb = new StringBuilder();
// for(String StArray : newArray);
// return sb.substring(0,sb.length()-1);
// }
The comments are ideas of converting to String and returning it!