[英]Printing numbers in array
Im trying to write a function to display all combinations in a jagged array, where each combination contains one element from each sub-array. 我试图编写一个函数来显示锯齿状数组中的所有组合,其中每个组合包含每个子数组中的一个元素。 The jagged array can consist of any number of arrays and each array can have any number of elements.
锯齿状数组可以由任意数量的数组组成,每个数组可以具有任意数量的元素。 Eg for the following array: a[0] = {1, 3, 5} a[1] = {2, 4} it should return: (1, 2) (1, 4) (3, 2) (3, 4) (5, 2) (5, 4)
例如,以下数组:a [0] = {1,3,5} a [1] = {2,4}它应该返回:(1,2)(1,4)(3,2)(3, 4)(5,2)(5,4)
I thought of doing it this way but immediately run into trouble. 我曾想过要这样做,但立即遇到麻烦。 Logically it looks OK to get 1, 2 and 1, 4 but then next run I is set back to 0 (sorry not at devel machine to test now).
从逻辑上讲,获得1、2和1、4看起来不错,但是接下来将我的运行I设置回0(抱歉,现在不是在devel机器上进行测试)。 Can anyone suggest a better solution please?
有人可以提出更好的解决方案吗?
Here is my code 这是我的代码
for (int i = 0; i < array1.length(); i++)
for (int j = 0; j < array2.length(); j++)
if (j < array2.length())
i = 0;
else
i++;
System.out.println(array1[i] "," array2[j])
You don't need this: 您不需要这个:
if (j < array2.length())
i = 0;
else
i++;
i is incremented automatically in a for loop. 我在for循环中自动递增。
This should be fine: 这应该很好:
for (int i = 0; i < array1.length(); i++)
for (int j = 0; j < array2.length(); j++)
System.out.println(array1[i] "," array2[j])
If I'm understanding your question correctly (which I might not be) I think all you need is just 如果我正确理解了您的问题(可能不是),我想您所需要的只是
for (int i = 0; i < array1.length(); i++){
for (int j = 0; j < array2.length(); j++){
System.out.println(array1[i] "," array2[j]);
}
}
to achieve the desired result 达到预期的效果
How about this: 这个怎么样:
int a [] = {1,2,3}; int a [] = {1,2,3}; int b[] = {1,2};
int b [] = {1,2};
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < a.length; j++) {
System.out.println(a[i]+","+a[j]);
}
}
Your if
statement inside the loop breaks everything. 您在循环内的
if
语句会破坏所有内容。 You just need 2 nested loop to complete your task: 您只需要2个嵌套循环即可完成任务:
for (int i = 0; i < array1.length(); i++)
for (int j = 0; j < array2.length(); j++) {
System.out.println(array1[i] + "," + array2[j]);
}
}
for (int i = 0; i < array1.length(); i++)
for (int j = 0; j < array2.length(); j++)
System.out.println("(" + array1[i] + "," array2[j] + ")");
Here's a general solution that works with any number of arrays (beware the exponential nature of the runtime of this algorithm): 这是适用于任意数量数组的通用解决方案(请注意此算法运行时的指数性质):
int[][] arrays = new int[][]
{
{1, 2, 3, 4, 5, 6},
{1, 2, 3, 4, 5, 6},
{1, 2, 3, 4, 5, 6}
}; // let's print all fair rolls of a 3d6
if (arrays.length == 0) return; // this is why we can't have nice things
int[] currentPos = new int[arrays.length];
while(currentPos[arrays.length - 1] != arrays[arrays.length - 1].length)
{
// print the current value
System.out.print(arrays[0][currentPos[0]]);
for (int i = 1; i < arrays.length; ++i)
System.out.print(", " + arrays[i][currentPos[i]]);
System.out.println();
// increment the "counter"
++currentPos[0];
for (int i = 1; i < arrays.length; ++i)
{
if (currentPos[i - 1] == arrays[i - 1].length)
{
currentPos[i - 1] = 0;
++currentPos[i];
}
else break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.