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

前端 未结 2 1161
無奈伤痛
無奈伤痛 2021-01-29 03:15

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 cl

相关标签:
2条回答
  • 2021-01-29 03:57

    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

    0 讨论(0)
  • 2021-01-29 03:59

    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;
    }
    
    0 讨论(0)
提交回复
热议问题