当打印ArrayList乘法值时,它仅显示->'[]'

时间:2018-06-22 05:41:05

标签: java arraylist command-line

我在用Java显示ArrayList的元素时遇到问题。当从视图中调用ArrayList返回BMIAnalyzier类时,该类现在包含伪值表单。显示
[] []
 运行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");
    }
    
  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.
    }
    

输出:

here I enter the option 2 and range 0 100 and result is in the picture below but that what I wanted to display the values which are in the ArrayList.to see the result of mine click this .

2 个答案:

答案 0 :(得分:0)

主要的奥秘在于您拥有Records类扩展ArrayList。我不知道为什么要这样做,但是这样做是在继承ArrayList的{​​{1}}方法,这是呈现toString()的方法,因为数组为空。您需要为[]类实现toString()

Records

答案 1 :(得分:0)

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

  

公共类Records扩展了ArrayList {

应该是

  

公共课记录{

此外,您应始终向类构造方法提供内容,否则将不为该对象设置任何值。

    public Records(String sid, Double h, Double w, Double bmi, String c) {
        this.SubjectId = sid;
       // set other values as well
    }

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

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

应更改为

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

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