int[] nums;//1.声明一个数组,此时数组并不存在 nums = new int[3];//2.用new操作符来创建数组 //3.给数组元素赋值 nums[0] = 1; nums[1] = 2; nums[2] = 3; //创建和赋值可同时进行 //int[3] nums = {1,2,3};
int[3] arrays = {1,2,3}; for (int i : a) { System.out.println(i); }//遍历打印
二维数组形如arrays[2] [3]={{1,2,5},{2,3,6}}
1 | 2 | 5 |
---|---|---|
2 | 3 | 6 |
public static void main(String[] args) { int[] a = {1,2,23,64,568,74,11};//建立数组 int[] sort = sort(a);//用编写的排序方法排序后赋给sort System.out.println(Arrays.toString(sort));//打印数组 } //冒泡排序 public static int[] sort(int[] array){ int temp = 0; for (int i = 0; i < array.length-1; i++) { for (int j = 0; j < array.length-1-i; j++) { if (array[j+1]<array[j]){ temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } return array; }