Cartesian Java中未绑定数量的数组的产品

时间:2016-05-27 17:06:00

标签: java arrays algorithm depth-first-search breadth-first-search

假设您有一个未绑定的命名数组列表:

ArrayNode[] vertexes = new ArrayNode[] {
    new ArrayNode("nouns", new String[]{"John", "Mary"}),
    new ArrayNode("verbs", new String[]{"Eats", "Works", "Plays"}),
    new ArrayNode("objects", new String[]{"Food", "Computer", "Guitar"})
 };

根据要遵循的顺序,说" 名词 - >动词 - >对象 ",我们可以使用什么算法来关联所有给定顺序的数组元素?输出类似于以下内容:

["John Eats Food"], ["John Eats Computer"], ["John Eats Guitar"], 
["John Works Food"], ["John Works Computer"], ["John Works Guitar"], 
..., ..., ..., 
...,                 ["Mary Plays Computer"], ["Mary Plays Guitar"]

数组的数量是未绑定的,序列可以是给定数量的数组的任何数组。

解决方案

使用Java 8和Google Guava 19 Lists.cartesianProduct Iterative Cartesian Product in Java

查看答案

1 个答案:

答案 0 :(得分:0)

您可以以inorder方式遍历数组,以获得所需顺序的输出。