[英]While printing ArrayList multipe value it displays only -> '[]'
I have a problem with displaying the elements of the ArrayList
in Java. 我在用Java显示ArrayList
的元素时遇到问题。 While returning the ArrayList
when its called from the Views to BMIAnalyzier class which contains the dummy values form now. 当返回ArrayList
时,它从Views调用到BMIAnalyzier类,该类现在包含伪值表单。 It shows 表明
[] [] [] []
When java files are run. 运行Java文件时。
Views.java Views.java
Switch(choice[0]){ case 1: //Option 1 to display the data fo subject of given subject id. ArrayList<Records> arraylist = analyzier.find(choice[1]); System.out.println(ArrayList); Break; Case 2: //Option 2 to display the data fo subject from the range given of BMI. ArrayList<Records> arraylistList = analyzier.find(Double.parseDouble(choice[1]),Double.parseDouble(choice[2])); for (int i = 0; i < arraylistList.size(); i++){ System.out.println((arraylistList.get(i)).toString()); } Break; default: System.out.println("wrong input"); }
BMIAnalyzier.java BMIAnalyzier.java
public class BMIAnalyzier { public Records find(String sid) { System.out.println(new Records(sid, 0.0, 0.0, 0.0, "none")); return new Records(sid, 0.0, 0.0, 0.0, "none"); } public ArrayList<Records> find(double bmi1, double bmi2) { ArrayList<Records> alr = new ArrayList<>(); alr.add(new Records("S01", 0.0, 0.0, 0.0, "none")); alr.add(new Records("S02", 0.0, 0.0, 0.0, "none")); return alr; } }
Records.java Records.java
public class Records extends ArrayList<Records> { private String SubjectId; private Double height; private Double width; private Double bmianalyzier; private String categories; public ArrayList <Records> list =new ArrayList<>(); public Records(String sid, Double h, Double w, Double bmi, String c) { } //getter ans setter methods. }
Output: 输出:
The main mystery is that you have the Records
class extend ArrayList
. 主要的奥秘在于您具有Records
类扩展ArrayList
。 I have no idea why you are doing that, but in so, you are inheriting ArrayList
's toString()
method, which is what renders the []
, since the array is empty. 我不知道为什么要这样做,但是在这种情况下,您继承了ArrayList
的toString()
方法,因为该方法为空,所以它是呈现[]
方法。 You need to implement toString()
for the Records
class. 您需要为Records
类实现toString()
。
String toString() {
return subjectId + " " + height + " " + width + " " + bmianalyzier + " " + categories;
}
I suppose you are new to java. 我想您是Java新手。 You need to understand the basics first. 您需要首先了解基础知识。 For instance in your case, ArrayList is a collection which you use to hold multiple values of the same type. 例如,在您的情况下,ArrayList是一个集合,您可以使用它保存相同类型的多个值。 With your Records class you are extending the collection which is not required here. 通过您的Records类,您可以扩展此处不需要的集合。
public class Records extends ArrayList < Records > { 公共类Records扩展ArrayList <Records> {
should be 应该
public class Records { 公共课记录{
Also, you should always provide content to your class Constructor otherwise no values would be set for the object. 另外,您应始终向类构造方法提供内容,否则将不会为该对象设置任何值。
public Records(String sid, Double h, Double w, Double bmi, String c) {
this.SubjectId = sid;
// set other values as well
}
And, find(String sid) is returning Records object 并且,find(String sid)返回Records对象
ArrayList < Records > arraylist = analyzier.find(choice[1]);
should be changed to 应该更改为
Records record = analyzier.find(choice[1]);
For printing the values of your Records object do as @Niklas suggested 要打印Records对象的值,请按照@Niklas的建议进行操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.