如何循环创建与我当前正在使用的数组列表不同的类的数组列表?

问题描述 投票:1回答:2

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

但是,我不知道如何编写一种方法来返回天文台记录的最大地震,因为此信息存储在地震类别中而不是天文台类别中。 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;
        }
    }
}

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

java for-loop arraylist
2个回答
0
投票

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

`recordedEarthquakes = new ArrayList<>();`

公众地震getLargestMagnitude(双震级){地震最大震级;对于(int i = 0; i 幅度){maximumMagnitude = EarthQuake;}}返回maximumMagnitude;}

0
投票

编码问题是您没有引用该变量。也往往不会用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;
}
© www.soinside.com 2019 - 2024. All rights reserved.