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

Java链表类方法结构修复求助:输出异常问题排查

手动实现LinkedList方法与ArrayList输出不符的问题排查

测试输出对比

以下是测试场景及ArrayList与自定义LinkedList的输出差异:

  • 插入课程451到索引4位置
    • ArrayList:大小6,数据131 132 215 315 451 415
    • LinkedList:大小6,数据131 132 215 315 451 451
  • 从列表中移除索引3的课程
    • ArrayList:大小5,数据131 132 215 451 415
    • LinkedList:大小5,数据131 132 215 315 451
  • 从列表中移除课程415
    • ArrayList:大小4,数据131 132 215 451
    • LinkedList:大小5,数据131 132 215 315 451
  • 替换课程131、132为121、122
    • ArrayList:数据121 122 215 451
    • LinkedList:数据121 122 215 315 451
  • 查找'451'的索引
    • ArrayList:3
    • LinkedList:4
  • 存在性检查
    • '215':ArrayList(true),LinkedList(true)
    • '315':ArrayList(false),LinkedList(true)

自定义LinkedList代码

public class LinkedList{
// linked list fields
private Node head;
private int size;

/**
 * Default constructor
 */
public LinkedList(){
    head = null;
    size = 0;
}


public Object get(int pos){
    // error for if the position is less than zero or greater 
    // than the size
    if (pos < 0 || (pos >= size)) {
        throw new IndexOutOfBoundsException();
    }

    Node temp = head;
    for (int i = 0; i < pos; i++){
        // if the next node is null and return current data
        if (temp.next == null){
            return temp.data;
        } else {
            //System.out.println(temp.data);
            temp = temp.next;
        }
    }
    // gets the value of that stored data in that position
    return temp.data;
}


public void clear(){
    for (int k = 0; k < size; k++) {
        head = null;
    }
    size = 0;
}

public boolean set(int pos, Object obj){
    if (pos < 0 || (pos >= size)){
        throw new IndexOutOfBoundsException();
    }
    Node temp = head;
    // loops through list until it gets to desired position
    for (int i = 0; i < pos; i++){
        temp = temp.next;
    }
    // the original data is being updated with the new
    // data(elem)
    temp.data = obj;
    // successfully updated the data
    return true;
}


public int indexOf(Object obj){
    for (int k = 0; k < size(); k++){
        Object elem = get(k);
        if (elem.equals(obj)){
            // found item; return its position
            return k;
        }
    }
    // item not found
    return -1;
}

public Object remove(int pos){
    if (pos < 0 || pos >= size){
        throw new IndexOutOfBoundsException();
    }
    // stores the element to be removed
    Node temp = head;
    // loops through list until it gets to desired position
    for (int i = 0; i < pos; i++){
        // if the next node is null and return current data
        if (temp.next == null){
            return temp.data;
        }
        temp = temp.next;
    }
    // subtracts size
    size--;

    return temp.data;
}

public boolean add(int pos, Object obj){
    if (pos < 0 || pos >= size) {
        throw new IndexOutOfBoundsException();
    }
    Node temp = head;
    // loops through list until it gets to desired position
    for (int i = 0; i < pos; i++){
        // if the next node is null and return current data
        if (temp.next == null){
            return false;
        }
        temp = temp.next;
    }
    temp.data = obj;
    size++;
    return true;
}

错误点分析与修复

1. add(int pos, Object obj) 方法错误

当前实现仅替换了指定位置节点的data,未创建新节点插入,导致原有后续节点被覆盖,同时size错误递增。
修复代码:

public boolean add(int pos, Object obj) {
    if (pos < 0 || pos > size) { // 允许pos等于size(追加到末尾)
        throw new IndexOutOfBoundsException();
    }
    Node newNode = new Node(obj);
    if (pos == 0) {
        // 插入头部
        newNode.next = head;
        head = newNode;
    } else {
        Node temp = head;
        // 定位到目标位置的前一个节点
        for (int i = 0; i < pos - 1; i++) {
            temp = temp.next;
        }
        newNode.next = temp.next;
        temp.next = newNode;
    }
    size++;
    return true;
}

2. remove(int pos) 方法错误

当前实现仅返回目标节点的data并减少size,未调整链表节点的引用关系,导致节点并未真正被移除。
修复代码:

public Object remove(int pos) {
    if (pos < 0 || pos >= size) {
        throw new IndexOutOfBoundsException();
    }
    Node removedNode;
    if (pos == 0) {
        // 移除头部节点
        removedNode = head;
        head = head.next;
    } else {
        Node temp = head;
        // 定位到目标位置的前一个节点
        for (int i = 0; i < pos - 1; i++) {
            temp = temp.next;
        }
        removedNode = temp.next;
        temp.next = removedNode.next;
    }
    size--;
    return removedNode.data;
}

3. clear() 方法冗余

循环多次设置head为null无意义,直接重置head和size即可。
修复代码:

public void clear() {
    head = null;
    size = 0;
}

4. 缺失remove(Object obj) 方法

测试中存在移除指定元素的场景,但当前类未实现该方法,导致元素无法被正确移除。
补充实现:

public boolean remove(Object obj) {
    if (head == null) {
        return false;
    }
    // 移除头部节点的情况
    if (head.data.equals(obj)) {
        head = head.next;
        size--;
        return true;
    }
    Node temp = head;
    // 查找目标元素的前一个节点
    while (temp.next != null && !temp.next.data.equals(obj)) {
        temp = temp.next;
    }
    if (temp.next != null) {
        temp.next = temp.next.next;
        size--;
        return true;
    }
    return false;
}

5. get() 方法冗余判断

已通过pos >= size做边界检查,无需额外判断temp.next == null,可简化代码。
修复代码:

public Object get(int pos) {
    if (pos < 0 || pos >= size) {
        throw new IndexOutOfBoundsException();
    }
    Node temp = head;
    for (int i = 0; i < pos; i++) {
        temp = temp.next;
    }
    return temp.data;
}

6. 缺失内部Node类定义

代码中使用了Node类但未给出定义,需补充内部类:

private class Node {
    Object data;
    Node next;

    Node(Object data) {
        this.data = data;
        this.next = null;
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 04:05:54