爪哇 - 如何建立唯一的对象的元组(n维)? [英] Java - How to build unique object tuples (n-dimension)?

查看:152
本文介绍了爪哇 - 如何建立唯一的对象的元组(n维)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要创建独特的元组对象从对象列表下面的挑战。该特殊车种零配件的挑战是如何在这里我能做到这一点的动态列表大小(n维)?这是很容易的情况下,你有固定的尺寸。我希望有人知道第三方的API或有一定的提示我怎么能达到这一点。

I have the following challenge that I need to create unique object tuples out of object lists. The speical challenge is here how I can do it for dynamic list sizes (n-dimension)? It is easy in case you have a fixed dimension. I hope somebody knows a third party API or has some hints for me how I can reach this.

的exampel下面示出了它的一个3列出,所以很容易解释。 我有排序的类列表如对象

The exampel below shows it for a 3 lists, so it is easier to explain. I have the objects sorted by its class in a list e.g.

lista = {a1,a2}
listb = {b1,b2,b3}
listc = {c1,c2}

我喜欢有下列元组,以使每个对象都与彼此成对的一次:

I like to have the following tuples, so that each object is paired once with each other:

tuple1  = {a1,b1,c1}
tuple2  = {a1,b1,c2}
tuple3  = {a1,b2,c1}
tuple4  = {a1,b2,c2}
tuple5  = {a1,b3,c1}
tuple6  = {a1,b3,c2}
tuple7  = {a2,b1,c1}
tuple8  = {a2,b1,c2}
tuple9  = {a2,b2,c1}
tuple10 = {a2,b2,c2}
tuple11 = {a2,b3,c1}
tuple12 = {a2,b3,c2}

如何才能做到这一点的案例 - - 名单的数目动态
是 - 列表的大小是动态

How can I achieve that in case
- the number of lists is dynamically
- the size of lists is dynamically

任何提示或想法?谢谢你在前进

any hints or ideas? Thank you in advance

推荐答案

我们可以建立的解决方案如下。首先,我们注意到,在该列表的大小不同的情况下从来没有在现代语言的一个问题,因为几乎所有当今语言支持可变大小的列表。在棘手的部分是,当列表的数量而变化。

We can build up to the solution as follows. First notice that the case in which the sizes of the lists vary is never a problem in modern languages, because virtually all languages today support the variable-sized list. The trickier part is when the number of lists vary.

下面是Python的情况下列表的固定数量:

Here is the Python case for the fixed number of lists:

lista = ["a1","a2"]
listb = ["b1","b2","b3"]
listc = ["c1","c2"]
[(a,b,c) for a in lista for b in listb for c in listc]

对于Java做同样的事情。您将需要三个嵌套for循环:

For Java do the same thing. You will need three nested for-loops:

List<String> lista = Arrays.asList("a1","a2");
List<String> listb = Arrays.asList("b1","b2","b3");
List<String> listc = Arrays.asList("c1","c2");

List<List<String>> result = new ArrayList<List<String>>();
for (String a: lista) {
    for (String b: listb) {
        for (String c: listc) {
            result.add(Arrays.asList(a, b, c));
        }
    }
}
System.out.println(result);

这产生了

[[a1, b1, c1], [a1, b1, c2], [a1, b2, c1], [a1, b2, c2], [a1, b3, c1], [a1, b3, c2], [a2, b1, c1], [a2, b1, c2], [a2, b2, c1], [a2, b2, c2], [a2, b3, c1], [a2, b3, c2]]

现在关键是你怎么做一个不确定数的循环?一种方法是使用递归。

Now the trick is how do you do an indeterminate number of for loops? One way is to use recursion.

先从0列表(甚至是1),然后问自己的基本情况:怎样的结果的变化,当我加入一个新的列表?然后,你可以把一个可爱的小递推公式。你需要帮助吗?

Start with the base case of 0 lists (or even 1) and then ask yourself: how does the result change when I add a new list? Then you can put together a nice little recursive formulation. Do you need help with this?

(旁白:在Python中,我们有 itertools 对这类事情,我不知道有任何的Java模拟,但一些谷歌上搜索可能变成的东西了)

(Aside: In Python we have itertools for these kinds of things. I am not aware of any Java analog, but some googling might turn something up.)

好了,让我们开发的递推组列表无限多个。如果你只有唯一的一个列表,说

Okay let's develop the recursive formulation for an unbounded number of lists. If you have only only one list, say

a = ["a1", "a2"]

那么结果是 [A1],[A2]] 。但现在,让我们添加另一个列表:

then the result is [["a1"],["a2"]]. But now let's add another list:

a = ["b1", "b2"]

,答案是什么呢?

What is the answer now?

[[a1, b1], [a1, b2], [a2, b1], [a2, b2]]

我们怎么会呢?答案是,我们采取了新的列表中的每个元素,并追加到结果中的每个元素。我们每次增加一个新的列表,我们将这样做。假设现在我们添加 [C1,C2,C3] 。你看看我们会得到什么呢?如果是的话,你现在应该可以定义递归步骤!

How did we get that? The answer is we took each element of the new list and appended it to each element in the result. And every time we add a new list we will do the same. Suppose now we add ["c1","c2","c3"]. Do you see what we will get? If so, you should now be able to define the recursive step!

完整code

好吧,我无法抗拒!在这里,你走了。

Okay I could not resist! Here you go.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * An application requested by a SO user.
 */
public class ProductExample {

    /**
     * Returns a list containing all possible products of a list of lists of strings.
     */
    public static List<List<String>> product(List<List<String>> lists) {
        List<List<String>> result = new ArrayList<List<String>>();

        if (lists.isEmpty()) {
            result.add(new ArrayList<String>());
            return result;
        }

        List<List<String>> partial = product(lists.subList(0, lists.size() - 1));
        for (List<String> list: partial) {
            for (String s: lists.get(lists.size() - 1)) {
                List<String> listCopy = new ArrayList<String>(list);
                listCopy.add(s);
                result.add(listCopy);
            }
        }
        return result;
    }


    public static void main(String[] args) {
        System.out.println(product(
                Arrays.asList(
                        Arrays.asList("a1", "a2"),
                        Arrays.asList("b1", "b2", "b3"),
                        Arrays.asList("c1", "c2"),
                        Arrays.asList("d1", "d2"))));
    }
}

咆哮:这是sooooooo在Python要容易得多。或Ruby。 :)

Rant: This is sooooooo much easier in Python. Or Ruby. :)

这篇关于爪哇 - 如何建立唯一的对象的元组(n维)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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