2.1数组的概括
1.定义:
多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据进行统一管理。
2.相关概念:
- 数组名
- 下标
- 元素
- 数组的长度
3.特点:
1.数组是有序排列的
2.数组本身是引用数据类型,而数组中的元素可以是任何数据类型,括基本数据类型和引用数据类型。
3.创建数组对象会在内存中开辟一整块连续的空间,而数组名中引用的是这块连续空间的首地址。
4.数组的长度一旦确定,就不能修改。
4.分类:
按照维度:一维数组、二维数组、三维数组、等
按照元素的数据类型分:基本数据类型元素的数组、用数据类型元素的数组(即对象数组)
2.2.一维数组的使用
1.一维数组的声明和初始化
一维数组的声明方式:
type var[] 或 type[] var;
例如:
int a[];
int[] a;
Java语言中声明数组时不能指定其长度(数组中元素的数), 例如: int a[5]; //非法
一维数组初始化:
1.动态初始化:数组的初始化和数组元素的赋值操作分开进行
int[] arr = new int[3];
arr[0] = 3;
arr[1] = 9;
arr[2] = 8;
String names[];
names = new String[3];
names[0] = “钱学森”;
names[1] = “邓稼先”;
names[2] = “袁隆平”;
2.静态初始化:数组的初始化和数组元素的赋值操作同时进行
int arr[] = new int[]{3,9,8}; 或 int[] arr = {3,9,8};
String names[] = {“李四光”,“茅以升”,“华罗庚” }
数组一旦初始化完成,长度就确定
2.如何调用数组指定位置的元素`
数组元素的引用方式:数组名[下标]
下标取值范围: 0 —>n-1;
下标可以是整型常量或整型表达式。如a[3] , b[i] , c[6*i]
3.如何获取数组的长度
数组名.length
4.如何遍历数组
for(int i = 0;i < 数组名.length;i++){
System.out.println(数组名[i]);
}
5.数组的默认初始化值
数组元素是
- 整型:0
- 浮点型:0.0
- char型:0 或 ‘\u0000’,而非 ‘0’
- boolean型:false
- 引用数据类型:null
2.3多维数组的使用
1.二维数组的声明和初始化
1.静态初始化:
int[][] arr = new int[][]{{1,2,3},{4,5},{6,7,8}};
2.动态初始化:
String[][] arr1 = new String[3][2];
String[][] arr2 = new String[3][];
理解: String[][] arr2 = new String[3][]; arr2[1] = new String[4];
其他正确写法:
int[] arr[] = {{1,2,3},{4,5},{6,7,8}};
int[] arr[] = new int[][]{{1,2,3},{4,5},{6,7,8}};
2.如何调用二维数组指定位置上的元素
格式:
数组名[下标][下标]
举例:
arr[1][2]
3.获取数组长度
package day004;
public class Test4 {
public static void main(String[] args) {
int[][] arr = new int[3][2];
System.out.println(arr.length);
System.out.println(arr[0].length);
}
}
4.如何遍历二维数组
package day004;
public class Test4 {
public static void main(String[] args) {
int[][] arr = new int[][]{{1,2,3},{4,5,5,6},{7,8}};
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();
}
}
}
5.二维数组元素默认的初始化值
二维数组分为外层数组的元素和内层数组的元素
针对初始化情况一:int[][] arr = new int[4][3]
外层元素的初始化值为:地址值
内层元素的初始化值为:与一维数组初始化情况相同
针对初始化情况二:int[][] arr = new int[4][]
外层元素的初始化值为:null
内层元素的初始化值为:不能调用,会报错
举例:
package day004;
public class Test3 {
public static void main(String[] args) {
int[][] arr = new int[4][3];
System.out.println(arr);
System.out.println(arr[0]);
System.out.println(arr[0][0]);
int[][] arr1 = new int[4][];
System.out.println(arr1);
System.out.println(arr1[0]);
}
}
2.4Arrays工具类的使用
package day005;
import java.util.Arrays;
public class Test7 {
public static void main(String[] args) {
int[] arr = new int[]{4,51,7,8,4,5,2,4};
int[] arr1 = new int[]{4,51,7,8,4,5,2,4};
boolean b = Arrays.equals(arr,arr1);
System.out.println(b);
System.out.println( Arrays.toString(arr));
Arrays.fill(arr,12);
System.out.println(Arrays.toString(arr));
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
int i = Arrays.binarySearch(arr1,4);
System.out.println(i);
}
}
输出结果:
true
[4, 51, 7, 8, 4, 5, 2, 4]
[12, 12, 12, 12, 12, 12, 12, 12]
[2, 4, 4, 4, 5, 7, 8, 51]
3
2.5数组中涉及的常见的算法/应用(例题)
1. 数组元素的赋值(杨辉三角、回形数等)
package day005;
public class YangHuiTest{
public static void main(String[] args) {
int[][] arr = new int[10][];
for (int i = 0;i < arr.length;i++) {
arr[i] = new int[i + 1];
arr[i][0] = arr[i][i] = 1;
if(i > 1){
for(int j = 1;j < arr[i].length-1;j++){
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}
}
}
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();
}
}
}
2. 求数值型数组中元素的最大值、最小值、平均数、总和等
package day005;
public class Test2 {
public static void main(String[] args) {
int[] arr = new int[10];
for (int i = 0;i < arr.length;i++){
arr[i] = (int)(Math.random() * 90 + 10);
System.out.print(arr[i] + " ");
System.out.println();
}
int max = 0;
for (int i = 0;i < arr.length;i++) {
if (arr[1] < arr[i]) {
arr[1] = arr[i];
}
max = arr[1];
}
System.out.println("数组最大值:");
System.out.println(max);
int min = 0;
for (int i = 0;i < arr.length;i++) {
if (arr[1] > arr[i]) {
arr[1] = arr[i];
}
min = arr[1];
}
System.out.println("数组最小值:");
System.out.println(min);
}
}
3. 数组的复制、反转、查找(线性查找、二分法查找)
复制:
package day005;
public class Test3 {
public static void main(String[] args) {
int[] arr1;
int[] arr = new int[]{1,2,3,5};
arr1 = arr;
arr1[0] = 8;
for (int i = 0;i < arr1.length;i++){
System.out.print(arr1[i]);
}
System.out.println();
for (int i = 0;i < arr1.length;i++){
System.out.print(arr[i]);
}
System.out.println();
}
}
package day005;
public class Test4 {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3,4,5};
int[] arr1 = new int[arr.length];
for (int i = 0;i < arr.length;i++){
arr1[i] = arr[i];
}
arr1[0] = 8;
for (int i = 0;i < arr1.length;i++){
System.out.print(arr1[i]);
}
System.out.println();
for (int i = 0;i < arr1.length;i++){
System.out.print(arr[i]);
}
System.out.println();
}
}
反转:
package day005;
public class Test5 {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3,4,5,6,7,8,9};
for (int i = 0;i <arr.length/2;i++){
int num;
num = arr[i];
arr[i] = arr[arr.length - i -1];
arr[arr.length - i -1] = num;
}
for (int i = 0;i < arr.length;i++){
System.out.print(arr[i]);
}
}
}
查找: ①线性查找
package day005;
public class Test5 {
public static void main(String[] args) {
int[] arr = new int[]{1,2,6,4,5,6,7,8,9};
int dest = 44;
boolean isFlag = true;
for (int i = 0;i <arr.length;i++){
if(arr[i] == dest){
isFlag = false;
System.out.println(i);
}
}
if (isFlag = true){
System.out.println("没有找到一样的元素。");
}
}
}
②二分法查找()折半查找 前提:所要查找的数组必须为有序数组
package day005;
public class Test6 {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3,4,5,7,8};
int dest =6;
int start = 0;
int end = arr.length - 1;
boolean isFlag = true;
while(start <= end){
int i = (start + end) / 2;
if (dest == arr[i]){
System.out.println(i);
isFlag = false;
break;
}else if(dest < arr[i]){
end = i -1;
}else{
start = i + 1;
}
}
if(isFlag){
System.out.println("没有找到");
}
}
}
4. 数组元素的排序算法
排序的目的是快速查找
①冒泡排序
package day005;
public class Maotest {
public static void main(String[] args) {
int[] arr = new int[]{4, 5, 6, 8, 45, 4, 1, 56, 8};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 ; j++) {
if (arr[j] > arr[j + 1]) {
int num = arr[j];
arr[j] = arr[j+1];
arr[j+1] = num;
}
}
}
for (int i = 0;i < arr.length;i++){
System.out.print(arr[i] + " ");
}
}
}
②选择排序
package day005;
public class XuanTest {
public static void main(String[] args) {
int[] arr = new int[]{4,51,7,8,4,5,2,145,5,6,85,884};
for (int i = 0;i < arr.length - 1;i++){
for (int j = i +1;j < arr.length;j++) {
if (arr[i] > arr[j]) {
int num = arr[i];
arr[i] = arr[j];
arr[j] = num;
}
}
}
for (int i = 0;i < arr.length;i++){
System.out.print(arr[i] + " ");
}
}
}
|