【问题标题】:Using for loop for an ArrayLIst in Java (Blue J)?在 Java 中为 ArrayLIst 使用 for 循环(蓝色 J)?
【发布时间】:2014-02-17 01:34:01
【问题描述】:

基本上,我一直很难使用 for 循环来查找存储在类天文台的数组列表中的最大地震震级。我收到一条错误消息,告诉我在我的 for 循环中找不到变量“i”,无法找到天文台记录的最大震级的方法。

class Observatory
{
private String name;
private String country;
private int yearStarted;
private int area;
private int runningTotal;
private ArrayList<Earthquake> earthquakes;
private ArrayList<String> observatories;
Scanner userInput = newScanner(System.in);

private double largestMag(ArrayList<Earthquake> earthquake, double magnitude)
{

    if (earthquake == null || earthquake.isEmpty()){
        return 0;
    }
    for (Earthquake quake : earthquake) {
        if (earthquake.get(0) < earthquake.get(i)){
            return earthquake.get(i);
        }
        if (earthquake.get(0) > earthquake.get(i)){
            return earthquake.get(0);
        }
    }      
}

我知道这与在我的地震类中添加地震的事实有关,数组列表存储了不止一条信息?

这是我的地震课

public class Earthquake
{
// instance variables - replace the example below with your own
private double magnitude;
private double latitude;
private double longitude;
private int yearOfEvent;
private String position;
private String details;




public Earthquake()
{
    magnitude = 0;
    latitude = 0;
    longitude = 0;
    yearOfEvent = 0;

}

public void setMagnitude(double magnitude)
{
    this.magnitude= magnitude;
}

public double getMagnitude()
{
    return magnitude;
}

public void setYear(int yearOfEvent)
{
    this.yearOfEvent = yearOfEvent;
}

public int getYear()
{
    return yearOfEvent;
}

public void setPosition(double latitude, double longitude)
{
    this.latitude = latitude;
    this.longitude = longitude;
}

public String getPosition()
{
   position = latitude + ", " + longitude;
   return position;
}

public String getDetails()
{
    details = magnitude + " ," + latitude + "," + longitude + ","+ yearOfEvent;
    return details;

}

}

我不确定如何解决从数组列表中提取大小以用于我的方法中的计算的问题。谢谢。

【问题讨论】:

  • 也许您可以将float 用于magnitude

标签: java arrays for-loop arraylist foreach


【解决方案1】:

您正在混合访问List 中项目的方式。像您一样使用增强的for 循环消除了手动计数时所拥有的计数器变量。相反,您使用 quake 变量,该变量按顺序设置为每个值:

Earthquake largest = quakes.get(0); // start with whatever's first
for(Earthquake quake: earthquakes) {
    if(quake.getMagnitude() > largest.getMagnitude()) {
        largest = quake;
    }
}

还有一些其他问题:

  • 方法参数只使用List 而不是ArrayList。这让使用您的代码的人可以发送任何类型的List
  • 您不能使用&lt;&gt; 比较对象。但是,您可以实现Comparable 接口并提供compareTo() 方法来告诉您哪个地震的震级更大。然后你就可以使用Collections.max(earthquakes)
  • 我不知道你想在你的循环逻辑中做什么,但你不能只是return 你看到的第一件事。如果您需要执行顺序搜索,您可以为“迄今为止的最大值”或“迄今为止的最小值”设置一个变量,然后查看每个项目,如果发现新的极值则更新该变量。

【讨论】:

  • 啊好吧,这更有意义。我试图做的是获取每次地震的震级,如果循环进行时遇到比存储的值大的一个,它将用新震级(作为最大震级)替换该值,然后在所有地震后返回最大震级看过了
  • @user3236854 您的代码看起来可能与一些递归搜索混淆了,这可以用于二进制,但对于线性只需依次遍历项目。
【解决方案2】:

您在内部使用的 for 循环使用 迭代器,它没有计数器变量 i。不要使用 earthquake.get(i),而是使用 quake

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2014-03-16
    • 2014-06-10
    • 2018-10-15
    相关资源
    最近更新 更多