JAVA/编程语言

[上]JAVA学习系列模块五第二章103.数组操作_存储数据

daimafengzi · 5月26日 · 2024年 · · 本文共1072个字 · 预计阅读4分钟1054次已读

[上]JAVA学习系列模块五第二章103.数组操作_存储数据

视频


笔记

存储元素

1.格式:
数组名[索引值] = 值 -> 将等号右边的值放到数组指定的索引位置上

public class Demo02Array {
    public static void main(String[] args) {
        int[] arr = new int[3];
        arr[0] = 100;//将100存到了arr这个数组的0索引上
        arr[1] = 200;//将200存到了arr这个数组的1索引上
        arr[2] = 300;//将300存到了arr这个数组的2索引上
        //arr[3] = 1000;

        System.out.println("============================");
        
        String[] arr1 = new String[3];
        arr1[0] = "东方不败";
        arr1[1] = "岳不群";
        arr1[2] = "林平之";
    }
}
public class Demo03Array {
    public static void main(String[] args) {
        //键盘录入三个整数,存储到数组中
        int[] arr = new int[3];

        Scanner sc = new Scanner(System.in);

        /*
          先看等号右边的,先录入,将录入的数据保存到指定的索引位置上
         */
      /*  arr[0] = sc.nextInt();
        arr[1] = sc.nextInt();
        arr[2] = sc.nextInt();*/

       /* for (int i = 0; i < 3; i++) {
            arr[i] = sc.nextInt();
        }*/
        
        for (int i = 0; i < arr.length; i++) {
            arr[i] = sc.nextInt();
        }
        
    }
}
public class Demo04Array {
    public static void main(String[] args) {
        //定义一个长度为3的数组
        int[] arr = new int[3];
        Random rd = new Random();

        //arr[0] = rd.nextInt(10);
        //arr[1] = rd.nextInt(10);
        //arr[2] = rd.nextInt(10);

        for (int i = 0;i
0 条回应
Copyright © 2022-2024 LuoWeiHua
| 耗时 0.355 秒 | 查询 58 次 | 内存 4.15 MB |