Java中对象数组添加元素重复显示最后元素的问题排查
问题排查:数组重复显示最后一个元素的原因及修复
我定义了ChargingPoint类,并在FuelSt类中创建了两个ChargingPoint类型的数组arrFast和arrUs。在Main方法中创建了4个ChargingPoint实例:2个标记为'f'的实例需加入arrFast,2个标记为'u'的实例需加入arrUs。但运行后数组中仅重复显示最后一个元素,附上相关代码请求排查原因。
ChargingPoint类
public class ChargingPoint { private double kw; private double trf; private char isFast; private String code; public ChargingPoint (String code,char isFast) { this.code = code; this.isFast = isFast; trf = 1.5; } public void setTRF(double trf ) { if(trf >=0) { this.trf = trf; } else{ System.out.println("Error,invaild input"); } } public void setKW(double kw) { if(kw >=0) { this.kw= kw; } else { System.out.println("Error,invaild input"); } } public double getKW() { return kw; } public double getTRF() { return trf; } public char getISFAST() { return isFast; } public String getCODE() { return code; } public double consumptionTotal(double KW,double TRF) { double sum; this.kw = KW; this.trf = TRF; sum = (TRF*KW); return sum; } public void print(){ System.out.println("kw =" + kw + ", trf =" + trf + ", isFast =" + isFast + ", code =" + code); } public ChargingPoint() { } public char getIsFast() { return isFast; } public double consumptionTotal() { return (trf*kw); } public String toString() { return "kw: "+this.kw+" " +"isfast: "+this.isFast + " " +"code: "+this.code; } }
FuelSt类
public class FuelSt { private ChargingPoint[] arrUs; private int currentUs ; private ChargingPoint[] arrFast; private int currentFt ; private boolean highway = true; ChargingPoint charg1 = new ChargingPoint(); ChargingPoint charg2 = new ChargingPoint(); public FuelSt (boolean highway) { if(highway = true) { this.highway = highway; double c; c = charg1.consumptionTotal(); c = c -((c*13)/100); } else { this.highway =false; } } public boolean addChargingPoint (ChargingPoint charg1){ if(charg1.getIsFast() == 'f') { if(arrFast == null) { arrFast = new ChargingPoint[30]; } else { for(int i=0; i < arrFast.length;i++) { arrFast[i] = charg1; } } } else if (charg1.getIsFast() == 'u') { if(arrUs == null) { arrUs = new ChargingPoint[30]; } else { for(int i=0; i < arrUs.length;i++) { arrUs[i] = charg1; currentUs++; } } } return false; } }
Main方法
public static void main(String[] args) throws IOException { System.out.print("is the fuel next to a highway?"); Scanner in = new Scanner(System.in); boolean yes = in.nextBoolean(); FuelSt fuelroad = new FuelSt(yes); System.out.println("type kw"); double kw = in.nextDouble(); System.out.println("type u/f"); char isFast = (char)System.in.read(); System.out.println("type code"); String code = in.next(); ChargingPoint chargF= new ChargingPoint(code,isFast); chargF.setKW(kw); fuelroad.addChargingPoint(chargF); chargF.print(); System.out.println("type kw"); double kw1 = in.nextDouble(); System.out.println("type u/f"); char isFast1 = (char)System.in.read(); System.out.println("type code"); String code1 = in.next(); ChargingPoint chargF1= new ChargingPoint(code1,isFast1); chargF1.setKW(kw1); fuelroad.addChargingPoint(chargF1); chargF1.print(); System.out.println(Arrays.toString(fuelroad.getarrFast())); System.out.println("type kw"); double kw2 = in.nextDouble(); System.out.println("type u/f"); char isFast2 = (char)System.in.read(); System.out.println("type code"); String code2 = in.next(); ChargingPoint chargU = new ChargingPoint(code2,isFast2); chargU.setKW(kw2); fuelroad.addChargingPoint(chargU); chargU.print(); System.out.println("type kw"); double kw3 = in.nextDouble(); System.out.println("type u/f"); char isFast3 = (char)System.in.read(); System.out.println("type code"); String code3 = in.next(); ChargingPoint chargU1 = new ChargingPoint(code3,isFast3); chargU1.setKW(kw3); fuelroad.addChargingPoint(chargU1); chargU1.print(); }
核心问题原因
addChargingPoint方法逻辑错误:每次添加元素时,遍历整个数组的所有位置,将每个位置都赋值为当前传入的对象。比如添加第一个'f'实例时,数组所有30个位置都被设为这个实例;添加第二个'f'实例时,又把所有位置覆盖成第二个实例,最终数组里全是最后一次添加的对象。- 未正确使用计数变量:定义了
currentFt和currentUs记录数组当前元素个数,但完全没用到。应该用这个变量定位下一个要插入的位置,而非遍历整个数组。 - 构造函数判断错误:
FuelSt构造函数里的if(highway = true)是赋值操作,不是判断,应改为if(highway == true)或直接if(highway)。 - 输入读取问题:
(char)System.in.read()会读取到之前输入后的换行符,导致实际读取的不是用户输入的'u'或'f',应改用in.next().charAt(0)。 - 缺少数组getter方法:Main里调用
fuelroad.getarrFast()但原代码无此方法,需补充。
修复后的代码
修复后的FuelSt类
public class FuelSt { private ChargingPoint[] arrUs; private int currentUs ; private ChargingPoint[] arrFast; private int currentFt ; private boolean highway = true; public FuelSt (boolean highway) { if(highway) { this.highway = highway; } else { this.highway = false; } } public boolean addChargingPoint (ChargingPoint chargingPoint){ if(chargingPoint.getIsFast() == 'f') { if(arrFast == null) { arrFast = new ChargingPoint[30]; } if(currentFt < arrFast.length) { arrFast[currentFt++] = chargingPoint; } else { System.out.println("arrFast数组已满"); } } else if (chargingPoint.getIsFast() == 'u') { if(arrUs == null) { arrUs = new ChargingPoint[30]; } if(currentUs < arrUs.length) { arrUs[currentUs++] = chargingPoint; } else { System.out.println("arrUs数组已满"); } } return true; } public ChargingPoint[] getArrFast() { return arrFast; } public ChargingPoint[] getArrUs() { return arrUs; } }
修复后的Main方法
import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.print("is the fuel next to a highway?"); Scanner in = new Scanner(System.in); boolean yes = in.nextBoolean(); FuelSt fuelroad = new FuelSt(yes); // 第一个快速充电点 System.out.println("type kw"); double kw = in.nextDouble(); System.out.println("type u/f"); char isFast = in.next().charAt(0); System.out.println("type code"); String code = in.next(); ChargingPoint chargF= new ChargingPoint(code,isFast); chargF.setKW(kw); fuelroad.addChargingPoint(chargF); chargF.print(); // 第二个快速充电点 System.out.println("type kw"); double kw1 = in.nextDouble(); System.out.println("type u/f"); char isFast1 = in.next().charAt(0); System.out.println("type code"); String code1 = in.next(); ChargingPoint chargF1= new ChargingPoint(code1,isFast1); chargF1.setKW(kw1); fuelroad.addChargingPoint(chargF1); chargF1.print(); // 打印arrFast数组(仅显示非空元素) System.out.println("arrFast内容:"); for(int i=0; i<fuelroad.getArrFast().length && fuelroad.getArrFast()[i]!=null; i++){ System.out.println(fuelroad.getArrFast()[i]); } // 第一个普通充电点 System.out.println("type kw"); double kw2 = in.nextDouble(); System.out.println("type u/f"); char isFast2 = in.next().charAt(0); System.out.println("type code"); String code2 = in.next(); ChargingPoint chargU = new ChargingPoint(code2,isFast2); chargU.setKW(kw2); fuelroad.addChargingPoint(chargU); chargU.print(); // 第二个普通充电点 System.out.println("type kw"); double kw3 = in.nextDouble(); System.out.println("type u/f"); char isFast3 = in.next().charAt(0); System.out.println("type code"); String code3 = in.next(); ChargingPoint chargU1 = new ChargingPoint(code3,isFast3); chargU1.setKW(kw3); fuelroad.addChargingPoint(chargU1); chargU1.print(); // 打印arrUs数组(仅显示非空元素) System.out.println("arrUs内容:"); for(int i=0; i<fuelroad.getArrUs().length && fuelroad.getArrUs()[i]!=null; i++){ System.out.println(fuelroad.getArrUs()[i]); } in.close(); } }
修复说明
addChargingPoint方法:移除遍历数组的逻辑,改用currentFt和currentUs记录插入位置,每次仅赋值到对应位置后计数加1。- 构造函数:修复
highway判断逻辑,移除无用实例变量。 - 输入读取:用
in.next().charAt(0)替代System.in.read(),避免读取换行符的问题。 - 添加getter方法:补充数组的getter方法,方便Main方法获取数组内容。
- 数组打印:遍历数组时仅打印非空元素,避免显示大量null。
内容的提问来源于stack exchange,提问作者liraz
相关产品推荐
相关产品推荐

