java基础 数组

 

 

 

 

 

 

public static void main(String[] args) {
        
        int[] nums = {1,2,3,9};
        
        //求数组最大值
        int maxN = nums[0];
        for(int i =1;i<nums.length;i++) {
            if (maxN < nums[i]) {
                maxN = nums[i];
            }
        }
        System.out.println("最大值:"+maxN);
        
    }

 

 

public static void main(String[] args) {
        
        int[] nums = {1,2,3,9};
        
        //数组的复制
        int[] cpNums = new int[nums.length];
        for(int i =0;i<cpNums.length;i++) {
            cpNums[i] = nums[i];
        }
        System.out.println(Arrays.toString(cpNums));
    }

 

 

 

public static void main(String[] args) {

        int[] nums = { 1, 2, 3, 9 };

        // 数组的反转
        int[] reversNums = new int[nums.length];
        for (int i = nums.length - 1; i >= 0; i--) {
            reversNums[nums.length - 1 - i] = nums[i];
        }
        System.out.println(Arrays.toString(reversNums));

        //方式2
        for (int i = 0, j = nums.length - 1; i < nums.length / 2; i++, j--) {
            //交换            
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
        
        System.out.println(Arrays.toString(nums));
    }

 

 

 

public static void main(String[] args) {

        int[] nums = { 1, 2, 3, 9 };

        // 线性查找
        int findN = 3;
        int index = -1;
        for (int i = 0; i < nums.length; i++) {
            if (findN == nums[i]) {
                index = i;
                break;
            }
        }

    }

 

 

 

posted @ 2021-02-20 08:32  AngDH  阅读(39)  评论(0编辑  收藏  举报