Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中使用LinkedList类?_Java_List_Linked List - Fatal编程技术网

如何在java中使用LinkedList类?

如何在java中使用LinkedList类?,java,list,linked-list,Java,List,Linked List,因此,我试图用java设计一个程序,提供一年中某个月的平均每日温度高低。我创建了一个类,包括两个方法,一个用于平均低温,一个用于平均高温 现在,我尝试添加一个方法,该方法使用日期、月份、年份和该日期的名义读数列表,并存储给定日期的每日报告,计算该日期给定读数列表中的高温和低温读数 我正在与最后一种方法作斗争,我不完全理解我应该如何继续这样做我不应该输入每天的例子,而只是计算某个月内每天可用读数的平均值 所以任何人都可以给我指出正确的方向,这应该是在java中使用LinkedList类的一种实践

因此,我试图用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>;


}
这是我到目前为止所做的。我应该按照描述存储一些读数,并在temperateHighAverage和TemperatureLoveRage中使用它。我不知道如何首先创建列表,并使用addDailyReport方法将数据存储在其中。

尝试以下方法:

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);
    }
} 
不能以这种方式定义方法,参数的定义应如下所示:

(type name, type name ,...)

顺便说一句,您应该在这种情况下使用组合,而不是继承。

首先,您的列表参数需要一个名称,比如

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

老兄,非常感谢,这帮了大忙。我有几个问题。首先,您为什么要在2020年1月1日编写这个System.out.printlnwM.averagehighform;?这应该等同于一个例子吗?@user3519023-不客气。这只是为了测试。您可以/应该在跟踪程序后删除它。是的,我认为这对你的示例类来说是一个等价物;这一行有什么作用?@user3519023-如果你觉得我的答案有用,你可以接受它并投票:不过,Iterator it=Readings.Iterator;创建一个名为it的迭代器,该迭代器在linkedlist中的读数之间移动,并且每次您可以访问它的当前值时。有关更多信息,请参阅。
 int addDailyReport(int date, LinkedList<DailyReadings> reportList ) 
  reportList.add(this);