尝试编写一个方法来从数组中找到最大的对象

huangapple go评论97阅读模式
英文:

Trying to write a method to find the largest object from an array

问题

添加一个方法

public static Measurable max(Measurable[] objects)

到Data类中,该方法返回具有最大度量值的对象。

这是我所拥有的代码。

public interface Measurable {
    double getMeasure();
}

public class MeasurableTester {
    public static void main(String[] args) {
        Measurable[] accounts = new Measurable[3];
        accounts[0] = new BankAccount(0);
        accounts[1] = new BankAccount(10000);
        accounts[2] = new BankAccount(2000);

        double averageBalance = Data.average(accounts);
        System.out.println("平均余额:" + averageBalance);
        System.out.println("预期结果:4000");

        Measurable[] countries = new Measurable[3];
        countries[0] = new Country("Uruguay", 176220);
        countries[1] = new Country("Thailand", 513120);
        countries[2] = new Country("Belgium", 30510);

        double averageArea = Data.average(countries);
        System.out.println("平均面积:" + averageArea);
        System.out.println("预期结果:239950");
    }
}

public class Data {
    Object tempObj = new Object();

    public static double average(Measurable[] objects){
        double sum = 0;
        for (Measurable obj : objects){
            sum = sum + obj.getMeasure();
        }
        if (objects.length > 0) {
            return sum / objects.length;
        }else{
            return 0;
        }
    }

    // 我在这里尝试编写一个方法

    public static Measurable max(Measurable[] objects){
        double max = 0;
        for (Measurable obj : objects){
            if (obj.getMeasure() > max){
                max = obj.getMeasure();
            }  
        }
        return max;
    }
}

注意:我已经将代码中的HTML实体编码(例如">)替换为了正常的引号和符号。如果您在代码中使用这些修饰符,可以恢复它们。

英文:

Add a method
public static Measurable max(Measurable[] objects)
to the Data class that returns the object with the largest measure.

This is what I have for code.

public interface Measurable
{
double getMeasure();
}

public class MeasurableTester {

public static void main(String[] args) {
    Measurable[] accounts = new Measurable[3];
    accounts[0] = new BankAccount(0);
    accounts[1] = new BankAccount(10000);
    accounts[2] = new BankAccount(2000);
    
    double averageBalance = Data.average(accounts);
    System.out.println("Average balance: " + averageBalance);
    System.out.println("Expected: 4000");
    
    Measurable[] countries = new Measurable[3];
    countries[0] = new Country("Uruguay", 176220);
    countries[1] = new Country("Thailand", 513120);
    countries[2] = new Country("Belgium", 30510);
    
    double averageArea = Data.average(countries);
    System.out.println("Average area: " + averageArea);
    System.out.println("Expected: 239950");
}

}`

'''
and the part I am having problems with
'''

public class Data {
     Object tempObj = new Object();

public static double average(Measurable[] objects){
    double sum = 0;
    for (Measurable obj : objects){
        sum = sum + obj.getMeasure();
    }
    if (objects.length > 0) {
        return sum / objects.length;
    }else{
        return 0;
    }
}

'''

//I am trying to come up with a method here

public static Measurable max(Measurable[] objects){
    double max = 0;
    for (Measurable obj : objects){
      if (obj.getMeasure() > max){
          obj =  max;
      }  
    }
   return max;
  }
 }

答案1

得分: 2

max方法应该如下所示:

public static Measurable max(Measurable[] objects) {
   Measurable max = null;
   for (Measurable obj : objects) {
       if (max == null || obj.getMeasure() > max.getMeasure()) {
              max = obj;
       }  
    }
   return max;
}

如果你使用的是Java 8,你可以这样写:

public static Measurable max(Measurable[] objects) {
  return Arrays.stream(objects).max(Comparator.comparing(Measurable::getMeasure)).get();
}

另外,使用以下代码片段时:

for (Measurable obj : objects){

没有进行null检查会导致出现NullPointerException

---- 编辑 ------

使用max方法:

Measurable max = Data.max(countries) // 得到包含最大测量值的对象
System.out.println(max.getMeasure()) // 打印最大测量值
英文:

The max method should be like:


public static Measurable max(Measurable[] objects) {
   Measurable max = null;
   for (Measurable obj : objects) {
       if (max == null || obj.getMeasure() > max.getMeasure()) {
              max = obj;
       }  
    }
       return max;
}

If you're using Java 8, you could do:

public static Measurable max(Measurable[] objects) {
  return Arrays.stream(objects).max(Comparator.comparing(Measurable::getMeasure)).get();
}

Also, using

for (Measurable obj : objects){

without null check is a recipe for NullPointerException.

---- Edit ------<br>
Using the max method:

Measurable max = Data.max(countries) // gives max measure containing object
System.out.println(max.getMeasure()) // prints the max measure value

huangapple
  • 本文由 发表于 2020年8月30日 11:50:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63653773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定