打印二维锯齿状数组中的字符串组合 [英] Print combinations of strings in a 2D jagged array

查看:38
本文介绍了打印二维锯齿状数组中的字符串组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符串数组,如下所示:

Say I have an array of strings that look like:

{{"blue", "red"}, {"1", "2", "3"}, {"dog", "cat", "fish", "bird"}}

我想打印数组的组合:

blue 1 dog
blue 1 cat
...
...
red 3 bird

但是我希望锯齿状数组具有用户指定的行和列.我如何创建一个 类似的方法 但以动态和迭代的方式?此外,我正在使用数组而不是 ArrayList,因为作为初学者,我想在学习 ArrayList 之前看看我可以用数组做什么.我的代码如下:

However I want the jagged array to have rows and column that are user specified. How can I create a similar approach but in a dynamic and iterative way? Also I'm working with arrays and not ArrayList because as a beginner I want to see what I can do with arrays before learning ArrayList. My code is below:

Scanner input = new Scanner(System.in);
System.out.print("Enter number of arrays: ");
int arrays = input.nextInt();
String[][] array = new String[arrays][];

for (int i = 0; i < x; i++) {
    System.out.print("Enter number of elements for array: ");
    int elements = input.nextInt();
    input.nextLine();
    arrays[i] = new String[elements];

    for (int j = 0; j < elements; j++) {
        System.out.print("Enter string: ");
        String word = input.nextLine();
        arrays[i][j] = word;
    }
}

推荐答案

此答案将打印所有组合,不使用递归,但如果组合总数超过 Long.MAX_VALUE,则会失败.因为无论如何打印这么多行都不会结束,所以这不是真正的问题.

This answer will print all combinations, without use of recursion, but will fail if the total number of combinations exceeds Long.MAX_VALUE. Since printing that many lines would never end anyway, that's not really a problem.

要按顺序打印组合,请考虑递增的数字,其中数字的每一位都是相应子列表的索引.

To print combinations in order, think of an incrementing number, where each digit of the number is an index into the corresponding sub-list.

示例(使用问题列表中的列表):

Examples (using list of list from question):

000: blue 1 dog
001: blue 1 cat
002: blue 1 fish
003: blue 1 bird
010: blue 2 dog
...
121: red 3 cat
122: red 3 fish
123: red 3 bird

每个数字"在到达相应子列表的末尾时都会翻转,例如最后一个子列表只有 4 个元素,所以数字从 3 滚动到 0.

Each "digit" will roll over when it reaches end of corresponding sublist, e.g. last sublist only has 4 elements, so digit rolls over from 3 to 0.

注意:一个数字"可以大于 9.想想十六进制的一种表示方式.

Note: A "digit" can count higher than 9. Think hex for one way to represent that.

现在,数字的数量也是动态的,即外部列表的大小.使用简单循环执行此操作的一种方法是计算组合总数 (2 * 3 * 4 = 24),然后使用除法和余数计算数字.

Now, the number of digits is dynamic too, i.e. the size of the outer list. One way to do this with simple loops, is to calculate the total number of combinations (2 * 3 * 4 = 24), then calculate the digits using division and remainder.

示例:

Combination #10 (first combination is #0):
  10 % 4                 = 2 (last digit)
  10 / 4 % 3     = 2 % 3 = 2 (middle digit)
  10 / 4 / 3 % 2 = 0 % 2 = 0 (first digit)
  Digits: 022 = blue 3 fish

为了解决这个问题,我们首先构建一个除数数组,例如div[] = { 12, 4, 1 },求组合总数(24).

To help with this, we first build an array of divisors, e.g. div[] = { 12, 4, 1 }, and find the total number of combinations (24).

long[] div = new long[array.length];
long total = 1;
for (int i = array.length - 1; i >= 0; i--) {
    div[i] = total;
    if ((total *= array[i].length) <= 0)
        throw new IllegalStateException("Overflow or empty sublist");
}

现在我们可以遍历组合并打印结果:

Now we can loop through the combinations and print the result:

for (long combo = 0; combo < total; combo++) {
    for (int i = 0; i < array.length; i++) {
        int digit = (int) (combo / div[i] % array[i].length);
        if (i != 0)
            System.out.print(' ');
        System.out.print(array[i][digit]);
    }
    System.out.println();
}

来自问题的输入:

String[][] array = {{"blue", "red"}, {"1", "2", "3"}, {"dog","cat", "fish", "bird"}};

我们得到以下输出:

blue 1 dog
blue 1 cat
blue 1 fish
blue 1 bird
blue 2 dog
blue 2 cat
blue 2 fish
blue 2 bird
blue 3 dog
blue 3 cat
blue 3 fish
blue 3 bird
red 1 dog
red 1 cat
red 1 fish
red 1 bird
red 2 dog
red 2 cat
red 2 fish
red 2 bird
red 3 dog
red 3 cat
red 3 fish
red 3 bird

它可以处理子数组的任意组合,例如具有 4 个大小为 2、3、2 和 2 的子数组:

It can handle any combination of sub-arrays, e.g. with 4 sub-arrays of sizes 2, 3, 2, and 2:

String[][] array = {{"small", "large"}, {"black", "tan", "silver"}, {"lazy", "happy"}, {"dog", "cat"}};

small black lazy dog
small black lazy cat
small black happy dog
small black happy cat
small tan lazy dog
small tan lazy cat
small tan happy dog
small tan happy cat
small silver lazy dog
small silver lazy cat
small silver happy dog
small silver happy cat
large black lazy dog
large black lazy cat
large black happy dog
large black happy cat
large tan lazy dog
large tan lazy cat
large tan happy dog
large tan happy cat
large silver lazy dog
large silver lazy cat
large silver happy dog
large silver happy cat

这篇关于打印二维锯齿状数组中的字符串组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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