Implementation of adding, deleting and deduplicating an int[] array in Java

illustrate

Here we record the use of int[] array to add, delete and deduplicate the array to better understand the collection class ideas in Java. Such as ArrayList, Set and other collections.

code

Implementation of deleting a certain data in the array based on index

package com.example.test;

import java.util.Arrays;


public class TestMain {
    
    

    public static void main(String[] args) {
    
    
        deleteList();
    }

    //根据索引删除数组中的某个数据实现
    public static void deleteList(){
    
    
        //删除数组原理,从索引位置开始将后面的元素向前移到一位,这就导致最后一位元素会出现2次,所以要新建一个数组,长度比原数组少一,然后将原数组的数据从索引0开始,到length-1复制到新数组里面实现元素删除
        int[] nums={
    
    34,35,36,37,38,39};
        //删除索引为2的元素,定义一个元素索引为2的下标
        int index=2;
        for(int i=index;i<nums.length-1;i++){
    
    
            //将后一个元素值赋值给前面一个元素
            nums[i]=nums[i+1];
        }
        System.out.println("数据删除后原数组数据:"+Arrays.toString(nums));
        //定义一个新数组,数组长度为nums1.length-1
        int[] newArray=new int[nums.length-1];
        //将原数组数据复制到新数组里面,最后面一位数据不要,从而实现元素的删除功能
        for(int i=0;i<newArray.length;i++){
    
    
            newArray[i]=nums[i];
        }
        System.out.println("新数组数据:"+Arrays.toString(newArray));
    }
}

The execution results are as follows:
Insert image description here

Add an element to the array

package com.example.test;

import java.util.Arrays;


public class TestMain {
    
    

    public static void main(String[] args) {
    
    
        addList();
    }

    //在数组中添加一个元素实现
    public static void addList(){
    
    
        //添加数组原理,先新建一个数组,这个数组长度为原数组的长度+1,将原数组数据复制到新数组里面,在新数组里面最后面直接添加元素
        int[] nums={
    
    34,35,36,37,38,39};
        //添加元素值,将40添加到数组里面
        int num=40;
        //定义一个新数组,数组长度为nums1.length+1
        int[] newArray=new int[nums.length+1];
        //将原数组数据复制到新数组里面
        for(int i=0;i<nums.length;i++){
    
    
            newArray[i]=nums[i];
        }
        //直接将新元素添加到最后段
        newArray[newArray.length-1]=num;
        System.out.println("新数组数据:"+Arrays.toString(newArray));
    }
}

The execution results are as follows:
Insert image description here

Remove duplicate data from an array

package com.example.test;

import java.util.Arrays;


public class TestMain {
    
    

    public static void main(String[] args) {
    
    
        deleteRepeatList();
    }

    //删除数组里面的重复数据
    public static void deleteRepeatList(){
    
    
        int[] nums={
    
    34,35,36,36,37,38,39,34,36,39};
        //新建一个数组用来放未重复的元素,因为不知道重复的元素个数,所以数组长度与原数组长度一致
        int[] newArray=new int[nums.length];
        int index=0;
        for(int i=0;i<nums.length;i++){
    
    
            for(int j=0;j<nums.length;j++){
    
    
                //元素的索引下标不能相等,同一个元素判断没有意义
                if(i!=j){
    
    
                    //如果后一个元素与前面的元素相等,直接结束循环
                    if(nums[i]==nums[j]){
    
    
                        //System.out.println("nums[i]="+nums[i]+",nums[j]="+nums[j]);
                        break;
                    }
                }
                //遍历结束,且没有重复元素,将这个元素放到新数组里面
                if (j==newArray.length-1){
    
    
                    newArray[index]=nums[i];
                    index++;
                }
            }
        }
        System.out.println("移除重复元素后的数组值="+Arrays.toString(newArray));
        System.out.println("index="+index);
        //定义一个准确长度的数组,用来存放未重复的数组,长度为index
        int[] newArr=new int[index];
        for(int i=0;i<newArr.length;i++){
    
    
            newArr[i]=newArray[i];
        }
        System.out.println("移除数据0后的数组值="+Arrays.toString(newArr));
    }
}

Results of the:
Insert image description here

Remove duplicate data from the array and keep only one

package com.example.test;

import java.util.Arrays;


public class TestMain {
    
    

    public static void main(String[] args) {
    
    
        distinctList();
    }

    //去掉数组里面的重复数据,只保留一个,顺序不能保证,时间复杂度为O(n^2)
    public static void distinctList(){
    
    
        int[] nums={
    
    34,35,36,36,37,38,39,34,36,39};
        //声明一个变量存原数组长度
        int len=nums.length;
        for(int i=0;i<len;i++){
    
    
            //从i的下一个元素开始遍历
            for(int j=i+1;j<len;j++){
    
    
                //如果后一个元素与前面的元素相等
                if(nums[i]==nums[j]){
    
    
                    //那么就将数组的最后一个元素值赋值给当前重复的元素
                    nums[j]=nums[len-1];
                    //将数组长度减一,因为最后一个元素已经赋值给了当前重复的元素值,所以最后一个元素没有存在的必要了
                    len--;
                    //j自减一,让for循环加一后再从j当前重复的元素位置开始遍历
                    j--;
                }
            }
        }
        //声明一个新数组用来存放不重复的元素
        int[] newArr = new int[len];
        for (int i = 0; i < len; i++) {
    
    
            newArr[i] = nums[i];
        }
        System.out.println("去重后的数组值="+Arrays.toString(newArr));
    }
}

Results of the:
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_48040732/article/details/132363219