Java静态引用非静态方法错误修复及双向链表实现验证
Hey there! Let's break down your two questions step by step:
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
chainLadderfirst (this is the more object-oriented approach):
Add amain()method to your class, then initialize the class and callreadData()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 thestatickeyword to the method signature:
Since yourpublic static LinkedList<Object> readData() { // Your existing code here }Scanner sis already static, this will work without other changes.
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

