JAVASE第三章(数组)


前言

数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同。
Java 语言中提供的数组是用来存储固定大小的同类型元素。


一、数组的基本概念及作用

1.数组是相同数据类型元素的集合
• 2.数组本身是引用数据类型,即对象。但是数组可以存储基本数据类型,也可以存储引用数据类型。


二、数组的创建

1.数组的声明的两种方式

1.数据类型 [] 数组名字 例如: int [] a;
2.数据类型 数组的名字 [] 例如: int a [];

注意:
在Java语言中两种声明方法没有任何区别,但是建议大家用第一种,避免混淆a的数据类型

2.数组创建的三种方式

● 声明数组的同时,根据指定的长度分配内存,但数组中元素值都为默认的初始化值
int[] ary0 = new int[10];
● 声明数组并分配内存,同时将其初始化
int[] ary1 = new int[]{1, 2, 3, 4, 5};
● 与前一种方式相同,仅仅只是语法相对简略 int[] ary2 = {1, 2, 3, 4, 5};

         /*
           创建数组
           默认值
           整数:0
           浮点:0.0
           char:空格
           布尔:false
           引用类型:null
          */
        //创建数组方式1,动态创建
        int[] a = new int[10];//创建一个长度为10的连续空间.
        System.out.println(Arrays.toString(a));//将数组对象以字符串的形式输出.

       //方式2
        char x = 'a';//97
        int [] b = new int[]{1,2,3,4,x};
        System.out.println(Arrays.toString(b));

       //方式3
        int [] c = {1,2,3,4,5};
        System.out.println(Arrays.toString(c));

        String[] s = new  String[]{"a","b","c"};
        System.out.println(Arrays.toString(s));
}

3.静态数组和动态数组

1.动态创建数组(没有为元素赋值,可以结合for循环进行赋值)
char[] chAry = new char[10];
2.静态创建数组,在创建的时候,即为每个元素赋初值
int[] ary1 = new int[]{1, 2, 3, 4, 5};

3.数组的长度:length属性
int [] b1 = new int []{1,2,3,4,5,6,7};
System.out.println(b1.length);


三、数组的访问与迭代

数组元素的访问:
• 数组名字[索引] 例如:a[0],a[1];
• 注意:

数组的索引从0开始。
索引的数据类型是整型(有长度限制)
索引最大值和数组长度始终差1

1.数组迭代的两种方式

1.for循环
int [] b1 = new int []{1,2,3,4,5,6,7};
数组的迭代
for(int i =0;i<b1.length;i++){
System.out.println(b1[i]);
}

2.增强for循环
int [] b1 = new int []{1,2,3,4,5,6,7};
for(数组元素的类型 临时变量名字 :数组的名字){
System.out.println(临时变量名字 );
}
即:
for(int x:b1){
System.out.println(x);
}

public class Demo2 {
    public static void main(String[] args) {
        int []a={1,2,3};
        //索引从0开始到length-1结束
        System.out.println(a[1]);
        System.out.println(a[2]);
        //System.out.println(a[5]);   编译时不会报错,运行时会报错,下标越界
        for(int i=0;i<a.length-1;i++){
            System.out.println(a[i]);      //数组的遍历
        }
        for(int t:a){        //增强for循环,注意t的类型和数组元素的类型一致;
            System.out.println(t);
        }
    }
}


四、数组排序

1.冒泡排序

