如何在ArrayList上编写for循环以查找最大值

时间:2019-10-30 17:07:08

标签: java for-loop arraylist

我必须为自己的课程创建地震监测系统。我有一类名为“地震”的类,其中有用于存储和检索事件的大小,位置和年份的代码。我的下一个课程是“天文台”,在那里我有代码可以检索天文台的名称,位置,开始的年份和区域。此类还列出了已记录的所有地震事件(通过从上一类中导入“地震”数组列表)。

但是,我不知道如何编写一种方法来返回由天文台记录的最大地震,因为此信息存储在“地震”类而非“天文台”类中。 SOS?

Java,在BlueJ上编码。

地震课:

public class Earthquake
{
/* instance variables - information about the magnitude, position and year of the event*/
private double magnitude;
private double latitude;
private double longitude;
private int year;

public Earthquake(double magnitude, double latitude, double longitude, int year) // initialise instance variables
{
    this.magnitude = magnitude;
    this.latitude = latitude;
    this.longitude = longitude;
    this.year = year;

}


public double getMagnitude()
//method to get magnitude data
{
    return magnitude;
}

public double getLatitude()
//method to get latitude data
{
    return latitude;
}

public double getLongitude()
//method to get longitude data
{
    return longitude;
}

public int getYear()
//method to get year data
{
    return year;
}

天文课:

import java.util.ArrayList;
public class Observatory
{
/* instance variables - name of observatory, country it is in, year in which earthquake observations started, area covered by observatory and list of earthquake events*/

private String name;
private String country;
private int yearFirstEarthquakeRecorded;
private double area;
private ArrayList<Earthquake> recordedEarthquakes;

public Observatory(String name, String country, int yearFirstEarthquakeRecorded, double area)
{
    // initialise instance variables
    this.name = name;
    this.country = country;
    this.yearFirstEarthquakeRecorded = yearFirstEarthquakeRecorded;
    this.area = area;
    ArrayList<Earthquake> recordedEarthquakes = new ArrayList<Earthquake>(); /*I assume this imports data from Earthquake class*/

}

public String getName()
{
    return name;
}

public String getCountry()
{
    return country;
}

public int getYearFirstEarthquakeRecorded()
{
    return yearFirstEarthquakeRecorded;
}

public double getArea()
{
    return area;
}

public void addEarthquake(Earthquake E)
{
    recordedEarthquakes.add(E); //allows earthquakes recorded to be added?
}

但是如何获取有关最大地震的数据?

想法:这有意义吗?它不起作用,并说找不到“ .size” ...

public Earthquake getLargestMagnitude(double magnitude)
{
    for (int i = 0; i < Earthquake.size(); i++) {
        if (Earthquake.getMagnitude(i) > magnitude) {
            return Earthquake;
        }
    }
}

public Earthquake getLargestMagnitude(double magnitude)
{
    for (int i = 0; i < Earthquake.size(); i++) {
        if (Earthquake.getMagnitude(i) > magnitude) {
            return Earthquake;
        }
    }
}

为此,我期望代码返回最大强度的整个地震对象。但这不起作用。

2 个答案:

答案 0 :(得分:0)

在构造函数中初始化recordedEarthquakes的方式也应该是错误的

`recordedEarthquakes = new ArrayList<>();`

    public Earthquake getLargestMagnitude(double magnitude)
    {
        Earthquake largestMagnitude;
        for (int i = 0; i < recordedEarthquakes.size(); i++) 
        {
            Earthquake earthQuake = recordedEarthquakes.get(i);
            if (earthQuake.getMagnitude() > magnitude) {
                largestMagnitude = earthQuake;
            }
        }
        return largestMagnitude;
    }
    

答案 1 :(得分:0)

编码问题是您没有引用该变量。也往往不会用get来命名非getter方法。

您不需要参数,因为您要的是最大参数,而不是第一个大于参数的参数。所以我删除了它。

master