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

Java自定义链表队列不通过add方法输出元素及打印偶数问题

解决方案

问题核心

  • 当前SimpleLinkedListQueue2仅实现了基础的出队、入队、取队首接口,没有提供不修改队列结构的遍历能力
  • add方法内置了打印逻辑,无法通过复制元素触发入队的方式做遍历,会产生重复打印
  • 现有代码中将main类作为非静态内部类定义在SimpleLinkedListQueue2内部,无法直接运行,建议调整main方法的位置。

方案1(推荐):修改SimpleLinkedListQueue2类新增遍历方法

在SimpleLinkedListQueue2类中新增不修改队列结构的遍历方法,直接访问内部链表节点完成筛选:

// 新增到SimpleLinkedListQueue2类中
// 方法1:直接打印队列中的偶数
public void printEvenElements() {
    ListNode<T> current = head;
    while (current != null) {
        if (current.value instanceof Integer) {
            int num = (Integer) current.value;
            if (num % 2 == 0) {
                System.out.println(num);
            }
        }
        current = current.next;
    }
}

// 方法2:通用获取所有元素,可外部自定义筛选逻辑
public List<T> getAllElements() {
    List<T> elements = new ArrayList<>();
    ListNode<T> current = head;
    while (current != null) {
        elements.add(current.value);
        current = current.next;
    }
    return elements;
}

修改后main方法调用示例:

public static void main(String[] args) {
    SimpleLinkedListQueue2<Integer> queue2 = new SimpleLinkedListQueue2<>();
    queue2.add(67);
    queue2.add(54);
    queue2.add(75);
    queue2.add(100);
    System.out.println("--------------");
    // 直接调用打印偶数方法
    queue2.printEvenElements();
    // 通用方法自定义筛选写法
    // queue2.getAllElements().stream().filter(num -> num%2 == 0).forEach(System.out::println);
}

运行后输出:

67
54
75
100
--------------
54
100

方案2:不修改原有队列类,通过反射遍历

如果无法修改SimpleLinkedListQueue2的源码,可以通过反射获取私有内部节点完成遍历:

import java.lang.reflect.Field;

public class Main {
    public static void main(String[] args) throws Exception {
        SimpleLinkedListQueue2<Integer> queue2 = new SimpleLinkedListQueue2<>();
        queue2.add(67);
        queue2.add(54);
        queue2.add(75);
        queue2.add(100);
        System.out.println("--------------");
        printEvenByReflection(queue2);
    }

    private static void printEvenByReflection(SimpleLinkedListQueue2<Integer> queue) throws Exception {
        // 反射获取head字段
        Field headField = SimpleLinkedListQueue2.class.getDeclaredField("head");
        headField.setAccessible(true);
        Object currentNode = headField.get(queue);
        if (currentNode == null) return;

        // 获取节点类的value、next字段
        Class<?> nodeClazz = currentNode.getClass();
        Field valueField = nodeClazz.getDeclaredField("value");
        Field nextField = nodeClazz.getDeclaredField("next");
        valueField.setAccessible(true);
        nextField.setAccessible(true);

        // 遍历筛选偶数
        while (currentNode != null) {
            Integer num = (Integer) valueField.get(currentNode);
            if (num % 2 == 0) {
                System.out.println(num);
            }
            currentNode = nextField.get(currentNode);
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 22:54:00