public class SortDemo1 {
    public static void main(String[] args) {
        int []a={5,3,4,2,1};
        //  5  3  4  2  1
        //  3  4  2  1  5   第一轮循环;
        //  3  2  1  4  5   第二轮循环;
        //  2  1  3  4  5   第三轮循环;
        //  1  2  3  4  5   第四轮循环;
        int temp;
        for (int i = 0; i <a.length-1 ; i++) {           //冒泡排序 (从顶端开始)
            for(int j=0;j<a.length-1-i;j++){
                if (a[j]>a[j+1]){
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        System.out.println(Arrays.toString(a));
         /*int t;                 冒泡排序(从底端开始)
        for(int i=0;i<a.length-1;i++){
            for(int j=a.length-1;j>=i+1;j--){
                if(a[j]>a[j-1]){
                    t=a[j-1];
                    a[j-1]=a[j];
                    a[j]=t;
                }
            }
        }
        System.out.println(Arrays.toString(a));
        */
    }
}

2.选择排序

public class ArraySort1 {                              //选择排序的第一种写法;
        public static void main(String[] args) {
            int []a={1,3,5,2,9};
            for (int i = 0; i < a.length-1; i++) {
                for (int j = i+1; j <a.length ; j++) {
                    if(a[j]>a[i]){
                        int temp=a[i];
                        a[i]=a[j];
                        a[j]=temp;
                    }
                }
            }
            System.out.println(Arrays.toString(a));
        }
    
    public static void main(String[] args) {              //选择排序的第二种写法,记录下标,一次循环只换一个数
      int []a={1,3,5,2,9};
        for (int i = 0; i <a.length-1 ; i++) {
            int max=i;
            for (int j = i+1; j <a.length ; j++) {
                if(a[j]>a[max]){                               //由大到小 降序
                    max=j;
                }
            }
            if(max!=i){
                int temp=a[i];
                a[i]=a[max];
                a[max]=temp;
            }
        }
        System.out.println(Arrays.toString(a));
    }

3.插入排序

 int[]a={1,3,2,4,5};                        
        for (int i = 0; i <a.length-1; i++) {
            int currentValue=a[i+1];
            int preIndex=i;
            while(preIndex>=0&&currentValue<a[preIndex]){
                a[preIndex+1]=a[preIndex];
                preIndex--;
            }
            a[preIndex+=1]=currentValue;
        }

这里只提供具体的代码实现,如果想了解更多内容可以参考博客(里面还有其他的排序方法)https://blog.csdn.net/MyronCham/article/details/84936046



五、二维数组

1.二维数组的定义

数组的数组------二维数组的每一个元素是一个一维数组例如:
int [][]a = {{1,2,3},{1,2,3},{1,2,3}};

2.二维数组的声明

1.int [][]a;
2.int a[][];
注意:建议用第一种,不容易混淆a的数据类型

3.二维数组创建

1.int [][]a = new int[][]{{1,2,3},{1,2,3},{1,2,3}};
2. int [] [] b = {{1,2,3},{1,2,3},{1,2,3}};
3 .int[][] arr = new int[3][5];—定义了一个整型的二维数组 ,这个二维数组有3 个一维数组,每一个一维数组包含5个元素.

4.二维数组的迭代

在这里插入图片描述

package day2;
import java.util.Arrays;
public class TwoArray {
    public static void main(String[] args) {
        int[][] a = new int[3][];
        a[0]=new int[]{1,2};    //单独为数组中的数组定义长度
        a[1]=new int[]{1,2,3,4};
        a[2]=new int[]{1,3,4,5,6};
        System.out.println(Arrays.toString(a));
        int[][] b = new int[][]{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
        for (int i = 0; i < b.length; i++) {    //二维数组的遍历
            for (int j = 0; j < b[i].length; j++) {
                System.out.print(b[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

六、易错习题

1.下面数组定义正确的有:
A.String strs[] = { ‘a’ ‘b’ ‘c’};
B.String[] strs = {“a”, “b”, “c”};
C.String[] strs = new String{“a” ”b” ”c”};
D.String strs[] = new String[]{“a”, “b”, “c”};
E.String[] strs = new String[3]{“a”, “b”, “c”};

答案:B、D
解析:
A:的定义数据类型不匹配,数组存放的是字符串型数据,但赋值赋了字符型;
C:缺少[]符号,正确形式应该为String[] strs = new String[]{“a” ”b” ”c”};

E:正确形式应该为:String[] strs = new String[]{“a”, “b”, “c”}

2.下面哪个数组定义是错误的:
并对错误的答案加上单行注释,写出错误的原因。

A,float[]=new float[3]; // 错误 没有数组名
B, float f2[]=new float[];// 错误,没有指定长度
C, float[] f1=new float[3];// 正确
D, boolean[] b={“true”,“false”,“true”};// 错误 ""表示存储的是字符串类型
E, double f4[]={1,3,5}; // 正确
F, int f5[]=new int[3]{2,3,4}; // 错误 正确形式应该为int f5[]=new int[]{2,3,4}
G, float f4[]={1.2F,3.0,5.4};// 错误, 3.0和5.4默认为double类型 数据类型不匹配

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JinziH Never Give Up

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值