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] 

    5 answers 5

    You can use the standard behavior from Arrays.toString (Object [] array)

    And it says there

     public static String toString(Object[] a) { if (a == null) return "null"; int iMax = a.length - 1; if (iMax == -1) return "[]"; StringBuilder b = new StringBuilder(); b.append('['); for (int i = 0;; i++) { b.append(String.valueOf(a[i])); if (i == iMax) return b.append(']').toString(); b.append(", "); } } 

      The code in question generally does not work correctly:

       if (i == massiv.length - 1) // это вообще стыдно показывать System.out.print(massiv[i]); 

      Here the last number is displayed again. http://ideone.com/tOYrbS


      Under the condition should be placed output separator. And I would compare with zero, but not with length:

      http://ideone.com/cBQsc4

       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(); if (i != 0) { System.out.print(", "); } System.out.print(massiv[i]); } System.out.println("]"); } 

      Well, as already mentioned in all the answers, it is worth using Arrays.toString :

      http://ideone.com/1Pp7J2

       public static void main(String[] args) { int massiv[] = new int[20]; for (int i = 0; i < massiv.length; i++) { massiv[i] = sluchaynoeCeloe(); } System.out.println(Arrays.toString(massiv)); } 

        You can use the toString(...) method of the Arrays class.

        For example, the code:

         int[] a = new int[5]; System.out.println(Arrays.toString(a)); 

        Displays:

         [0, 0, 0, 0, 0] 

          I would first of all separate initialization and output.

           int massiv[] = new int[20]; for (int i = 0; i < massiv.length; i++) { massiv[i] = sluchaynoeCeloe(); } 

          And then it is very simple:

           System.out.println(Arrays.toString(massiv)); 

          Output peeped here

             public static void main(String[] args) { int massiv[] = new int[20]; System.out.print("["); // разделитель элементов массива. String razdelitel = ""; // сначала перед первым элементом пустой for (int i = 0; i < massiv.length; i++) { massiv[i] = sluchaynoeCeloe(); System.out.print(razdelitel + massiv[i]); razdelitel = ", "; // после первого элемента разделитель всегда ", " // или с условием: if (razdelitel.isEmpty()) razdelitel = ", "; } System.out.println("]"); }