数据类型[] 变量名;
int[] score; score = new int[10]; score[0] = 1; score[9] = 3; score[10] = 5;//Index 10 out of bounds for length 10
int[] level = { 1, 2, 3 }; level[3] = 5;//Index 3 out of bounds for length 3
int[] level = { 1, 2, 3, 4, 5 }; for (int i = 0; i < level.length; i++) { System.out.println(level[i]); }
int[] level = { 1, 2, 3, 4, 5 }; for (int i : level) { System.out.println(i); }
package base.class1;
public class Function {
public static void main(String[] args) {
int[] level = { 1, 2, 3, 4, 5 };
dis(level);
dis(reverse(level));
}
public static int[] reverse(int[] arr) {
int[] res = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
res[arr.length - 1 - i] = arr[i];
}
return res;
}
public static void dis(int[] arr) {
for (int i : arr) {
System.out.println(i);
}
}
}
int[][] arr
int[][] arr = { { 1, 2, 3, 4 }, { 4, 5, 6, 7 }, { 7, 8, 9, 0 } }; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.println(arr[i][j]); } System.out.println("---"); }
int[][] arr = { { 1, 2, 3, 4 }, { 4, 5, 6, 7 }, { 7, 8, 9, 0 } }; for (int[] is : arr) { for (int is2 : is) { System.out.print(is2 + "\t"); } System.out.println(); }
1 2 3 4 4 5 6 7 7 8 9 0
package base.class1; import java.util.Arrays; public class Function { public static void main(String[] args) { int[] arr = { 1, 12, 3, 4, 11, 23, 4, 6 }; System.out.println(Arrays.toString(arr)); Arrays.sort(arr); System.out.println(Arrays.toString(arr)); char[] chars = { 'a', 'a', 'c', '1', 'm', 'd', '2', '0' }; System.out.println(Arrays.toString(chars)); Arrays.sort(chars); System.out.println(Arrays.toString(chars)); } }
[1, 12, 3, 4, 11, 23, 4, 6] [1, 3, 4, 4, 6, 11, 12, 23] [a, a, c, 1, m, d, 2, 0] [0, 1, 2, a, a, c, d, m]
int[] arr = new int[5]; Arrays.fill(arr, 2); System.out.println(Arrays.toString(arr));
[2, 2, 2, 2, 2]