简体   繁体   English

如何在Java中使用LinkedList类?

[英]How to use LinkedList class in java?

So I'm trying to design a program in java that provides average daily temperate (low and high) for a certain month of a year.I've created a class that include two methods one for the average low and one for the average high temperature. 所以我正在尝试用Java设计一个程序,该程序可以提供一年中某个月的平均每日温度(低和高)。我创建了一个类,该类包括两种方法,一种用于平均低温度,一种用于平均高温度。温度。

Now I'm trying to add a method that consumes a date (day, month, year) and a List of readings (nominally for that date) and stores a daily report for the given date (computing the high and low temperature readings from the given list of readings for that date). 现在,我正在尝试添加一种方法,该方法使用一个日期(日,月,年)和一个读数列表(名义上针对该日期),并存储给定日期的每日报告(计算温度和温度的读数)给定该日期的阅读清单)。

I am struggling with the last method, i don't fully understand how am i supposed to go on doing it (I am not supposed to enter examples for each day, rather just calculate the average of available readings of the days in a certain month). 我正在为最后一种方法而苦苦挣扎,我不完全了解我应该如何继续进行(我不应该每天输入示例,而只是计算某个月中几天的可用读数的平均值)。

So anyone could point me in the right direction, this is supposed to be a practice for using LinkedList class in java. 因此,任何人都可以向我指出正确的方向,这应该是在Java中使用LinkedList类的一种做法。

 Public class DailyReadings{

  int day;
  int month;
  int year;
  int highTemperature;
  int lowTemperature;

public DailyReading(int day, int month, int year, int highTemperature, int lowTemperature){

 this.day = day;
 this.month = month;
 this.year = year;
 this.highTemperature = highTemperature;
 this.lowTemperature= lowTemperature;
}

}

public class WeatherMonitor extends DailyReadings {


int averageHighForMonth (this.month, this.year) {
int tempHighAvg = 0;
int tempSum = 0;  
int x = 0;
for ( DailyReadings highTemperature : Report ) {
 if (this.month=this.month){
 tempSum = tempSum + highTemperature;
  x  = x++;
 }
 }
 tempHighAverage = tempSum/x; 
}
return tempHighAverage;
}

int averageLowForMonth (this.month, this.year){
 int tempLowAvg = 0;
 int tempSum = 0;
 int x = 0;
 for ( DailyReadings lowTemperature : Report ) {
  if (this.month = this.month) {
  tempSum = tempSum + lowTemperature;
  x  = x++;
  }
  }
 tempLowAverage = tempSum/x;
 } 
 return tempLowAverage;
}

int addDailyReport(int date, LinkedList<DailyReadings>  ) {



  }

 } 

class Examples {

 LinkedList<DailyReadings> Report = new  LinkedList<DailyReadings>;


}

Here's what I've go so far. 这是我到目前为止所要做的。 I'm supposed to store some readings as described and use it in the "temperateHighAverage, and temperatureLowAverage). I'm not sure how to first create the list and also storing the data in it using the "addDailyReport" method. 我应该按所述方式存储一些读数,并在“ temperateHighAverage和temperatureLowAverage”中使用它。我不确定如何首先创建列表,以及如何使用“ addDailyReport”方法在其中存储数据。

Try this: 尝试这个:

import java.util.Iterator;
import java.util.LinkedList;

public class TestClass {
    public static void main(String[] args) {

        WeatherMonitor wM = new WeatherMonitor();

        DailyReadings dR = new DailyReadings(1,1,2020, 10, -20);
        wM.addDailyReport(dR);

        dR = new DailyReadings(1,2,2020, 10, -21);
        wM.addDailyReport(dR);

        dR = new DailyReadings(1,3,2020, 11, -20);
        wM.addDailyReport(dR);

        //...

        System.out.println(wM.averageHighForMonth(1, 2020));
        System.out.println(wM.averageLowForMonth(1, 2020));

    }
}


class DailyReadings {
    int day;
    int month;
    int year;
    int highTemperature;
    int lowTemperature;

    public DailyReadings(int day, int month, int year, int highTemperature, int lowTemperature){

        this.day = day;
        this.month = month;
        this.year = year;
        this.highTemperature = highTemperature;
        this.lowTemperature= lowTemperature;
    }
}

class WeatherMonitor {

    LinkedList<DailyReadings> Readings = new  LinkedList<DailyReadings>();

    int averageHighForMonth (int month, int year) {
        int tempHighAvg = 0;
        int count = 0;
        Iterator<DailyReadings> it =  Readings.iterator();
        DailyReadings tmp;
        while( (tmp = it.next()) != null) {
            if (tmp.month == month && tmp.year == year) {
                tempHighAvg += tmp.highTemperature;
                count++;
            }
        }
        return tempHighAvg / count;
    }

    int averageLowForMonth (int month, int year) {
        int tempLowAvg = 0;
        int count = 0;
        Iterator<DailyReadings> it =  Readings.iterator();
        DailyReadings tmp;
        while( (tmp = it.next()) != null) {
            if (tmp.month == month && tmp.year == year) {
                tempLowAvg += tmp.lowTemperature;
                count++;
            }
        }
        return tempLowAvg / count;
    }

    void addDailyReport(DailyReadings dR) {
        Readings.add(dR);
    }
} 

//----------------------- // -----------------------

But some points that you should know : 但是您应该知道一些要点:

int averageLowForMonth (this.month, this.year){// This is wrong!

You cannot define a method this way, parameters should be defined like this: 您不能以这种方式定义方法,应按以下方式定义参数:

(type name, type name ,...)

BTW, you should use Composition in situations like this, not Inheritance . 顺便说一句,您应该在这种情况下使用Composition ,而不是Inheritance

First of all, your list parameter needs a name, something like 首先,您的list参数需要一个名称,例如

 int addDailyReport(int date, LinkedList<DailyReadings> reportList ) 

Then you can use it inside the method and add the report with 然后,您可以在方法内部使用它,并使用

  reportList.add(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM