数组

1. 容器:将多个数据存储到⼀起,每个数据都称为该容器的⼀个元素
2. 数组:数组就是⽤于存储数据的固定的容器,保证多个数据的数据
类型要⼀致
3. 数组的特点:
    1. 数组的长度⼀但确定不能修改
    2. 创建数组时,会在内存中开辟⼀块连续的空间
    3. 存取元素的速度 快,因为可以通过下标,直接定位到任意⼀个
元素
4. 数组定义
      1. ⽅式⼀:静态初始化
         数据类型[ ] 数组名 = {元素1,元素2,。。。。}
     2. ⽅式⼆: 静态初始化
         数据类型[ ] 数组名 = new 数据类型[ ] {元素1,元素2,。。。。}
     3. ⽅式三: 动态初始化
       1. 数据类型 [ ] 数组名;
       2. 数组名 = new 数据类型[长度];
       3. 数据名 = new 数据类型[] {元素1,元素2,元素3,。。。。}
     4. 例⼦
       
public class Demo01 {
    public static void main(String[] args) {
        String[] names = {"吕布","赵云","典韦","关羽","张飞"};
        String[] name1 = new String[]{"吕布","赵云","典韦","关羽","张飞"};
        String[] name2;
        name2 = new String[]{"吕布","赵云","典韦","关羽","张飞"};
        String[] name3;
        name3 = new String[5];
        name3[0] = "吕布";
        System.out.println(name3[0]);

    }
}
5. [] 表⽰是⼀个数组。
6. new关键字:创建数组时使⽤的,数组本⾝是⼀个引⽤数据类型,所以需要使⽤new创建⼀个数组对象。
7. [长度] 数组的长度,表⽰这个数组容器中,可以存放多少个元素。
       长度⼀旦指定不能修改
8. 索引:每个存储到数组中的元素,都会⾃动拥有⼀个编号,这个编号从0开始,称为数组索引(index)
9. 索引范围[0,数组的长度-1]
10. 数组长度,每个数组都有⼀个长度,是固定的,java中给了数组的⼀个属性,可以获取数组的长度,数组名.length, 执⾏后可以得到数组的长度,是⼀个int类型的结果1. 数组的最⼤索引数,数组名.length-1
11. 数组遍历
        1. 就是把数组中的每⼀个元素都获取出来,这就是数组遍历
        2.
for (int i = 0; i < scores.length; i++) {
       System.out.println(scores[i]);
}
      3. 练习:在前边的基础之上,求出总成绩,最⾼成绩,最低成绩,平均成绩
import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int sum = 0, max = 0, min = 100;
        double avg;
        int[] scores = new int[5];
        for (int i = 0; i < 5; i++) {
            System.out.print("请输入第" + (i + 1) + "个学生的java成绩:");
            scores[i] = scanner.nextInt();
        }
        scanner.close();
        for (int j = 0; j < scores.length; j++) {
            System.out.println(scores[j]);
            sum += scores[j];
            if (max < scores[j]) {
                max = scores[j];
            }
            if (min > scores[j]) {
                min = scores[j];
            }


        }
        avg = sum / scores.length;
        System.out.println("总成绩:" + sum + ",最高成绩" + max + ",最低成绩" + min + ",平均成绩" + avg);

    }
}
12. 内存相关
      1. ⽅法区
          保存类的⼀些信息,常量,静态变量
       2. 堆
          保存的对象,如 数组对象, new创建的,都保存在堆内存中
       3. 栈
          1. java执⾏过程中,局部变量都会在⾥边,对象的引⽤,⽅法的执⾏,执⾏完后会⾃动释放(先进后出,后进先出),压栈,出栈
13. 数组引⽤数据类型
      1. 两个数组同⼀个引⽤

public class Demo01 {
    public static void main(String[] args) {
        int[] num1 = new int[]{1, 2, 3, 4, 5};
        int[] num2 = num1;
        System.out.println(num2);
        num2[0] =99;
        System.out.println(num1[0]);
        System.out.println(num2[0]);

    }
}
14. 练习
      1. 有两个数组,把两个数组合并
        1. {1,2,3,4,5}
        2. {7,8,9}
        3. {1,2,3,4,5,7,8,9}
   
public class Demo01 {
    public static void main(String[] args) {
        int[] num1 = new int[]{1, 2, 3, 4, 5};
        int[] num2 = new int[]{6,7,8,9};
        int[] num3 = new int[num1.length+num2.length];
        for (int i = 1; i < num1.length; i++) {
            num3[i] = num1[i];
        }
        for (int i = 0; i < num2.length; i++) {
            num3[i+num1.length] = num2[i];
        }
        for (int i = 0; i < num3.length; i++) {
            System.out.print(num3[i] + " ");
        }



    }
}

 

 
 
posted @ 2023-08-25 22:05  韩世康  阅读(20)  评论(0编辑  收藏  举报