如何在java中使用LinkedList类?

时间:2014-04-10 10:30:22

标签: java list linked-list

所以我试图在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>;


}

这是我到目前为止所做的事情。我应该按照描述存储一些读数,并在温度高度平均值和温度低温度范围内使用它。我不确定如何首先创建列表并使用&#34; addDailyReport&#34;将数据存储在其中。方法

2 个答案:

答案 0 :(得分:0)

试试这个:

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);
    }
} 

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

但是你应该知道一些观点:

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

您不能以这种方式定义方法,参数应该像这样定义:

(type name, type name ,...)
顺便说一句,你应该在这种情况下使用作文,而不是继承

答案 1 :(得分:0)

首先,您的list参数需要一个名称,例如

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

然后您可以在方法中使用它并使用

添加报告
  reportList.add(this);