-1

现在我有一个名为“ID”的数组列表,其中包含一个数字列表,例如:

3  3  3  3  3  6  6  6  6  6  7  7  7  7  8  8  8  

每次程序运行时ID都会不同。

有没有一种方法可以创建一种情况,我可以将我的“ID”数组列表分解为仅包含相同数字的较小数组列表?我想不出一种方法来创建可以根据“ID”中有多少不同数字来创建一定数量的新数组列表的代码。

ArrayList<Double> ID = new ArrayList<Double>();
//ID is filled by a text file that will vary but will look something like 
    // 2 2 2 2 2 2 4 4 4 4 4 4 4 6 6 6 6 6 6 6 7 7 7 7 8 8 (all whole numbers)

我想要什么

ArrayList<Double> newArray1 = new ArrayList<Double>();
//contains 2 2 2 2 2 2 
ArrayList<Double> newArray2 = new ArrayList<Double>();
//contains 4 4 4 4 4 4 4
ArrayList<Double> newArray3 = new ArrayList<Double>();
//contains 6 6 6 6 6 6 6

等等

4

4 回答 4

1

要具有唯一值,请使用 aSet并让 java 完成工作。

要随时了解每种类型的值有多少,您可以使用Map<Integer,Integer>.

于 2013-06-05T13:57:23.767 回答
0

仅包含相同数字的较小数组列表?

所以你想要一个充满元素的数组列表都是平等的吗?为什么要这么做??

您可以通过只为每个数字使用一个计数器来实现您想要的。然后,您可以将所有计数器保存在Map.

Map<String, Integer> countMap = new HashMap<>();

对于计算 ID,您可以使用如下所示的方法:

public void incrementForID(String id, Map<String, Integer> countMap) {
    Integer c = countMap.get(id);
    if(c == null) {
         c = 0;
    }
    coutmap.put(id, c.intValue()+1);
}
于 2013-06-05T14:08:36.183 回答
0
public class OhMyGod {

    public List<ArrayList<Object>> answerThisQuestion(List<File> files) {

        ArrayList<ArrayList<Object>> returnValue = new ArrayList<>();

        for (File f: files) {
            int numberOfLists = magicallyGuessHowMany(f);
            for (int i = numberOfLists -1; numberOfLists != 0; numberOfLists--) {
                returnValue.add(new ArrayList<>());
            }
        }
        return returnValue;
    }

    public int magicallyGuessHowMany(File f){
        // answer my question here
    }
}

我想这不会满足您的需要。但是,它回答了您的问题

于 2013-06-06T08:57:55.653 回答
-1

您可以尝试使用List<ArrayList<MyObject>>.

因此,您可以根据ArrayList需要添加任意数量。

于 2013-06-05T13:34:41.450 回答