I mean, in terms of elegance code. Or all these commas and brackets should be placed in print? I can’t imagine how to optimize - what would have been beautiful. Do not suggest using Collections !!!
public static void main(String[] args) { int massiv[] = new int[20]; System.out.print("["); // здесь первая скобка for (int i = 0; i < massiv.length; i++) { massiv[i] = sluchaynoeCeloe(); System.out.print(massiv[i] + ", "); // собственно, запятые if (i == massiv.length - 1) // это вообще стыдно показывать System.out.print(massiv[i]); } System.out.println("]"); // ну и, последний штрих! } public static int sluchaynoeCeloe() { int a = -7, b = 13; return (int)(Math.random() * (b + (Math.abs(a) + 1))) - Math.abs(a); } Displays:
[3, 4, 6, 12, 2, 10, -1, -5, 4, 13, 11, -6, 8, -6, 12, 0, 5, 7, 1, 4, 4]