1.一维数组的使用
1)一维数组的声明和初始化
2)如何调用数组的指定位置的元素
3)如何获取数组的长度
4)如何遍历数组
public class ArrayTest { public static void main(String[] args) { //一维数组的声明和初始化; int[] ids; ids = new int[]{1001,1002,1003,1004};//1.1静态初始化:数组的初始化和数组元素的赋值操作同时进行 //也是正确的: int[] ide = {1,2,3,4,5};//类型推断 int ida[] = {1,2,3,4,5};//中括号位置可以更改 //1.2动态初始化:数组的初始化和数组元素的赋值操作分开进行 String[] names = new String[5]; //总结:数组一旦初始化完成,其长度就确定了 //2.如何调用数组的指定位置的元素:通过角标(索引)的方式调用 names[0] = "Vlahovic"; names[1] = "Pogba"; names[2] = "Mbappe"; names[3] = "Halland"; names[4] = "Cristiano"; //3.如何获取数组的长度。属性:length System.out.println(names.length); System.out.println(ids.length); //4.如何遍历数组元素 int i; for(i = 0;i < names.length;i++) { System.out.println(names[i]); } } }
5)数组元素的默认初始化值
public class ArrayTest1 { public static void main(String[] args) { //5.数组元素的默认初始化值 int i; int[] arr = new int[4]; for(i = 0;i < arr.length;i++) { System.out.println(arr[i]); } short[] arr1 = new short[4]; for(i = 0;i < arr1.length;i++) { System.out.println(arr1[i]); }//数组元素是整形:0 float[] arr2 = new float[4]; for(i = 0;i < arr2.length;i++) { System.out.println(arr2[i]); }//数组元素为浮点型:0.0 char[] arr3 = new char[4]; for(i = 0;i < arr3.length;i++) { System.out.println("----" + arr3[i] + "----"); }//数组元素为字符型:类似于空格的符号 boolean[] arr4 = new boolean[4]; for(i = 0;i < arr4.length;i++) { System.out.println(arr4[i]); }//数组元素为boolean:false String[] arr5 = new String[4]; for(i = 0;i < arr5.length;i++) { System.out.println(arr5[i]); }//数组元素为引用数据类型:null } }
6)数组的内存解析
数组地址在栈空间,赋值留在堆空间内
TEST2:
import java.util.Scanner; public class FindGrade { public static void main(String[] args) { Scanner scanf_s = new Scanner(System.in); System.out.println("请输入学生人数:"); int num1 = scanf_s.nextInt(); int[] score = new int[num1]; char[] grade = new char[num1]; int i, maxScore = 0; System.out.println("请输入" + num1 + "个学生的成绩:"); for(i = 0;i < score.length;i++) { score[i] = scanf_s.nextInt(); } for(i = 0;i < score.length;i++) { if(maxScore < score[i]) { maxScore = score[i]; } } System.out.println("最高分是" + maxScore); for(i = 0;i < score.length;i++) { if(score[i] >= (maxScore - 10)) { grade[i] = 'A'; }else if(score[i] >= (maxScore -20)) { grade[i] = 'B'; }else if(score[i] >= (maxScore - 30)) { grade[i] = 'C'; }else { grade[i] = 'D'; } } for(i = 0;i < score.length;i++) { System.out.println("student " + i + " score is " + score[i] + " grade is " + grade[i]); } } }
2.二维数组的使用(数组的元素又是数组)
1)二维数组的声明和初始化
2)如何调用数组的指定位置的元素
3)如何获取数组的长度
4)如何遍历数组
5)数组元素的默认初始化值
package com.atguigu.java; public class ArrayTest3 { public static void main(String[] args) { //数组元素的默认初始化值 int[][] arr = new int[4][3]; System.out.println(arr[0]);//地址值 System.out.println(arr[0][0]); int[][] arr1 = new int[4][]; System.out.println(arr1[4]);//null } }
6)数组的内存解析
public class ArrayTest2 { public static void main(String[] args) { //1.二维数组的声明和初始化 int[] arr = new int[]{1,2,3};//一维数组 //静态初始化 int[][] arr1 = new int[][]{{1,2,3},{4,5,6}}; //动态初始化1 String[][] arr2 = new String[3][2]; //动态初始化2 String[][] arr3 = new String[3][]; int[] arr4[] = new int[][]{{1,2,3},{4,5},{6,7,8}}; //2.如何调用数组指定位置的元素 System.out.println(arr1[0][1]);//2 System.out.println(arr2[1][1]);//null arr3[1] = new String[4];// System.out.println(arr3[1][0]); //3.获取数组的长度 System.out.println(arr4.length);//3 System.out.println(arr4[1].length);//2 System.out.println(); //4.如何遍历2维数组 int i, j; for(i = 0;i < arr4.length;i++) { for(j = 0;j < arr4[i].length;j++) { System.out.println(arr4[i][j]); } } } }
打印杨辉三角:
import java.util.Scanner; public class YanghuiTriangle { public static void main(String[] args) { Scanner scanf_s = new Scanner(System.in); System.out.println("请输入要打印的杨辉三角的行数:"); int n = scanf_s.nextInt(); int[][] yanghui = new int[n][n]; int i, j; for(i = 0;i < n;i++) { yanghui[i][0] = 1; yanghui[i][i] = 1; } for(i = 2;i < n;i++) { for(j = 1;j <= i - 1;j++) { yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j]; } } for(i = 0;i < n;i++) { for(j = 0;j <= i;j++) { System.out.print(yanghui[i][j] + " "); } System.out.println(); } } }
3.数组中涉及到的常用算法
1.数组元素的赋值
package com.atguigu.test; /* * 获取一个充满1-30的随机数的数组 */ public class SetValue { public static void main(String[] args) { int[] arr = new int[6]; int i, j; for(i = 0;i < arr.length;i++) { arr[i] = (int) (Math.random() * 30) + 1; for(j = 0;j < i;j++) { if(arr[j] == arr[i]) { i--; break; } } } for(i = 0;i < arr.length;i++) { System.out.println(arr[i]); } } }
2.数组的复制、反转、查找
package com.atguigu.exer; public class ArrayCopy { public static void main(String[] args) { /* * 实现数组的复制 * */ int[] array1 ,array2; array1 = new int[]{1,3,5,7,8}; array2 = new int[array1.length]; for(int i = 0;i < array1.length;i++) { array2[i] = array1[i]; } //输出 for(int i = 0;i < array1.length;i++) { System.out.println(array2[i]); } System.out.println(); /* * 数组的反转 * */ for(int i = 0;i < array1.length / 2;i++)//终止条件为array1.length / 2,防止反转两遍 { int temp = array1[i]; array1[i] = array1[array1.length - i - 1]; array1[array1.length - i - 1] = temp; } //输出 for(int i = 0;i < array1.length;i++) { System.out.println(array1[i]); } System.out.println(); /* * 线性查找 * * */ String[] arr = new String[]{"JJ","DD","MM","BB","GG","AA"}; String dest = "BB"; boolean isFlag = true; for(int i = 0;i <arr.length;i++) { if(dest.equals(arr[i])) { System.out.println("找到了指定的元素,位置为" + i); isFlag = false; break; } } if(isFlag) { System.out.println("很遗憾,没有找到哦!"); } System.out.println(); /* * 二分法查找 * 前提:所要查找的数组必须有序 * */ int[] arr2 = new int[]{-98, -34, 2, 34, 54, 66, 79, 105, 210, 333}; int dest1 = -34; int head = 0;//初始首索引 int end = arr2.length - 1; boolean isFlag1 = true; while(head <= end) { int middle = (head + end)/ 2; if(dest1 == arr2[middle]) { System.out.println("找到了指定的元素,位置为" + middle); isFlag1 = false; break; } else if(arr2[middle] > dest1) { end = middle - 1; } else { head = middle + 1; } } if(isFlag1) { System.out.println("很遗憾,没有找到哦!"); } System.out.println(); /* * * 排序算法 * * */ } }
3.冒泡排序和快速排序
冒泡排序
package com.atguigu.test; /* * 数组的冒泡排序的实现 * * */ public class BubbleSortTest { public static void main(String[] args) { int[] array1 = new int[]{43, 32, 76, -98, 0, 64, 33, -21, 32, 99}; //冒泡排序 int test; for(int i = 0;i < array1.length - 1;i++) { for(int j = 0;j < array1.length - i - 1;j++) { if(array1[j] > array1[j + 1]) { test = array1[j]; array1[j] = array1[j + 1]; array1[j + 1] = test; } } } for(int i = 0;i < array1.length;i++) { System.out.print(array1[i] + "\t"); } } }
快速排序
package com.atguigu.java; /** * 快速排序 * 通过一趟排序将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分关键字小, * 则分别对这两部分继续进行排序,直到整个序列有序。 * @author shkstart * 2018-12-17 */ public class QuickSort { private static void swap(int[] data, int i, int j) { int temp = data[i]; data[i] = data[j]; data[j] = temp; } private static void subSort(int[] data, int start, int end) { if (start < end) { int base = data[start]; int low = start; int high = end + 1; while (true) { while (low < end && data[++low] - base <= 0) ; while (high > start && data[--high] - base >= 0) ; if (low < high) { swap(data, low, high); } else { break; } } swap(data, start, high); subSort(data, start, high - 1);//递归调用 subSort(data, high + 1, end); } } public static void quickSort(int[] data){ subSort(data,0,data.length-1); } public static void main(String[] args) { int[] data = { 9, -16, 30, 23, -30, -49, 25, 21, 30 }; System.out.println("排序之前:\n" + java.util.Arrays.toString(data)); quickSort(data); System.out.println("排序之后:\n" + java.util.Arrays.toString(data)); } }
4.Arrays工具类的使用
1 | boolean equals(int[] a,int[] b) | 判断两个数组是否相等 |
2 | String to String(int[], a) | 输出数组信息 |
3 | void fill(int[] a, int val) | 将指定值填充到数组当中 |
4 | void sort(int[] a) | 对数组进行排序 |
5 | int binarySearch(int[] a,int key) | 对排序后的数组进行二分法检索指定的值 |
package com.atguigu.test; import java.util.Arrays; public class ArraysTest { public static void main(String[] args) { //1 boolean equals(int[] a,int[] b) 判断两个数组是否相等 int[] arr1 = new int[]{1, 2, 3, 4}; int[] arr2 = new int[]{1, 3, 2, 4}; boolean isEquals = Arrays.equals(arr1, arr2); System.out.println(isEquals); //2 String to String(int[], a) 输出数组信息 System.out.println(Arrays.toString(arr1)); //3 void fill(int[] a, int val) 将指定值填充到数组当中 Arrays.fill(arr1, 10); System.out.println(Arrays.toString(arr1)); //4 void sort(int[] a) 对数组进行排序 Arrays.sort(arr2); System.out.println(Arrays.toString(arr2)); //5 int binarySearch(int[] a,int key) 对排序后的数组进行二分法检索指定的值 int[] arr3 = new int[]{-98, -34, 2, 34, 54, 66, 79, 105, 210, 333}; int index = Arrays.binarySearch(arr3, 2); System.out.println(index);//index若返回一个负数,则没找到 } }
5.数组中常见异常
package com.atguigu.test; /** * 数组常见异常 * 1.数组角标越界异常:ArrayIndexOutOfBoundsException * * 2.空指针异常:NullPointerException * * */ public class ArrayExceptionTest { public static void main(String[] args) { //1.角标越界 int[] arr = new int[]{1, 2, 3, 4, 5}; //System.out.println(arr[5]); //System.out.println(arr[-2]); /*2.空指针异常 //情况一: int[] arr1 = new int[]{1,2,3}; arr1 = null; System.out.println(arr1[0]); //情况二: int[][] arr2 = new int[4][]; System.out.println(arr2[0]);//输出为null:堆中一维数组已经分配好内存空间 且 栈中内存指向堆空间 System.out.println(arr2[0][0]);//空指针异常,二位数组尚未分配内存空间,一维数组无指针指向二维数组 */ //情况三: String[] arr3 = new String[]{"AA","BB","CC"}; arr3[0] = null; System.out.println(arr3.toString());//对象是null } }