简体   繁体   中英

While printing ArrayList multipe value it displays only -> '[]'

I have a problem with displaying the elements of the ArrayList in Java. While returning the ArrayList when its called from the Views to BMIAnalyzier class which contains the dummy values form now. It shows
[] []
When java files are run.

  1. 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"); } 
  2. 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; } } 
  3. 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:

在这里,我输入选项2和范围0 100,结果在下面的图片中,但是我要显示ArrayList.in中的值以查看我的结果,请单击this。

The main mystery is that you have the Records class extend 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. You need to implement toString() for the Records class.

String toString() {
    return subjectId + " " + height + " " + width + " " + bmianalyzier + " " + categories;
}

I suppose you are new to 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. With your Records class you are extending the collection which is not required here.

public class Records extends 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM