English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

Can anyone tell me how do I print the array as bellow statement?

int [] array = new int[4];
..........

System.out.println(array[0], array[1], array[2], array[3]);

Thanks

2006-12-15 05:31:52 · 2 個解答 · 發問者 Cloud 2 in 電腦與網際網路 程式設計

2 個解答

println prints strings only. However, Java will coerce a numeric variable into a string if you do a string operation. For example, your statement will work simply adding a null string ("") or a space (" ") at the beginning, and spacing out the numbers, such as:
String space=" ";
System.out.println(space+array[0]+space+array[1]+space+array[2]+space+ array[3]);

You can be more elegant by using a for loop and print the numbers on separate lines:
String space=" ";
for(int i=0;i<4;i++)System.out.println(space+array[i]);

2006-12-15 05:42:14 · answer #1 · answered by p 6 · 0 0

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

2006-12-15 05:38:09 · answer #2 · answered by 杰倫 3 · 0 0

fedest.com, questions and answers