簡體   English   中英

如何比較對象數組列表中的一個值以輸出最大值?

[英]How to compare one value in an arraylist of objects to output largest value?

我需要創建一個方法getLargestObject(),該方法在數組列表中查找具有最大面積的對象,該數組返回其位置並輸出該對象的內容。 我正在使用的當前循環不起作用,並且我不確定如何比較數組列表中區域的值,因此我可以獲得最大的循環。

package csu.cole;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Driver {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File(
                "C:/Users/Charles/Desktop/GeometricObjectsData.txt"));

        ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();


        while (input.hasNextLine()) {
            String line = input.nextLine();
            String[] tokens = line.split(", ");
            if (tokens[0].equals("CIRCLE")) {
                Circle c = new Circle();
                if (tokens.length == 4) {
                    float radius = Float.parseFloat(tokens[1]);
                    c.setRadius(radius);
                    String color = String.valueOf(tokens[2]);
                    c.setColor(color);
                    Boolean filled = Boolean.valueOf(tokens[3]);
                    c.setFilled(filled);
                    c.getArea();
                    list.add(c);
                    System.out.println(c.toString());
                } else if (tokens.length == 3) {
                    float radius = Float.parseFloat(tokens[1]);
                    c.setRadius(radius);
                    String color = String.valueOf(tokens[2]);
                    c.setColor(color);
                    Boolean filled = false;
                    c.setFilled(filled);
                    c.getArea();
                    list.add(c);
                    System.out.println(c.toString());
                } else if (tokens.length == 1) {
                    String color = "white";
                    c.setColor(color);
                    Boolean filled = false;
                    c.setFilled(filled);
                    c.getArea();
                    list.add(c);
                    System.out.println(c.toString());
                }
            } else if (tokens[0].equals("RECTANGLE")) {
                Rectangle r = new Rectangle();
                if (tokens.length == 5) {
                    float height = Integer.parseInt(tokens[1]);
                    r.setHeight(height);
                    float width = Integer.parseInt(tokens[2]);
                    r.setWidth(width);
                    String color = String.valueOf(tokens[3]);
                    r.setColor(color);
                    Boolean filled = Boolean.valueOf(tokens[4]);
                    r.setFilled(filled);
                    r.getArea();
                    list.add(r);
                    System.out.println(r.toString());
                } else if (tokens.length == 1) {
                    String color = "white";
                    r.setColor(color);
                    Boolean filled = false;
                    r.setFilled(filled);
                    r.getArea();
                    list.add(r);
                    System.out.println(r.toString());
                }
            }
            }

        }
        public int getLargestObject() {
            int max = Integer.MIN_VALUE;
            for (int = 0; i < list.size(); i++){
                if (list.get(i) > max){
                    max = list.get(i);
                }
            }
            return max;
    }
}

Collections#max很有趣,但是很高級。 似乎您只需要更改if語句

 if (list.get(i) > max){

這樣使用該區域

 if (list.get(i).getArea() > max){

但是您說過“返回其位置並輸出對象的內容”,所以使用max來跟蹤位置,而不是對象,所以

    public int getLargestObjectIndex() {
        int maxIndex = 0;
        for (int = 1; i < list.size(); i++){
            // compare the area of the current index to the max index
            if (list.get(i).getArea() > list.get(maxIndex).getArea()){
                // if this one is bigger, save the location
                maxIndex = i;
            }
        }
        // output the contents of the object
        System.out.println(list.get(maxIndex).toString())
        // return the location
        return maxIndex;
   }

請注意,此循環從i = 1開始,因為maxIndex從0開始

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM