如何实现向LinkedList插入Ball对象的方法?程序异常求助
问题描述
尝试从文件读取数据创建并打印包含Ball对象的LinkedList,原本打算通过给head赋值插入第一个节点,但程序输出00,说明对象未正确创建,不清楚问题出在哪。
主类代码
class test{ public static void main(String[] args){ // 不允许修改main方法 String inputfilename = "input.txt"; String outputfilename= "output.txt"; BallLinkedList ballSet = new BallLinkedList("", 0, 0); constructLLFromFile(inputfilename, ballSet); ballSet.printLL(); System.out.println(); ballSet.printLL(); } static BallLinkedList constructLLFromFile(String theInputFile, BallLinkedList ballSet){ System.out.println("Constructing the linked list from "+theInputFile); BallLinkedList head = null; try{ File file = new File(theInputFile); Scanner scan = new Scanner(file); int data = 0; int index = 0; while(scan.hasNext()){ data = Integer.parseInt(scan.nextLine()); String color = scan.nextLine(); long diamater = Long.parseLong(scan.nextLine()); int bounce = Integer.parseInt(scan.nextLine()); Ball insertee = newBall(color,diamater,bounce); ballSet.insert(insertee,index); index++; } }catch(Exception eee){ } return null; // 不能修改 } }
LinkedList类代码
import java.io.FileWriter; class BallLinkedList{ private Planet head; BallLinkedList(){ } BallLinkedList(Ball initial){ head=initial; } BallLinkedList(String c, long d, int b){ head = new Planet(c, d, b); } // 插入方法位置 void insert(Ball insertee, int pos){ if(head == null ){ head = insertee; }else{ head = head.next; pos++; } } // 打印链表 void printLL(){ while(head != null ){ System.out.println(head.getColor()); System.out.println(head.getDiameter()); System.out.println(head.getBounce()); head = head.next; } } }
Ball类代码
class Ball{ private String color; private long diameter; private int bounce; Ball next; Ball(String c, long d, int b){ color = n; diameter = d; bounce = m; } String getColor(){ return color; } long getDiameter(){ return diameter; } int getBounce(){ return bounce; } public String toString(){ return "Color: "+color+ "\nDiameter: "+diameter+ "\nBounce: "+bounce; } }
输入文件内容
Blue 48 0 Red 121 0 Green 127 1 Yellow 680 2 Black 120 6
问题分析与修复
1. Ball类构造函数参数错误
Ball类构造函数中,将参数赋值给了未定义的变量n和m,修正后:
Ball(String c, long d, int b){ color = c; diameter = d; bounce = b; }
2. BallLinkedList类类型混淆
- 链表节点应为
Ball类型,将private Planet head;改为private Ball head; - 构造函数
BallLinkedList(String c, long d, int b)中,将new Planet(c, d, b)改为new Ball(c, d, b)
3. 插入方法逻辑完全错误
当前insert方法未实现正确的链表插入逻辑,修正后:
void insert(Ball insertee, int pos){ // 插入头部或空链表 if(pos == 0 || head == null){ insertee.next = head; head = insertee; return; } Ball current = head; // 遍历到目标位置的前一个节点 for(int i=0; i < pos-1 && current.next != null; i++){ current = current.next; } // 插入新节点 insertee.next = current.next; current.next = insertee; }
4. printLL方法破坏链表结构
原方法遍历中直接修改head,导致第二次打印无内容,改用临时变量遍历:
void printLL(){ Ball current = head; while(current != null ){ System.out.println(current.getColor()); System.out.println(current.getDiameter()); System.out.println(current.getBounce()); current = current.next; } }
5. 文件读取逻辑错误
输入文件每组数据为3行,原代码多读取一行整数导致报错,修正后:
while(scan.hasNext()){ String color = scan.nextLine(); long diameter = Long.parseLong(scan.nextLine()); int bounce = Integer.parseInt(scan.nextLine()); Ball insertee = new Ball(color,diameter,bounce); ballSet.insert(insertee,index); index++; }
6. 方法调用错误
将Ball insertee = newBall(color,diamater,bounce);改为Ball insertee = new Ball(color,diamater,bounce);,不存在newBall方法。
7. 异常处理空实现
catch块添加异常打印,方便排查问题:
catch(Exception eee){ eee.printStackTrace(); }
内容的提问来源于stack exchange,提问作者pokemonmater940
相关产品推荐
相关产品推荐

