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

大城市里の小女人 提交于 2019-12-20 05:38:08

问题


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:


回答1:


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;
}



回答2:


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



来源:https://stackoverflow.com/questions/50981342/while-printing-arraylist-multipe-value-it-displays-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!