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

Java静态引用非静态方法错误修复及双向链表实现验证

Hey there! Let's break down your two questions step by step:

1. Fixing the "cannot make static reference to non-static field" error

This error pops up when you try to call a non-static method (like your readData() method) directly from a static context (say, a main() method).

Static members belong to the class itself, not to any instance of the class. Non-static methods, on the other hand, require an instance of the class to run because they're tied to object state.

Two easy fixes:

  • Option 1: Create an instance of chainLadder first (this is the more object-oriented approach):
    Add a main() method to your class, then initialize the class and call readData() on the instance:
    public static void main(String[] args) {
        chainLadder ladderCalculator = new chainLadder();
        LinkedList<Object> data = ladderCalculator.readData();
        // Do something with your data here
    }
    
  • Option 2: Make readData() a static method (quick fix, but less ideal for OOP design):
    Just add the static keyword to the method signature:
    public static LinkedList<Object> readData() {
        // Your existing code here
    }
    
    Since your Scanner s is already static, this will work without other changes.
2. Is your "bidirectional linked list" implementation correct?

Short answer: No, not in the way you're thinking.

Java's built-in LinkedList is indeed implemented as a doubly linked list under the hood, but your current approach is just stuffing mixed data (strings, integers, symbols like > and <) into a single list. This doesn't create a structured bidirectional list of your year-cost data—it's just a flat sequence of unrelated elements that's hard to work with later.

What you should do instead:

You need to structure your data properly. Here are two practical approaches:

Approach 1: Use LinkedList with a custom data class (simple, leverages Java's built-in list)

First, create a class to encapsulate a year and its associated costs:

class YearCost {
    private String year;
    private List<Integer> costs;

    public YearCost(String year, List<Integer> costs) {
        this.year = year;
        this.costs = costs;
    }

    // Getters to access your data
    public String getYear() { return year; }
    public List<Integer> getCosts() { return costs; }
}

Then rewrite your readData() method to store YearCost objects instead of mixed values:

public LinkedList<YearCost> readData() {
    LinkedList<YearCost> yearCostList = new LinkedList<>();
    System.out.println("How many years are you calculating?");
    int numYears = s.nextInt();
    s.nextLine(); // Consume the leftover newline character

    for (int i = 1; i <= numYears; i++) {
        System.out.println("Please enter year " + i + ":");
        String year = s.nextLine();

        List<Integer> costs = new ArrayList<>();
        System.out.println("Enter costs for year " + year + "\n(Type -1 when done)");
        while (true) {
            int cost = s.nextInt();
            if (cost == -1) break;
            costs.add(cost);
            System.out.println("Next cost:");
        }
        s.nextLine(); // Consume newline after entering costs

        yearCostList.add(new YearCost(year, costs));
    }
    return yearCostList;
}

Now your LinkedList (a doubly linked list) holds structured objects, making it easy to traverse forward/backward and access year-cost data cleanly.

Approach 2: Build a custom doubly linked list (if you want to implement the logic yourself)

If you want to create your own doubly linked list from scratch, define a node class with prev and next pointers, then implement methods to add/remove/traverse nodes:

class YearCostNode {
    String year;
    List<Integer> costs;
    YearCostNode prev;
    YearCostNode next;

    public YearCostNode(String year, List<Integer> costs) {
        this.year = year;
        this.costs = costs;
        this.prev = null;
        this.next = null;
    }
}

// Then in your chainLadder class, manage the linked list:
public class chainLadder {
    static Scanner s = new Scanner(System.in);
    private YearCostNode head;
    private YearCostNode tail;

    public void addYearCost(String year, List<Integer> costs) {
        YearCostNode newNode = new YearCostNode(year, costs);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            tail.next = newNode;
            newNode.prev = tail;
            tail = newNode;
        }
    }

    // Add methods to traverse forward/backward, etc.
}

Quick bug fix in your original code:

In your while(numY != 0) loop, you're resetting int i = 1; every iteration—so you'll always print "year1" no matter how many years you enter. Move int i = 1; outside the loop to fix this.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 22:07:40