蓝桥杯|JAVA 数组专题(韩顺平)

判断下列数组赋值是否正确(答案放在最后面啦)

String str[] = {'a','b','c'}; String str[] = {"a","b","c"}; String str[] = new String[]{"a","b","c"}; String str[] = new String{"a","b","c"}; String str[] = new String[3]{"a","b","c"};

一.简单的数组扩容
int[] arr1 = {1,2,3}; int[] arrNew = new int[arr1.length + 1]; for (int i = 0; i < arr1.length; i++) { arrNew[i] = arr1[i]; } arrNew[arr1.length] = 5; arr1 = arrNew; //将arrnew的地址传给arr1后,数组arr1将消失 for (int i = 0; i < arr1.length + 1; i++) { System.out.println(arrNew[i]); } for (int i = 0; i < arr1.length; i++) { System.out.println(arr1[i]); }

二.连续进行数组扩容
Scanner myscanner = new Scanner(System.in); int[] arr = {1,2,3}; do { int[] arrnew = new int[arr.length + 1]; for (int i = 0; i < arr.length; i++) { arrnew[i] = arr[i]; }arrnew[arrnew.length - 1] = myscanner.nextInt(); arr = arrnew; for (int i = 0; i < arrnew.length; i++) { System.out.println(arrnew[i]); }System.out.println("是否继续输入"); char key = myscanner.next().charAt(0); if (key == 'n') break; //输入n结束扩容 }while (true);


.数组删减
int[] arr = {1,2,3}; Scanner myscanner = new Scanner(System.in); do {int[] newarr = new int[arr.length - 1]; for (int i = 0; i < newarr.length; i++) { newarr[i] = arr[i]; }arr = newarr; char s = myscanner.next().charAt(0); if (newarr.length == 0 || s == 'n') break; //输入n结束缩减for (int i = 0; i < newarr.length; i++) { System.out.println(newarr[i]); }}while (true);

四.二维数组静态初始化
int[][] arr = {{1},{2,2},{3,3,3}}; //第一个一维数组的括号不能省略 ////1.定义了一个二维数组arr ////2.arr有三个元素 ////3.第一二数组有三个元素,第三个一维数组有一个数组

五.动态二维数组开空间
/*动态创建以下二维数组 i = 0:1 i = 1:22 i = 2:333 */

int[][] arr = new int[3][]; //创建了二维数组,一共有三个一维数组,但每个一维数组还没有开空间 for (int i = 0; i < arr.length; i++) { arr[i] = new int[i + 1]; //给每个一位数组开空间for (int j = 0; j < arr[i].length; j++) { arr[i][j] = i + 1; //给一维的每个元素赋值 } }//遍历输出 for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j] + " "); } System.out.println("\n"); }

例题1:杨辉三角
Scanner myscanner = new Scanner(System.in); int n = myscanner.nextInt(); int[][] arr = new int[n][]; for (int i = 0; i < arr.length; i++) { arr[i] = new int[i + 1]; arr[i][0] = 1; arr[i][i] = 1; for (int j = 1; j < i; j++) { arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1]; } }for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j] + "\t"); } System.out.print("\n"); }

例题2:给一升序数组插入一元素,保证插入后数组仍保证升序排列
int[] arr = {10,12,40,90}; Scanner myscanner = new Scanner(System.in); do { int insertnum = myscanner.nextInt(); if (insertnum == 0) break; //输入0扩容结束int index = -1; for (int i = 0; i < arr.length; i++) { if (insertnum <= arr[i]) { index = i; //确定插入位置 break; } }if (index == -1) { index = arr.length; //判断插入位置是否为最后一位 }int[] arrNew = new int[arr.length + 1]; for (int i = 0, j = 0; i < arrNew.length; i++) { if (i != index) { arrNew[i] = arr[j++]; //将arr数组拷贝到arrNew且跳过index } else { arrNew[i] = insertnum; //插入数据 } }arr = arrNew; for (int i = 0; i < arrNew.length; i++) {//遍历输出 System.out.print(arrNew[i] + "\t"); } }while(true);

例题3:冒泡排序
bubble sort int[] arr = {3,5,2,7,5,4,1,0}; for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]){ arr[j] = arr[j] ^ arr[j + 1]; arr[j + 1] = arr[j] ^ arr[j + 1]; arr[j] = arr[j] ^ arr[j + 1]; } } } for (int j = 0; j < arr.length; j++) { System.out.println(arr[j]); }

例题4:判断下列语句是否正确
String str[] = {'a','b','c'}; //error String str[] = {"a","b","c"}; //ok String str[] = new String[]{"a","b","c"}; //ok String str[] = new String{"a","b","c"}; //error String str[] = new String[3]{"a","b","c"}; //error

【蓝桥杯|JAVA 数组专题(韩顺平)】

    推荐阅读