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

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

问题描述

我在用 Java 显示 ArrayList 的元素时遇到问题.当返回 ArrayList 从视图调用到 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]);
    

    按照@Niklas 的建议打印 Records 对象的值

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

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

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