I'm new to java and am trying to output this array: -5, -2, -12, 7, 3, 15, 10
To look exactly like this: [ 0: -5, 1: -2, 2: 12, 3: 7, 4: 3, 5: 15, 6: 10 ]
public static void array(int[] numbers) {
int index = 0;
System.out.print("[");
for (int i = 0; i < numbers.length; i++){
System.out.print(index + ": " + numbers[i] + ", ");
index ++;
}
System.out.print("]");
}
When I run my code it looks exactly like it should except has a comma after 10. Any ideas on how to get rid of the last comma or a better way to do this?
Thanks for any help! CK