zoukankan      html  css  js  c++  java
  • java 数组2

    一、创建异常

    1、空指针异常

    2、超出索引范围

    二、遍历

    for循环

    三、求数组中的最大值

    package cn.wt.day05.demon02;
    
    public class DemonArray03 {
        public static void main(String[] args) {
            int[] array = {10, 2, 3, 7, 9, 8, 3, 4, 20};
            int max = array[0];
            for (int i = 1; i < array.length; i++) {
                if (array[i] > max){
                    max = array[i];
                }
            }
            System.out.println(max);
    
        }
    }

    四、反转数组的数据

    1、方法一,通过另一个数组

    package cn.wt.day05.demon02;
    
    public class DemonArray04 {
        public static void main(String[] args) {
            int[] arr1 = {2, 1, 5, 3};
            int[] arr2 = new int[arr1.length];
            for (int i = 0; i < arr1.length; i++) {
                arr2[arr1.length-1-i] = arr1[i];
            }
            for (int i = 0; i < arr2.length; i++) {
                System.out.println(arr2[i]);
            }
        }
    }

    2、方法二,通过中间值

    package cn.wt.day05.demon02;
    
    public class DemonArray05 {
        public static void main(String[] args) {
            int[] arr = {2, 4, 1, 8, 5, 0};
            int temp;
            for (int i = 0; i < (int)arr.length/2; i++) {
                temp = arr[i];
                arr[i] = arr[arr.length-1-i];
                arr[arr.length-1-i] = temp;
            }
    
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
    }

    五、数组作为 方法参数

    package cn.wt.day05.demon02;
    
    public class DemonArray06 {
        public static void main(String[] args) {
            int[] array = {1, 5, 2, 7, 3, 5, 0};
            printArray(array);
        }
    
        public static void printArray(int[] arr){
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
    }

    注意:python中有默认参数陷阱

    六、数组 作为返回值

    python 通过 tuple 直接返回

    package cn.wt.day05.demon02;
    
    public class DemonArray07 {
        public static void main(String[] args) {
            int[] result = isCal(10, 20);
            System.out.println(result[1]);
        }
    
        public static int[] isCal(int a, int b){
            int isSum = a + b;
            int isMax = a > b ? a:b;
            int isAvg = (a+b)/2;
            int[] total = {isSum, isMax, isAvg};
            return total;
        }
    }

    总结:

    1、数据类型相同,运行过程中长度不可改变

    2、参数、返回值 都是内存地址

    3、异常:空指针、超出索引范围

    4、遍历,最值,反转

  • 相关阅读:
    如何下载Bilibili视频
    pyqt5 主界面打开新主界面、打开Dialog、打开提示框的实现模板
    【爬坑】python3+pyqt5+pyinstaller 打包成exe的各种问题
    【PyQt5-Qt Designer】QComboBox(下拉列表框) 使用模板
    【PyQt5-Qt Designer】读取txt文件在打印
    pyqt5核心-信号与槽(第二弹)
    使用QT设计师-信号和槽signal-slot(第一弹)
    【python基础】python程序打包成.exe运行时会弹出黑框
    【pyqt5】QdateTimeEdit(日期时间)
    pyqt5-对文本样式进行操作
  • 原文地址:https://www.cnblogs.com/wt7018/p/12173811.html
Copyright © 2011-2022 走看看