Java数组

Java数组

1.数组概述

什么是数组?
数组是一种线性数据结构。是多个相同类型数据按一定顺序列的集合,并使用一个名字命名,通过编号的方式对这些数据进行统一管理。

数组的特点

  • 数组是有序排列的
  • 数组属于引用数据类型的变量,数组的元素,既可以是基本数据类型,也可以是引用数据类型。
  • 创建数组对象会在内存中开辟一套整块连续的空间
  • 数组的长度一旦确定,就不可修改,

数组的分类

  • 按维度分:一维数组、二维数组、. . .
  • 按数组元素的类型:基本数据类型元素的数组、引用数据类型元素的数组。

2.一维数组

声明和初始化

public class ArrayDemo {
    public static void main(String[] args) {
        //数组的声明,声明时不能指定数组长度
        int []score;
        //1.静态初始化:数组的初始化和数组元素的赋值操作同时进行
        score = new int []{32,56,89};
        
        //2.动态初始化,数组的初始化和数组元素的赋值操作分开进行
        score = new int[3];
        score [0]=98;
        score [1]=33;
        score [2]=75;
        //数组的声明和创建
        int [] count = new int[3];
    }
}

遍历数组

public class ArrayDemo {
    public static void main(String[] args) {
      
        //赋值创建数组
        int [] great ={0,1,2,3};
        for (int i=0;i< great.length;i++){//获取数组的长度
            System.out.println(great[i]);
        }
        //新加的
        for (int x:great){
            System.out.println(x);
        }
    }
}

默认初始化值

数组元素是整型:0
数组元素是浮点型:0.0
数组元素是char型:0或‘\u0000',而非‘0‘
数组元素是boolean型:false

数组元素是引用数据类型:null

一维数组的内存解析

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RtsF2jWe-1661988025403)(C:\Users\chenpeixue\AppData\Roaming\Typora\typora-user-images\image-20220829211736476.png)]

/**
 *从键盘输入学生成绩,找出最高分,并输出学生的成绩等级。(成绩>=最高分-10 等级‘A’
 *成绩>=最高分-20 等级‘B’  成绩>=最高分-30 等级‘C’  其它情况 等级‘D’)
 */
import java.util.Scanner;
public class ScoreText {
    public static void main(String[] args) {
        //1.从键盘输入学生的个数
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学生的个数");
        int number = scanner.nextInt();
        //2.创建数组,从键盘上输入学生的成绩
        int[]score = new int[number];
        System.out.println("请输入学生的成绩");
        for(int i =0;i<score.length;i++){
            score[i] = scanner.nextInt();
        }
        //3.输出学生成绩的最大值
        int maxscore = 0;
        for(int i = 0;i<score.length;i++){
            if(maxscore<score[i]){
                maxscore=score[i];
            }
        }
        System.out.println("成绩最高为:"+maxscore);
        //4.输出学生的成和等级
        char level;
        for(int i = 0;i<score.length;i++) {
            if (score[i]>=maxscore - 10) {
                level='A';
            }else if(score[i]>=maxscore - 20){
                level='B';
            }else if(score[i]>=maxscore - 30){
                level='C';
            }else{
                level='D';
            }
            System.out.println("studnet"+i+"score is"+score[i]+",gerat is"+level);
        }
    }
}

3.二维数组

理解:我们可以看成是一维数组Array1作为一维数组Array2的元素而存在。

声明和初始化

public class Array {
    public static void main(String[] args) {
        //二维数组的声明和初始化
        //1.静态初始化
        int[][]num = new int[][]{{1,2,3},{4,5}};
        //2.动态初始化
        String [][]arr = new String[3][2];
        String [][]arr1 = new String[3][];
        //其它正确写法
        int[] arr2[] = new int[][]{{1,2,3},{4,5}};
        int[] arr3[] = {{1,2,3},{4,5}};
    }
}

遍历数组

public class Array {
    public static void main(String[] args) {
        int[] arr2[] = new int[][]{{1,2,3},{4,5}};
        //遍历二维数组
        for(int i =0;i< arr2.length;i++){
            for(int j = 0;j<arr2[i].length;j++){
                System.out.print(arr2[i][j]+" ");
            }
            System.out.println();
        }
    }
}

默认初始化值

//二维数组的默认初始化值
针对于初始化方式一、
    比如:int[][]arr = new int[2][3];
	外层元素的初始化值为:地址值
	内层元素的初始化值为:与一维数组的初始化情况一致
        
针对于初始化方式二
    比如:int[][]arr = new int[2][];
	外层元素的初始化值为:null
    内层元素的初始化值为:不能调用,否则报错

二维数组的内存解析

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2HlNxvjl-1661988025405)(C:\Users\chenpeixue\AppData\Roaming\Typora\typora-user-images\image-20220829213916099.png)]

4.数组中常见的算法

数组元素的赋值

杨辉三角

public class YangHui {
    public static void main(String[] args) {
        //1.创建二维数组
        int [][]yanghui = new int[10][];
        //2.赋值
        for(int i = 0;i<yanghui.length;i++){
            yanghui[i] = new int[i+1];
            yanghui[i][0] = yanghui[i][i] = 1;
            for(int j = 1;j<yanghui[i].length-1;j++){
                System.out.println(yanghui[i][j]=yanghui[i-1][j]+yanghui[i-1][j-1]);
            }
        }
        //3.遍历二维数组
        for (int i = 0;i<yanghui.length;i++){
            for(int j = 0;j<yanghui[i].length;j++){
                System.out.print(yanghui[i][j]+" ");
            }
            System.out.println();
        }
    }
}

