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

Java 如何在ArrayList上编写for循环以查找最大值,java,for-loop,arraylist,Java,For Loop,Arraylist,我必须为我的课程建立一个地震监测系统。我有一个名为“地震”的类,我有代码来存储和检索地震的震级、位置和年份。我的下一堂课是“天文台”,我有代码来检索天文台名称、位置、开始年份和地区。此类还列出了它记录的所有地震事件(通过从上一个类导入“地震”Arraylist) 但是,我不知道如何编写一个方法来返回天文台记录的最大震级地震,因为这些信息存储在地震类而不是天文台类中。求救 Java,在BlueJ上编码 地震等级: public class Earthquake { /* instance vari

我必须为我的课程建立一个地震监测系统。我有一个名为“地震”的类,我有代码来存储和检索地震的震级、位置和年份。我的下一堂课是“天文台”,我有代码来检索天文台名称、位置、开始年份和地区。此类还列出了它记录的所有地震事件(通过从上一个类导入“地震”Arraylist)

但是,我不知道如何编写一个方法来返回天文台记录的最大震级地震,因为这些信息存储在地震类而不是天文台类中。求救

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?
}
import java.util.ArrayList;
公营天文台
{
/*实例变量-天文台名称、所在国家、地震观测开始年份、天文台覆盖区域和地震事件列表*/
私有字符串名称;
私人国家;
私人国际年第一届EarthQuakereCorded;
私人双区;
私人ArrayList记录的地震;
公共天文台(字符串名称、字符串国家、国际年首个EarthQuakerRecorded、双区域)
{
//初始化实例变量
this.name=名称;
这个国家=国家;
this.yearFirstEarthquakeRecorded=yearFirstEarthquakeRecorded;
这个面积=面积;
ArrayList recordedEarthquakes=new ArrayList();/*我假设这会从地震类导入数据*/
}
公共字符串getName()
{
返回名称;
}
公共字符串getCountry()
{
返回国;
}
public int getYearFirstEarthquakeRecorded()
{
返回年份第一次地震记录;
}
公共区域()
{
返回区;
}
公共场所地震(地震E)
{
recordedEarthquakes.add(E);//是否允许添加已记录的地震?
}
但是如何获得关于最大震级地震的数据呢

想法:这有意义吗?它不起作用,并且说“.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;
        }
    }
}
公共地震最大震级(双倍震级)
{
对于(int i=0;i<地震.size();i++){
if(地震震级(i)>震级){
回归地震;
}
}
}
公共地震最大震级(双级)
{
对于(int i=0;i<地震.size();i++){
if(地震震级(i)>震级){
回归地震;
}
}
}

为此,我希望代码返回震级最大的地震的整个对象。但它不起作用。

在构造函数中初始化RecordedEarth地震的方法也是错误的,应该是错误的

`recordedEarthquakes = new ArrayList<>();`
`recordedEarthquakes=新建ArrayList()`
公共地震最大震级(双级) { 最大震级地震; 对于(int i=0;i震级){ 最大震级=地震; } } 返回量最大; }

您在构造函数中初始化RecordedEarth震动的方式也是错误的,应该是错误的

`recordedEarthquakes = new ArrayList<>();`
`recordedEarthquakes=newarraylist()`
公共地震最大震级(双级) { 最大震级地震; 对于(int i=0;i震级){ 最大震级=地震; } } 返回量最大; }

编码问题在于您没有引用变量。也不倾向于用get命名非getter方法

您不需要参数,因为您要求的是最大的,而不是比参数大的第一个。因此我删除了它

public Earthquake largestMagnitude()
{
    Earthquake largest = null;
    for (int i = 0; i < recordedEarthquakes.size(); i++) {
        if (largest == null || recordedEarthquakes.get(i).getMagnitude() > largest.getMagnitude()) {
            largest = recordedEarthquake.get(i);
        }
    }
    return largest;
}
公共地震最大震级()
{
最大地震=零;
对于(int i=0;i最大.getMagnite()){
最大=记录的地震。获取(i);
}
}
回报最大;
}

编码问题在于您没有引用变量。也不倾向于用get命名非getter方法

您不需要参数,因为您要求的是最大的,而不是比参数大的第一个。因此我删除了它

public Earthquake largestMagnitude()
{
    Earthquake largest = null;
    for (int i = 0; i < recordedEarthquakes.size(); i++) {
        if (largest == null || recordedEarthquakes.get(i).getMagnitude() > largest.getMagnitude()) {
            largest = recordedEarthquake.get(i);
        }
    }
    return largest;
}
公共地震最大震级()
{
最大地震=零;
对于(int i=0;i最大.getMagnite()){
最大=记录的地震。获取(i);
}
}
回报最大;
}

我更喜欢较少的注释,而喜欢方法和变量的好名称。我认为在你的情况下,注释是不必要的。这是我的偏好。注释会随着时间的推移而过时,因为代码会发生变化,并且是测试(希望如此)大多数人不会同时更新评论。谢谢,@fedup。你能就GetLargestMagnite代码提供一些建议吗?我对编码是新手!当然。只要你是新手…使用有意义的参数名称-addEarthquake(地震E)-应该是addEarthquake(地震地震地震)相反。一种公认的做法是以小写字母开头变量名。@fedup非常感谢!!我更喜欢少一些注释,而选择好的方法和变量名。我认为在你的情况下,注释是不必要的。这是我的首选。注释会随着时间的推移而过时,因为代码会发生变化,并且是测试(希望如此)大多数人不会在同一时间更新评论。谢谢,@fedup。你能就getLargestMagnitude代码提供一些建议吗?我对编码不太熟悉!当然。还有lon