在 Java 中显示带括号和逗号的数组输出?

Ave*_*ova 2 java arrays formatting brackets comma

我试图在我的程序中用括号和逗号打印数组。这是我的代码:

public static void main(String[] args) {

    int[] arrayIntList = new int[10]; // Starting the array with the specified length

    int sum = 0; // Defining the sum as 0 for now

    // Using the for loop to generate the 10 random numbers from 100 to 200, inclusive.
    for(int nums1 = 0; nums1 < arrayIntList.length; nums1++) {
        arrayIntList[nums1] = (int)(100 + Math.random()*101);
    }           

    Arrays.sort(arrayIntList); // Sorting the array list

    System.out.print("[");
    for(int i = 0; i < arrayIntList.length; i++) { // Printing the array
        System.out.print(arrayIntList[i] + " "); 
        }
    System.out.print("]");

    int[] arrayC = CalcArray(arrayIntList); // Sending the array to the method

    System.out.println("");

    for(int doubles : arrayC) { 
        System.out.print(doubles + " "); // Printing the output from the second method and calculating the sum
        sum = sum + doubles;
    }

    System.out.printf("%nThe total is %,d", sum); // Printing the sum
}

private static int[] CalcArray(int[] nums) {

    for(int nums2 = 0; nums2 < nums.length; nums2++) { // Doubling the original array
        nums[nums2] *= 2; 
    }
    return nums; // Returning the doubles numbers

}
Run Code Online (Sandbox Code Playgroud)

我正在寻找的格式类似于 [1, 2, 3, 4, 5, 6]。如果有人能给我一些指点,那就太好了。谢谢!

Arn*_*lle 5

Arrays.toString 可以为您完成。

更一般地说,加入者的目的是:

System.out.println(
    Arrays.stream(array)
        .mapToObj(Integer::toString)
        .collect(Collectors.joining(", ", "[", "]")));
Run Code Online (Sandbox Code Playgroud)

奖励:计算总和 Arrays.stream(array).sum();