程序不返回数组的元素

时间:2017-10-03 11:00:52

标签: java arrays

下面是一个程序,我需要将String time和int datum存储为数组中的对象,然后将它们打印出来。这就是我目前所要做的。我相信这是我的recordEvent方法的一个问题。

class RecordEvents4 {
   public static void main(String[] args){
      Recorder4 r1 = new Recorder4(100,100,"Wombat Detection");
      r1.recordEvent("10:53",20);
      r1.recordEvent("10:59",20);
      r1.recordEvent("11:05",20);
      r1.recordEvent("12:59",10);
      r1.recordEvent("13:59",10);
      r1.recordEvent("14:06",10);
      r1.printEvents();
   }
}

class Recorder4 {
   int xPos, yPos;
   String eventType;
   EventInformation[] event = new EventInformation[10];

   int xevent = 0;

   final int EVENT_MAX = 10;

   Recorder4(int xPos, int yPos, String eventType){
      this.xPos = xPos;
      this.yPos = yPos;
      this.eventType = eventType;
   }

   public void recordEvent(String time, int datum){
      /*for(int i = 0; i<event.length; i++){
         event[i] = new EventInformation(time, datum);
      }*/

      event[xevent] = new EventInformation(time, datum);

      xevent++; 
      if(xevent > EVENT_MAX){
         System.out.println("Event log overflow - terminating");
         System.exit(1);
      }
   }

   void printEvents(){       
      System.out.println("Record of " + eventType + " events at [" + xPos + "," + yPos + "]");  
      for(int i = 0; i < xevent; i++){
         System.out.println("Event number " + i + " was recorded at " + event[i] + " with datum = " + event[i]);
      }
   }
}

class EventInformation {
   String eventTime;
   int eventDatum;

   EventInformation(String eventTime, int eventDatum){
      this.eventTime = eventTime;
      this.eventDatum = eventDatum;      
   }
}

这就是它输出的内容:

Record of Wombat Detection events at [100,100]
Event number 0 was recorded at EventInformation@15db9742 with datum = EventInformation@15db9742
Event number 1 was recorded at EventInformation@6d06d69c with datum = EventInformation@6d06d69c
Event number 2 was recorded at EventInformation@7852e922 with datum = EventInformation@7852e922
Event number 3 was recorded at EventInformation@4e25154f with datum = EventInformation@4e25154f
Event number 4 was recorded at EventInformation@70dea4e with datum = EventInformation@70dea4e
Event number 5 was recorded at EventInformation@5c647e05 with datum = EventInformation@5c647e05

这就是我所期待的:

Record of Wombat Detection events at [100,100] 
Event number 0 was recorded at 10:53 with datum = 20 
Event number 1 was recorded at 10:59 with datum = 20 
Event number 2 was recorded at 11:05 with datum = 20 
Event number 3 was recorded at 12:59 with datum = 10 
Event number 4 was recorded at 13:59 with datum = 10 
Event number 5 was recorded at 14:06 with datum = 10

我被建议使用toString(),但我不确定我是否在使用recordEvent()的方向正确,我打算将方法参数分配给EventInformation对象,其引用是存储在事件数组的元素中。

2 个答案:

答案 0 :(得分:1)

您实际上是在要求Java打印出您的events数组的元素。在您对此数组的声明中,您为其中的元素提供了EventInformation类型。默认情况下,Java显然不知道如何打印它,因为您自己定义了它!要解决此问题,请更改您的打印声明:

    System.out.println("Event number " + i + " was recorded at " + event[i].eventTime + " with datum = " + event[i].eventDatum);

现在要求Java打印eventTime(字符串)和eventDatum(int)。这些都是Java知道如何打印的东西,因为它们是由Java本身定义的。

答案 1 :(得分:0)

您的<link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.11.3.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script> $(document).on('change','select#month',function(e){ var eday; e.preventDefault(); var month = $(this).val(); if(month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" || month == "12" ) { eday = 31; } if(month == "04" || month == "06" || month == "09" || month == "11") { eday = 30; } if(month == "02") { eday = 28; } var today = new Date(); var startDate = new Date(today.getFullYear(), month-1, 1); var endDate = new Date(today.getFullYear(), month-1, eday); $("#date-picker").datepicker("destroy"); $('#date-picker').datepicker( { autoclose: true, minDate: startDate, maxDate: endDate }); }); </script> 方法应如下所示:

printEvents()

在原始代码中,您尝试打印void printEvents(){ System.out.println("Record of " + eventType + " events at [" + xPos + "," + yPos + "]"); for(int i = 0; i < xevent; i++){ System.out.println("Event number " + i + " was recorded at " + event[i].eventTime + " with datum = " + event[i].eventDatum); } } 两次的实例,而不是首先打印EventInformation字段,然后打印eventTime。因此,您将获得实例的defualt String表示,如eventDatum中所定义。

那个,或者为Object编写getter方法,如果你愿意的话,同时将字段设置为EventInformation,这可能是更好的做法,特别是如果有的话你以后会更喜欢上课。

编辑:与Oliver-R说的相同。