开发者

Java ArrayList Choose N elements

开发者 https://www.devze.com 2023-04-11 14:30 出处:网络
Say I have an ArrayList containing the elements {1,2,3,4}, and I want to enumerate all pos开发者_如何转开发sible combinations of two elements in the ArrayList.i.e. (1,2), (1,3), (1,4), (2,3), (2,4), (

Say I have an ArrayList containing the elements {1,2,3,4}, and I want to enumerate all pos开发者_如何转开发sible combinations of two elements in the ArrayList. i.e. (1,2), (1,3), (1,4), (2,3), (2,4), (3,4). What is the most elegant way of going about doing this?


Nested for loops would work:

for (int i = 0; i < arrayList.size(); ++i) {
    for (int j = i + 1; j < arrayList.size(); ++j) {
        // Use arrayList.get(i) and arrayList.get(j).
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消