0
点赞
收藏
分享

微信扫一扫

【Java 数组的应用】

迪莉娅1979 2022-02-20 阅读 21

最值问题

package com.yuzhenc.array;

/**
 * @author: yuzhenc
 * @date: 2022-02-20 20:30:01
 * @desc: com.yuzhenc.array
 * @version: 1.0
 */
public class Test09 {
    public static void main(String[] args) {
        int[] arr = {55,85,12,34,69,78};
        System.out.println(getMaxValue(arr));//85
    }
    //定义一个方法,求给定int类型数组中元素的最大值
    public static int getMaxValue(int[] array){
        int maxValue = 0;
        for(int arr:array){
            maxValue = maxValue >= arr ? maxValue : arr;
        }
        return maxValue;
    }
}

查询问题

package com.yuzhenc.array;

/**
 * @author: yuzhenc
 * @date: 2022-02-20 20:38:06
 * @desc: com.yuzhenc.array
 * @version: 1.0
 */
public class Test10 {
    public static void main(String[] args) {
        int[] arr = {12,34,56,78,90,60,52};
        //查询下标对应的元素
        System.out.println(arr[2]);//56
        //查询元素在数组中的下标
        System.out.println(getIndex(arr,90));//4
    }
    //查询数组中指定元素的索引
    public static int getIndex(int[] array, int ele){
        int index = -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == ele){
                index = i;
                break;
            }
        }
        return index;
    }
}

添加元素

package com.yuzhenc.array;

import java.util.Arrays;

/**
 * @author: yuzhenc
 * @date: 2022-02-20 20:51:55
 * @desc: com.yuzhenc.array
 * @version: 1.0
 */
public class Test11 {
    public static void main(String[] args) {
        int[] arr = {10,80,90,45,62,385,156,412,769,999};
        System.out.println("增加元素前的数组:"+Arrays.toString(arr));//增加元素前的数组:[10, 80, 90, 45, 62, 385, 156, 412, 769, 999]
        insertEle(arr,5,1000);
        System.out.println("增加元素后的数组:"+Arrays.toString(arr));//增加元素后的数组:[10, 80, 90, 45, 62, 1000, 385, 156, 412, 769]
    }
    //定义一个方法,向数组的指定下标位置插入指定元素
    public static void insertEle(int[] array, int index, int element){
        if(index >= 0 && index < array.length){
            for (int i = array.length-1; i >= index+1 ; i--) {
                array[i] = array[i-1];
            }
            array[index] = element;
        }
    }
}

删除元素

package com.yuzhenc.array;

import java.util.Arrays;

/**
 * @author: yuzhenc
 * @date: 2022-02-20 21:06:09
 * @desc: com.yuzhenc.array
 * @version: 1.0
 */
public class Test12 {
    public static void main(String[] args) {
        int[] arr = {12,34,56,78,95,65,48,23,100,521,250};
        System.out.println("删除前的数组:"+Arrays.toString(arr));//删除前的数组:[12, 34, 56, 78, 95, 65, 48, 23, 100, 521, 250]
        int deleteElement = deleteEle(arr,5);
        System.out.println("删除的元素:"+deleteElement);//删除的元素:65
        System.out.println("删除指定下标元素后的数组:"+Arrays.toString(arr));//删除指定下标元素后的数组:[12, 34, 56, 78, 95, 48, 23, 100, 521, 250, 0]
        //删除指定元素
        deleteElement = deleteEle(arr,getIndex(arr,521));
        System.out.println("删除的元素:"+deleteElement);//删除的元素:521
        System.out.println("删除指定元素后的数组:"+Arrays.toString(arr));//删除指定元素后的数组:[12, 34, 56, 78, 95, 48, 23, 100, 250, 0, 0]
    }
    //定义一个方法,删除指定位置上的元素,后面的元素前移,返回删除的元素
    public static int deleteEle(int[] array, int index){
        int element = -1;
        if(index >= 0 && index < array.length){
            element = array[index];
            for (int i = index; i < array.length-1; i++) {
                array[i] = array[i+1];
            }
            array[array.length-1] = 0;
        }
        return element;
    }

    //查询数组中指定元素的索引
    public static int getIndex(int[] array, int ele){
        int index = -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == ele){
                index = i;
                break;
            }
        }
        return index;
    }
}
举报

相关推荐

0 条评论