当打印ArrayList乘法值时,它仅显示->.'[]' [英] While printing ArrayList multipe value it displays only -> '[]'

查看:57
本文介绍了当打印ArrayList乘法值时,它仅显示->.'[]'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用Java显示 ArrayList 的元素时遇到问题.返回 ArrayList 时,它从Views调用到BMIAnalyzier类,该类现在包含伪值表单.它显示
[][]
运行Java文件时.

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

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

  • 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.
    }
    

  • 输出:

    推荐答案

    我想您是java的新手.您需要首先了解基础知识.例如,在您的情况下,ArrayList是一个集合,您可以使用它保存相同类型的多个值.使用Records类,您可以扩展此处不需要的集合.

    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.

    公共类Records扩展了ArrayList<记录> {

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

    然后,find(String sid)返回Records对象

    And, find(String sid) is returning Records object

    ArrayList < Records > arraylist = analyzier.find(choice[1]);
    

    应更改为

    Records record = analyzier.find(choice[1]);
    

    要打印Records对象的值,请按照@Niklas的建议进行操作

    For printing the values of your Records object do as @Niklas suggested

    这篇关于当打印ArrayList乘法值时,它仅显示-&gt;.'[]'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