试图获得最低整数 [英] Trying to get lowest integers

查看:76
本文介绍了试图获得最低整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有4个列表,
我想找出哪个list.size();是最低的。
我可以找到最低价,但有些结果是相同的,这是
我所拥有的。

So I have 4 list, I want to find out which list.size(); is the lowest. I can find the lowest but some turn out to be the same, this is what I have.

              if(EAmount < DAmount && EAmount < GAmount && EAmount < IAmount){
        FinalDecision = "Emerald";
    }else if(DAmount < EAmount && DAmount < GAmount && DAmount < IAmount){
        FinalDecision = "Diamond";
    }else if(GAmount < EAmount && GAmount < DAmount && GAmount < IAmount){
        FinalDecision = "Gold";
    }else if(IAmount < EAmount && IAmount < DAmount && IAmount < GAmount){
        FinalDecision = "Iron";
    }

虽然我需要检查这两者之间的随机选择如果。例如,如果EAmount和DAmount都是2,但是Gold和Iron分别是3和4.我想要它,因此它发现EAmount和DAmount都是2然后随机选择其中一个。总而言之我想要它所以它会找到最低的int,如果有一个以上的最低int选择它之间的随机。我该怎么办? BTW:它并不总是如此,EAmount和DAmount是相同的。所有这些变量都是完全随机的。

Though I need it to check for the lowest than pick a random between those two in case. Example, if EAmount and DAmount are both 2, but then Gold and Iron are 3 and 4. I want it so it finds out that EAmount and DAmount are both 2 then pick one of them by random. So all in all "I want it so it will find the lowest int, if there is more than one of the lowest int pick a random between it." How would I do this? BTW: Its not always going to be so EAmount and DAmount are equal. All of these variables are completely random.

推荐答案

经过大量的逻辑研究后,我得到了解决方案,它100%工作,我测试了它

after lot of logic research i got solution,it works 100% and i tested it

首先使用treeset找到最小尺寸列表的大小

first found minimum sized list's size using treeset

将所有列表及其大小放入地图

put all list and their sizes into map

迭代地图以找到所有最小尺寸的列表(如果有多个)使用上面最小尺寸作为关键字并且只将那些放入finallist

iterate map to find all lowest sized list(if there are more than one) using above lowest size as a key and put only those into finallist

然后进入决赛并使用其大小随机数

then take finalist and take random number using its size

然后将此随机数传递给最终列表get方法随机获取相应的列表。

then pass this random number to final list get method get corresponding list randomly.

List list1 = new ArrayList();
    list1.add(1);
    list1.add(2);
    List list2 = new ArrayList();
    list2.add(1);
    list2.add(2);
    List list3 = new ArrayList();
    list3.add(1);
    list3.add(2);
    list3.add(1);
    List list4 = new ArrayList();
    list4.add(1);
    list4.add(2);
    list4.add(1);
    list4.add(2);


    Set<Integer> set = new TreeSet<>();
    set.add(list1.size());
    set.add(list2.size());
    set.add(list3.size());
    set.add(list4.size());

    List<Integer> list = new ArrayList<>(set);
    int min = list.get(0);
    System.out.println(min);


    Map<List, Integer> map = new HashMap<>();

    map.put(list1,list1.size());
    map.put(list2,list2.size());
    map.put(list3,list3.size());
    map.put(list4,list4.size());



    List finalList = new ArrayList<>();

    for (Map.Entry<List, Integer> entry : map.entrySet())
    {
        if(entry.getValue().equals(min)){
            finalList.add(entry.getKey());

        }

    }


    int finalKey = new Random().nextInt(finalList.size());

    System.out.println(finalList.get(finalKey));

这篇关于试图获得最低整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