Java Study Notes 07 Multidimensional Arrays and Program Calls

Multidimensional Arrays

Random rand = new Random();
        //定义int数组,从一维到三维
        int[] a1 = new int[5];
        int[][] a2 = new int[5][5];
        int[][][] a3 = new int[5][5][5];
        //为生成的数组赋随机值
        for (int i = 0; i < a1.length; i++) {
    
    
            a1[i] = rand.nextInt(1, 101);
        }
        for (int i = 0; i < a2.length; i++) {
    
    
            for (int j = 0; j < a2[i].length; j++) {
    
    
                a2[i][j] = rand.nextInt(1, 101);
            }
        }
        for (int i = 0; i < a3.length; i++) {
    
    
            for (int j = 0; j < a3[i].length; j++) {
    
    
                for (int k = 0; k < a3[i][j].length; k++) {
    
    
                    a3[i][j][k] = rand.nextInt(1, 101);
                }
            }
        }
        //输出数组
        System.out.println(Arrays.toString(a1));
        System.out.println(Arrays.deepToString(a2));
        System.out.println(Arrays.deepToString(a3));

        for (int i = 0; i < a3.length; i++) {
    
    
            System.out.println(Arrays.deepToString(a3[i]));
        }
        Integer[] n1 = {
    
    1, 2, 3};
        System.out.println(n1);
        //数组转换为List
        List<Integer> ints = Arrays.asList(n1);
        System.out.println(ints);

        //asList() 返回List
        List<Integer> list = Arrays.asList(1, 2, 3);
        System.out.println(list);

        int[] n2 = new int[6];
        System.out.println(Arrays.toString(n2));
        Arrays.fill(n2, 9);
        Arrays.fill(n2, 1, 4, 6);
        System.out.println(Arrays.toString(n2));

insert image description here
operation result. Among them, Arrays.fill(n2, 1, 4, 6); means to change the value to 6 at the subscripts of 1, 2, and 3 in the n2 array.

Write a message to calculate the time function to realize the data call between methods.
To build a class
public is the access modifier, String is the return value, followed by the method name, and the parameters in parentheses are passed.

public class TimeLiuYan {
    
    
    public String timeliuyan(int a1, int a2, int a3, int a4, int a5, int a6) {
    
    

        Calendar cal = Calendar.getInstance();
        //获取传递来的时间
        cal.set(a1, a2, a3, a4, a5, a6);
        //定义格式化时间的形式
        SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String ms = "时间是:";
        //留言时间,将时间转化为时间戳
        long t1 = cal.getTimeInMillis();
        long t2 = System.currentTimeMillis();
        int ts = (int) ((t2 - t1) / (1000));
        int tm = (int) ((t2 - t1) / (1000 * 60));
        int th = (int) ((t2 - t1) / (1000 * 60 * 60));
        int td = (int) ((t2 - t1) / (1000 * 60 * 60 * 24));
        if (ts <= 60) {
    
    
            System.out.print("刚刚:" + ms + "%n");
            ms = ms + "刚刚";
        } else if (tm >= 1 && tm < 60) {
    
    
            System.out.printf("%d分钟前:" + ms + "%n", tm);
            ms = ms + tm + "分钟前";
        } else if (th >= 1 && th < 24) {
    
    
            System.out.printf("%d小时前:" + ms + "%n", th);
            ms = ms + th + "小时前";
        } else if (td >= 1 && td < 7) {
    
    
            ms = ms + td + "天前";
        } else {
    
    
            ms = simp.format(t1);
        }
        return ms;
    }

    public String timeliuyan() {
    
    
        return "";
    }
}

Write a main method to call this class

public static void main(String[] args) {
    
    
        TimeLiuYan t = new TimeLiuYan();
        Scanner scan = new Scanner(System.in);
        System.out.print("请输入年");
        int a1 = scan.nextInt();
        System.out.print("请输入月");
        int a2 = scan.nextInt();
        a2 = a2 - 1;
        System.out.print("请输入日");
        int a3 = scan.nextInt();
        System.out.print("请输入时");
        int a4 = scan.nextInt();
        System.out.print("请输入分");
        int a5 = scan.nextInt();
        System.out.print("请输入秒");
        int a6 = scan.nextInt();
        System.out.println(t.timeliuyan(a1, a2, a3, a4, a5, a6));

    }

Guess you like

Origin blog.csdn.net/xxxmou/article/details/129049118