获取数组和相应索引的最高值

时间:2014-06-05 12:43:20

标签: java arrays

How to find biggest variable using while loop?的帮助下,我设法找到最高价值的东西 从一个数组,但后来我不知道结果属于哪个区域......

    String area1[] = new String[2];
    String area2[] = new String[2];
    String area3[] = new String[2];
    String area4[] = new String[2];
    String area5[] = new String[2];

    area1[0] = "15";
    area1[1] = "Area 1";
    area2[0] = "7";
    area2[1] = "Area 2";
    area3[0] = "11";
    area3[1] = "Area 3";
    area4[0] = "9";
    area4[1] = "Area 4";
    area5[0] = "7";
    area5[1] = "Area 5";

    int max = Math.max(Integer.valueOf(area1[0]), Math.max(Integer.valueOf(area2[0]), Math.max(Integer.valueOf(area3[0]), Math.max(Integer.valueOf(area4[0]), Integer.valueOf(area5[0])))));

所以,直到现在我知道数组的最高值(max),但我不知道哪个area属于...

我希望你能帮我解决这个问题。谢谢!

5 个答案:

答案 0 :(得分:1)

面向对象的方法会好得多。创建一个Area类:

public class Area {    
    private final int areaNumber;
    private final int someOtherValue;

    public Area(int areaNumber, int someOtherValue) {
        this.areaNumber = areaNumber;
        this.someOtherValue = someOtherValue;
    }

    @Override
    public String toString() {
        return String.format("Area: %d (%d)", areaNumber, someOtherValue);
    }
}

然后像这样使用它:

List<Area> areas = Arrays.asList(
        new Area(1, 15), 
        new Area(2, 7),
        new Area(3, 11)); // etc

Area maxArea = Collections.max(areas, new Comparator<Area>() {
    @Override
    public int compare(Area o1, Area o2) {
        return Integer.compare(o1.someOtherValue, o2.someOtherValue);
    }
});

System.out.println(maxArea);

这将打印:Area: 1 (15)

根据您的具体用例,如果您始终打算以相同方式订购实例,则可能希望Area类实施Comparable<Area>

答案 1 :(得分:0)

将所有区域放在一个数组中,迭代查找最大值,然后保存该值和区域名称。我有代码返回一个包含两条信息的字符串,但如果您只需要知道最大区域,则只能返回名称。

String[][] areas {area1,area2,area3,area4,area5};
int maxValue=-MAX_INTEGTER;
String maxArea = "";
for(int i=0; i<areas.size; i++){
    if(Integer.parseInt(areas[i][0]) >= maxValue){
        maxValue = Integer.parseInt(areas[i][0]);
        maxArea = areas[i][1];
    }
 }
 return maxArea +" : " maxValue;

这将返回一个字符串,如:&#34;区域3:11&#34;

答案 2 :(得分:0)

如果你想使用5个数组,你必须做这样的事情:

int max = Math.max(Integer.valueOf(area1[0]);
String area = "area1";
int tmp = Math.max(Integer.valueOf(area2[0]);
if(max < tmp) {
  max = tmp;
  area = "area2";
}
tmp = Math.max(Integer.valueOf(area3[0]);
if(max < tmp) {
  max = tmp;
  area = "area3";
}
tmp = Math.max(Integer.valueOf(area4[0]);
if(max < tmp) {
  max = tmp;
  area = "area4";
}
tmp = Math.max(Integer.valueOf(area5[0]);
if(max < tmp) {
  max = tmp;
  area = "area5";
}

答案 3 :(得分:0)

您可以使用此功能:

public static String[] max(String[]... input) {
    String[] max = null;
    int maxVal = -1;

    for (String[] strings : input) {
        int value = Integer.valueOf(strings[0]);
        if(value > maxVal) {
            maxVal = value;
            max = strings;
        }
    }

    return max;
}

这会将所有数组作为输入,并返回具有最大值的数组。

用法:

    String area1[] = new String[2];
    String area2[] = new String[2];
    String area3[] = new String[2];
    String area4[] = new String[2];
    String area5[] = new String[2];

    area1[0] = "15";
    area1[1] = "Area 1";
    area2[0] = "7";
    area2[1] = "Area 2";
    area3[0] = "11";
    area3[1] = "Area 3";
    area4[0] = "9";
    area4[1] = "Area 4";
    area5[0] = "7";
    area5[1] = "Area 5";

    System.out.println(Arrays.toString(max(area1,area2,area3,area4,area5)));

输出:

[15, Area 1]

答案 4 :(得分:0)

好吧,既然您使用的是Java,我建议使用Java的精细机制,例如类,集合和自定义比较器:

class Area {

    public int number;
    public String name;

    public Area(int number, String name) {
        this.number = number;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Area{" + "number=" + number + ", name=" + name + '}';
    }

}

...

ArrayList<Area> areas = new ArrayList<>();
areas.add(new Area(15, "Area 1"));
areas.add(new Area(7, "Area 2"));
areas.add(new Area(11, "Area 3"));
areas.add(new Area(9, "Area 4"));
areas.add(new Area(7, "Area 5"));

Area max = Collections.max(areas, new Comparator<Area>() {
    @Override
    public int compare(Area o1, Area o2) {
        return o1.number - o2.number;
    }
});

System.out.println(max);