java中一維數組的定義和遍歷


public class ArrayDemo1{
    public static void main(String[] args){
        //1 定義數組 並同時賦值
        int[] arr = new int[]{3,9,5,8,2};
        //                            4
        System.out.println(arr.length);
        System.out.println(arr[arr.length-1]);
        System.out.println("=========================");
        //2定義數組 直接用大括號賦值
        int[] arr1 = {22,56,89};
        System.out.println(arr1[arr1.length-1]);
        // 3 為數組賦值
        arr1[2] = 111;
        System.out.println(arr1[arr1.length-1]);
    }
}


public class ArrayDemo2{
    public static void main(String[] args){
        //1 定義數組 並同時賦值
        int[] arr = {3,9,5,8,2,87};
        int max=arr[0];
        for(int i=0;i<=5;i++){
            //遍歷
            //System.out.println(arr[i]);
            if(max<arr[i]){
                max=arr[i];
            }
        }
        System.out.println(max);
        
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM