如何比较对象的arraylist中的一个值与输出最大值?

时间:2014-10-15 04:23:53

标签: java arraylist

我需要创建一个方法getLargestObject(),它找到数组列表中具有返回其位置的最大区域并输出对象内容的对象。我正在使用的当前循环不起作用,我不确定如何比较arraylist中的区域值,以便我可以得到最大的。

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;
    }
}

1 个答案:

答案 0 :(得分:1)

收藏#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开始