【问题标题】:While printing ArrayList multipe value it displays only -> '[]'在打印 ArrayList 多个值时,它仅显示 -> '[]'
【发布时间】:2018-06-22 05:41:05
【问题描述】:

我在用 Java 显示 ArrayList 的元素时遇到问题。当从 Views 调用 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.
    }
    

输出:

【问题讨论】:

  • 来吧,如果你懒得格式化你的代码并使你的问题可读,你为什么认为我们可以费心回答/帮助你?
  • 显然是空的。
  • 你的 Break 和 Switch 命令如何工作.. 我想知道 ??

标签: java arraylist command-line


【解决方案1】:

主要的谜团是你有 Records 类扩展 ArrayList。我不知道你为什么要这样做,但是在这样的情况下,你继承了ArrayListtoString() 方法,这是呈现[] 的原因,因为数组是空的。您需要为Records 类实现toString()

String toString() {
    return subjectId + " " + height + " " + width + " " + bmianalyzier + " " + categories;
}

【讨论】:

    【解决方案2】:

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

    公共类 Records 扩展 ArrayList {

    应该是

    公开课记录{

    此外,您应该始终为您的类 Constructor 提供内容,否则不会为该对象设置任何值。

        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 的建议进行操作

    【讨论】:

      猜你喜欢
      • 2014-03-25
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 2020-07-22
      相关资源
      最近更新 更多