求数值型数组元素的各种值

/*
* 定义一个int型的一维数组,包含10个元素,分别赋一些随机整数,
* 然后求出所有元素的最大值,最小值,和值,平均值,并输出出来。要求:所有随机数都是两位数。
*
* [10,99]
* 公式  :(int)(Math.random()*(99-10+1)+10)
 */
public class HelloWorld {
    public static void main(String[] args) {
        int[] array = new int[10];
        for (int i = 0; i <array.length ; i++) {
            array[i] =  (int)(Math.random()*(99-10+1)+10);
            System.out.print(array[i]+"\t");
        }
        System.out.println();
//        最大值
        int maxValue = array[0];
        for (int i = 0; i <10 ; i++) {
           if(maxValue<array[i]){
               maxValue = array[i];
           }
        }
        System.out.println("最大值=>"+maxValue);
//        最小值
        int minValue = array[0];
        for (int i = 0; i <10 ; i++) {
            if(minValue>array[i]){
                minValue =array[i];
            }
        }
        System.out.println("最小值=>"+minValue);
//        平均值
        int sum = 0;
        for (int i = 0; i <10 ; i++) {
            sum += array[i];
        }
        System.out.println("和=>"+sum);
        System.out.println("平均值=>"+sum / array.length);
    }
}

数组元素的复制

public class Test {
    public static void main(String[] args) {
        //数组元素的复制
        String[]array = new String[]{"aa","bb","cc","dd"};
        //数组的复制不同于数组的赋值(array1=array 只改变了变量名,但其地址值是相同的,公用相同的数据)
        String[]array1 = new String[array.length];
        for (int i= 0;i<array.length;i++){
            array1[i]=array[i];
            System.out.print(array1[i]+"\t");
        }
    }
}

数组元素的反转

public class Test {
    public static void main(String[] args) {
        String[]array = new String[]{"aa","bb","cc","dd"};
        for (int i = 0; i <(array.length/2) ; i++) {
            String temp = array[i];
            array[i] = array[array.length-i-1];
            array[array.length-i-1] = temp;
        }
        for (int i = 0; i <array.length ; i++) {
            System.out.println(array[i ]+"\t");
        }
    }
}

数组的查找

顺序查找

public class Test {
    public static void main(String[] args) {
        String[]array = new String[]{"aa","bb","cc","dd"};
        String bib = "aa";
        boolean isFlog = true;
        for(int i=0;i<array.length;i++){
            if(bib.equals(array[i])){
                System.out.println("已查找到搜索元素,当前位置"+i);
                isFlog = false;
                break;
            }
        }
        if(isFlog){
            System.out.println("未找到所查找元素");
        }
    }
}

二分法查找

public class ArrayTest {
    public static void main(String[] args) {
        //二分法查找:必须是有序的
        int []arr = new int[]{12,23,34,45,56,67,78,89};
        int be = 34;
        int head = 0;//出事的首索引
        int end = arr.length-1;//出事的末索引
        boolean isFlog1 = true;
        while (head <= end) {
            int mid = (head+end)/2;
            if (be == arr[mid]) {
                System.out.println("已查找到搜索元素,当前位置" + mid);
                isFlog1 = false;
                break;
            }else if(be <arr[mid]){
                end = mid - 1;
            }else if(be >arr[mid]){
                head = mid +1;
            }
        }
        if(isFlog1) {
            System.out.println("未找到所查找元素");
        }
    }
}

排序算法

排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存。

冒泡排序

public class Arrays {
    public static void main(String[] args) {
        int[]arr = new int[]{10,12,32,-25,89,62,-15,0,16};
        //冒泡排序 (相邻之间经行排序 i是轮数,j是次数)
        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]){
                 int temp = arr[j];
                 arr[j] = arr[j+1];
                 arr[j+1] = temp;
                }
            }
        }for(int i = 0;i<arr.length-1;i++){
            System.out.print(arr[i]+"\t");
        }
    }
}

5.Arrays工具类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v6lkSGpI-1661988025406)(C:\Users\chenpeixue\AppData\Roaming\Typora\typora-user-images\image-20220901070816016.png)]

public class ArratsText {
    public static void main(String[] args) {
        int[]arr = new int[]{1,2,3,4};
        int[]arr1 = new int[]{1,3,2,4};
        //equals(int[] a, int[] a2) 如果两个指定的int数组彼此 相等 ,则返回 true 。
        boolean isFlog = Arrays.equals(arr,arr1);
        System.out.println(isFlog);
        //toString(int[] a) 返回指定数组的内容的字符串表示形式。
        System.out.println(Arrays.toString(arr));
        //fill(int[] a, int val) 将指定的int值分配给指定的int数组的每个元素。
        Arrays.fill(arr,10);
        System.out.println(Arrays.toString(arr));
        //sort(int[] a) 按照数字顺序排列指定的数组。
        Arrays.sort(arr1);
        System.out.println(Arrays.toString(arr1));
        //binarySearch(int[] a, int key) 使用二叉搜索算法搜索指定的int数组的指定值。
        int digit = Arrays.binarySearch(arr1,3);
        System.out.println("第"+digit+"位");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值