2016-06-10 232 views
0

我想用括號和逗號在我的程序中打印數組。這裏是我的代碼:在Java中使用括號和逗號顯示數組輸出?

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 

} 

我正在尋找的格式就像[1,2,3,4,5,6]。 如果有人能給我一些指示,那麼這將是很好的。 謝謝!

+0

首先downvote,hooray! – AvenNova

+0

你現在得到什麼?你需要把更多的信息放在這個問題上 – techspider

回答

3

Arrays.toString能爲你做到這一點。

更一般地,加入者意爲:

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

獎勵:計算與Arrays.stream(array).sum();

2

只需使用Arrays.toString,它會爲你做,更多詳情here

+0

當我嘗試在數組的for循環輸出中使用它時,由於字符串類型與int類型不兼容,我不斷收到代碼中的多個錯誤。有什麼地方我應該使用這個? – AvenNova

+0

你直接調用Arrays.toString(arrayIntList),你不需要任何循環 –

0

你可以嘗試這樣的事情,如果你想實現它自己:

int[] array = new int[]{1, 2, 3, 4, 5, 6}; 
StringBuilder sb = new StringBuilder(); 
sb.append("["); 
for (int i = 0; i < array.length; i++) { 
    sb.append(array[i]); 
    if (i < array.length - 1) { 
     sb.append(", "); 
    } 
} 
sb.append("]"); 
+0

事情是,這是爲了一個類的分配,所以我不允許使用這些方法,只有在章節中提到的那些:( – AvenNova

+0

)而不是StringBuilder可以直接使用String。 – hotzst

0

總和只看到數據結構概念上的Java它將給你更多的幫助。!。! java API提供了特殊的類來存儲和操作對象組。其中一類是ArrayList的

注意的ArrayList類是java.util.ArrayList中

創建一個ArrayList,就像任何對象。

import java.util.ArrayList; 
//.. 
ArrayList ajay = new ArrayList(); 

這裏

的ArrayList - >類

阿賈伊 - >對象

您可以選擇指定的容量和對象的類型的ArrayList將持有:

ArrayList ajay<String> = new ArrayList<String>(10); 

Arraylist類爲操作對象提供了許多有用的方法TS ..

的add()方法增加了新的對象到ArrayList.And 刪除()方法從列表中刪除的對象..

示例代碼:

import java.util.ArrayList; 
import java.util.Scanner; 
public class MyClass { 
    public static void main(String[ ] args) { 

     Scanner sc = new Scanner(System.in); 
     ArrayList<Integer> ajay = new ArrayList<Integer>(); 
     int num; 
     int i; 
     for(i=0;i<5;i++){ 
      num=sc.nextInt(); 
      ajay.add(num); 
     } 
     System.out.println(ajay); 
    } 
} 

輸入:1 2 3 4 5 輸出:

[1,2,3,4,5] 

如果你有疑問,請帶導師Java上ArrayList上的ials .. 謝謝。