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

为何ArrayList首个数字后出现字母"D"?Java去重程序问题咨询

解决首个数字后附带"D"的问题

Hey there! Let's figure out why that random "D" is tagging along after your first input number—this kind of type-related quirk is super common when mixing number types in Java, so let’s break down the most likely causes and fixes:

最可能的成因

1. 首个数字被错误存储为Double类型

The "D" is almost certainly a type identifier for a Double object, not part of your actual input. Here’s how this usually happens:

  • You might have accidentally used Scanner.nextDouble() to read your first number instead of nextInt(), then stored that Double value directly in your ArrayList (instead of casting it to an int first).
  • If your ArrayList uses a loose generic type like ArrayList<Object> or ArrayList<Number> instead of ArrayList<Integer>, Java lets you mix Integer and Double values. When you print or debug the list, Double values will show up with a "D" suffix (or .0 in plain console output) to distinguish them from Integers.

2. 调试视图的类型标识

If you’re seeing the "D" in your IDE’s debug console (not the regular output), that’s just your IDE’s way of showing you the data type of the value. For example, IntelliJ or Eclipse will label Double values with "D" to let you know it’s not an Integer, even if the numeric value looks like a whole number.

快速排查&修复步骤

  • Check your ArrayList’s generic type: Make sure it’s explicitly ArrayList<Integer> instead of a generic Object or Number. This will force the compiler to catch accidental type mismatches before they cause issues.
  • Standardize input reading: Use Scanner.nextInt() for all integer inputs—don’t mix nextDouble() and nextInt() unless you intentionally need to handle decimals.
  • Verify element addition: Double-check that you’re adding int or Integer values to the list, not Double objects. For example, if you did read a Double by mistake, cast it to int first: list.add((int) doubleValue);

错误代码示例(导致问题的常见写法)

import java.util.ArrayList;
import java.util.Scanner;

public class DuplicateRemover {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Object> numberList = new ArrayList<>(); // 泛型未指定为Integer
        
        // 首个数字误用nextDouble读取
        double firstInput = sc.nextDouble();
        numberList.add(firstInput); // 直接存储Double对象
        
        // 后续数字用nextInt读取
        while (sc.hasNextInt()) {
            int num = sc.nextInt();
            numberList.add(num);
        }
        
        // 去重逻辑...
        
        System.out.println(numberList);
    }
}

修复后的代码片段

import java.util.ArrayList;
import java.util.Scanner;

public class DuplicateRemover {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> numberList = new ArrayList<>(); // 指定Integer泛型
        
        // 统一用nextInt读取所有输入
        while (sc.hasNextInt()) {
            int num = sc.nextInt();
            numberList.add(num);
        }
        
        // 去重逻辑...
        
        System.out.println(numberList);
    }
}

If you can share a snippet of your actual code, we can pinpoint the exact line causing the issue—but these steps should fix the vast majority of cases where a "D" is appearing unexpectedly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:00:09