数组

2021.02.15
第十次记录
课堂笔记1:
/*
一维数组的深入,数组中存储的类型为:引用数据类型
对于数组来说,实际上只能存储java对象的内存地址。数组中存储的每个元素是“引用”
*/
代码演示1:

public class ArrayDemo07 {
    public static void main(String[] args) {
        //创建对象
        AnimalTest a1 = new AnimalTest();
        AnimalTest a2 = new AnimalTest();
        //静态初始化AnimalTest类型数组,数组元素为a1和a2
        AnimalTest[] animals = {a1, a2};
        //对AnimalTest数组进行遍历
        for (int i = 0; i < animals.length; i++) {
            //取的是AnimalTest这个数组
            /*AnimalTest a = animals[i];
            a.move();*/
            //代码合并
            //注意:animals是数组,animals[i],数组中的元素a1和a2,再调用move()方法
            animals[i].move();
        }
        System.out.println("-------我是分割线-------");
        //动态初始化一个长度为2的AnimalTest类型数组
        AnimalTest[] ans = new AnimalTest[2];
        //创建一个对象,放到数组的“第一个盒子”中,下标为0
        ans[0] = new AnimalTest();
        //AnimalTest数组中只能存放AnimalTest类型,不能放Product类型
        //ans[1] = new Product();

        //AnimalTest数组中可以存放CatTest类型的数据,因为Cat是AnimalTest的子类
        ans[1] = new CatTest();
        for (int i = 0; i < ans.length; i++) {
            ans[i].move();
        }
        System.out.println("-------我是分割线-------");
        //创建一个AnimalTest类型的数组,数组当中存储CatText和BirdTest
        AnimalTest[] ans2 = {new CatTest(), new BirdTest()};
        for(int i = 0; i < ans2.length; i++){
            //AnimalTest a = ans2[i];
            //a.move();
            //调用子类中特有方法的话需要向下转型
            if (ans2[i] instanceof CatTest){
                CatTest cat = (CatTest)ans2[i];
                cat.catchMouse();
            }else if (ans2[i] instanceof BirdTest){
                BirdTest bird = (BirdTest)ans2[i];
                bird.Sing();
            }
        }
    }
}
//动物类,父类
class AnimalTest{
    public void move(){
        System.out.println("Animal move");
    }
}
//商品类
class Product{

}
//猫类,子类
class CatTest extends AnimalTest{
    public void move(){
        System.out.println("Cat move");
    }
    public void catchMouse(){
        System.out.println("猫抓老鼠");
    }
}
//鸟类,子类
class BirdTest extends AnimalTest{
    public void move(){
        System.out.println("Bird move");
    }
    public void Sing(){
        System.out.println("鸟儿唱歌");
    }
}

输出结果:
Animal move
Animal move
-------我是分割线-------
Animal move
Cat move
-------我是分割线-------
猫抓老鼠
鸟儿唱歌

课堂笔记2:
/*
数组扩容:
1.在java开发中,数组长度一旦确认不可变,那么数组满了,你还想继续添加,就需要扩容。
2.java中对数组的扩容是:
先建立一个大容量的数组,然后将小容量数组中的数据一个一个拷贝到大数组当中。
3.数组扩容效率较低。因为涉及到拷贝问题。所以在以后的开发中请注意:尽可能少地进行数组的拷贝。
可以在创建数组对象的时候预估合适长度,这样可以减少数组扩容次数,提高效率。
*/
代码演示2:

public class ArrayDemo08 {
    public static void main(String[] args) {
        //拷贝源
        int[] src = {1, 11, 22, 3, 4};
        //拷贝目标
        int[] dest = new int[6];
        //通过JDK类中的System类中的arraycopy方法,来完成数组的拷贝
        System.arraycopy(src, 1, dest, 3, 2);
        //遍历dest目标数组
        for(int i = 0; i < dest.length; i++){
            System.out.println(dest[i]);
        }
        System.out.println("-------我是分割线-------");
        //数组中如果存储的元素是引用,也可以拷贝
        String[] strs = {"hello", "world", "study", "java", "oracle", "mysql", "jdbc"};
        String[] newStrs = new String[10];
        System.arraycopy(strs, 0, newStrs, 0, strs.length);
        for (int i = 0; i < newStrs.length; i++) {
            System.out.println(newStrs[i]);
        }
    }
}

输出结果:
0
0
0
11
22
0
-------我是分割线-------
hello
world
study
java
oracle
mysql
jdbc
null
null
null

课堂笔记3:
/*
关于java中的二维数组
1.二维数组其实是一个特殊的一维数组,特殊在这个一维数组当中的每一个元素是一个一维数组。
2.三维数组是什么?
三维数组是一个特殊的二维数组,特殊在这个二维数组当中的每一个元素是一个一维数组。
实际开发中使用最多的就是一维数组。二维数组也很少使用,三维数组几乎不用。
3.二维数组静态初始化
例如:int[][] array = {{1,2,3},{4,5,6},{5,78,596}};
4.关于二维数组中元素的:读和改
array[二维数组中一维数组的下标][一维数组的下标];
如:array[0][0];表示第一个一维数组当中的第一个元素。
5.二维数组的遍历
*/
代码演示3:

public class ArrayDemo09 {
    public static void main(String[] args) {
        //一维数组静态初始化
        int[] array1 = {100, 200, 300};
        System.out.println(array1[0]);
        //二维数组静态初始化
        int[][] array2 = {
                {100, 200, 300},
                {20, 30, 40, 50, 60},
                {6, 7, 9, 1},
                {0}
        };
        System.out.println(array2.length);
        System.out.println(array2[0].length);
        //二维数组中第一个一维数组中的第一个元素
        /*int a = array2[0][0];
        System.out.println(a);*/
        System.out.println(array2[0][0]);
        //改
        array2[0][0] = 777;
        System.out.println(array2[0][0]);
        System.out.println("-------我是分割线-------");
        //静态二维数组遍历
        String[][] array3 = {
                {"java", "oracle", "C++", "python", "C#"},
                {"张三", "李四", "王五"},
                {"lucy", "jack", "rose"}
        };
        for (int i = 0; i < array3.length; i++) {
            //String[] a = array3[i];
            for (int j = 0; j < array3[i].length; j++){
                System.out.print(array3[i][j] + " ");
            }
            //输出换行符
            System.out.println();
        }
        System.out.println("-------我是分割线-------");
        //动态二维数组遍历
        /*
        int[][] array4 = new int[4][4];
        for (int i = 0; i < array4.length; i++){
            for (int j = 0; j < array4[i].length; j++){
                System.out.print(array4[i][j] + " ");
            }
            System.out.println();
        }
        */
        String[][] array4 = {
                {" "," ",  "*"},
                {" ","*", " ", "*"},
                {"*", " ", "*", " ", "*"}
        };
        printArray(array4);
    }

    public static void printArray(String[][] array){
        for (int i = 0; i < array.length; i++){
            for (int j = 0; j < array[i].length; j++){
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
}

输出结果:

100
4
3
100
777
-------我是分割线-------
java oracle C++ python C# 
张三 李四 王五 
lucy jack rose 
-------我是分割线-------
    * 
  *   * 
*   *   * 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值