You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在数组中实现重复元素置顶、最新输入优先的插入逻辑?

数组重复元素置顶功能实现完善

需求回顾

实现数组功能需满足以下规则:

  • 用户输入数字时,若该数字已存在于数组中,将其移至数组顶部(作为最新输入优先),其余元素依次下移
  • 若数组已满,新输入的非重复元素直接置于顶部,其余元素后移
  • 示例:原数组为[4,5,6,7,9],输入7后应变为[7,4,5,6,9]

完善后的代码

import java.util.Scanner;

public class ArrayTopPriority {
    public static void main(String[] args) {
        // 初始化固定大小的数组
        int intArray[] = new int[5]; 
        // 记录数组中已存储的元素数量
        int count = 0;
        // 用户输入值
        int userInput = 0;
        Scanner in = new Scanner(System.in);

        // 首次输入提示
        System.out.println("Enter The File You Wish To Use. Enter 0 to stop:");
        userInput = in.nextInt();

        // 校验输入合法性(0-10之间)
        while((userInput < 0) || (userInput > 10)){
            System.out.println("Invalid number. Must be between 1 - 10\n");
            System.out.println("Enter The File You Wish To Use. Enter 0 to stop:");
            userInput = in.nextInt();
        }

        // 处理首次输入(非0情况)
        if (userInput != 0) {
            intArray[count] = userInput;
            count++;
        }

        while(userInput != 0){
            // 打印当前数组状态
            System.out.println("There is currently " + count + " On The Table.");
            for(int i = 0; i < count; i++){
                System.out.print(intArray[i] + " ");
            }
            System.out.print("\n\n");

            // 提示用户输入
            System.out.println("Enter The File You Wish To Use. Enter 0 to stop:");
            userInput = in.nextInt();

            // 输入合法性校验
            while(userInput < 0 || userInput > 10){
                System.out.println("Invalid number.\n");
                System.out.println("Enter The File You Wish To Use. Enter 0 to stop:");
                userInput = in.nextInt();
            }

            // 输入为0时退出循环
            if (userInput == 0) {
                break;
            }

            // 检查输入值是否已存在于数组中
            int existingIndex = -1;
            for (int i = 0; i < count; i++) {
                if (intArray[i] == userInput) {
                    existingIndex = i;
                    break;
                }
            }

            if (existingIndex != -1) {
                // 存在重复元素,将其移至顶部
                int temp = intArray[existingIndex];
                // 将该元素之前的所有元素后移一位
                for (int i = existingIndex; i > 0; i--) {
                    intArray[i] = intArray[i - 1];
                }
                intArray[0] = temp;
            } else {
                // 元素不存在,处理数组满或未满情况
                if(count == intArray.length){
                    // 数组已满,将所有元素后移一位,新元素放顶部
                    for(int i = (intArray.length - 1); i > 0; i--){
                        intArray[i] = intArray[i - 1];
                    }
                    intArray[0] = userInput;
                } else {
                    // 数组未满,先将现有元素后移一位,新元素放顶部
                    for (int i = count; i > 0; i--) {
                        intArray[i] = intArray[i - 1];
                    }
                    intArray[0] = userInput;
                    count++;
                }
            }
        }
        System.out.println("The end.");
        in.close();
    }
}

关键修改说明

  • 重复元素检测:遍历数组查找输入值的索引,若找到则执行置顶逻辑
  • 置顶逻辑实现:保存重复元素的值,将该元素位置之前的所有元素依次后移一位,最后把重复元素放到数组头部
  • 数组未满时的新元素处理:原代码是直接追加到数组末尾,现在改为将现有元素后移,新元素放到顶部,符合“最新输入优先”的规则
  • 边界处理优化:添加了输入为0时的直接退出逻辑,避免无效处理,同时关闭Scanner资源防止泄漏

内容的提问来源于stack exchange,提问作者PrnceAkeem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 23:42:51