将数组乘以 JAVA 中的数组
Multiply arrays by arrays in JAVA
例如,我有三个数组(但我可以有更多),其中一些值如下:
table_1 = [a,b,c]; //three elements
table_2 = [d]; //one elements
table_3 = [e,f]; //two elements
我想得到那个输出
<test>
<test_1>a</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>a</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
<test>
<test_1>b</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>b</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
<test>
<test_1>c</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>c</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
也许 JAVA 中有人知道如何做到这一点。如果我有两个数组,我可以用两个 for (for in for) 来做到这一点,但我必须更通用地做到这一点。
是这样的吗?
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MatrixCross {
public static void cross(String[]... matrix){
cross(0,matrix, Collections.EMPTY_LIST);
}
private static void cross(int index,String[][] matrix, List<String> result){
if (index >= matrix.length){
System.out.println("<test>");
int i = 1;
for (String str : result) {
System.out.println(" <test_"+i+">"+str+"</test_"+i+">");
i++;
}
System.out.println("</test>");
} else {
for (int i = 0; i < matrix[index].length; i++) {
List<String> values = new ArrayList<>(result);
values.add(matrix[index][i]);
cross(index+1,matrix,values);
}
}
}
public static void main(String[] args) {
String [] table1 = {"a", "b", "c"};
String [] table2 = {"d"};
String [] table3 = {"e","f"};
cross(table1,table2,table3);
}
}
输出:
<test>
<test_1>a</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>a</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
<test>
<test_1>b</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>b</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
<test>
<test_1>c</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>c</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
假设您有数组列表
List<String[]> list;
您只需要了解如何在基于变量的系统中递增数字。
它与以 10 为基础的常量没有什么不同。
您尝试递增最后一位数字,如果有溢出则将其设置为 0 并尝试递增下一位数字。
所以我们需要数字的表示。让我们创建包含数字的数组。
int[] number = new int[list.size()];
要增加它,我们需要一个当前数字的变量;
int current = 0;
我们将遍历我们的数字,直到我们可以在不溢出的情况下递增数字
while(true){
if(number[current] == list.get(current).length){
//Overflow happend
number[current] = 0;
current++;
} else {
//We can increment current digit
number[current]++;
break;
}
if(current == list.size()){
//we out of digits
throw new OverflowException();
}
}
现在我们知道如何增加我们的数字了。您可以循环执行直到发生异常并获取所有可能的数字。
将数字解码为 XML 很明显。
这是我能想到的最简单的解决方案:
public class Test {
public static String[][] array = {
{"a", "b", "c"},
{"d"},
{"e", "f"}
};
public void recurse(String result, int i) {
if(i >= array.length) {
printResult(result);
return;
}
for (String element : array[i]) {
recurse(result + element, i + 1);
}
}
public void printResult(String result) {
System.out.println("<test>");
for(int i = 0; i < result.length(); i++) {
System.out.print(" <test" + (i + 1) + ">" + result.charAt(i) + "</test" + (i + 1) + ">");
System.out.println();
}
System.out.println("</test>");
}
public static void main(String[] args) {
Test test = new Test();
test.recurse("", 0);
}
}
例如,我有三个数组(但我可以有更多),其中一些值如下:
table_1 = [a,b,c]; //three elements
table_2 = [d]; //one elements
table_3 = [e,f]; //two elements
我想得到那个输出
<test>
<test_1>a</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>a</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
<test>
<test_1>b</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>b</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
<test>
<test_1>c</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>c</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
也许 JAVA 中有人知道如何做到这一点。如果我有两个数组,我可以用两个 for (for in for) 来做到这一点,但我必须更通用地做到这一点。
是这样的吗?
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MatrixCross {
public static void cross(String[]... matrix){
cross(0,matrix, Collections.EMPTY_LIST);
}
private static void cross(int index,String[][] matrix, List<String> result){
if (index >= matrix.length){
System.out.println("<test>");
int i = 1;
for (String str : result) {
System.out.println(" <test_"+i+">"+str+"</test_"+i+">");
i++;
}
System.out.println("</test>");
} else {
for (int i = 0; i < matrix[index].length; i++) {
List<String> values = new ArrayList<>(result);
values.add(matrix[index][i]);
cross(index+1,matrix,values);
}
}
}
public static void main(String[] args) {
String [] table1 = {"a", "b", "c"};
String [] table2 = {"d"};
String [] table3 = {"e","f"};
cross(table1,table2,table3);
}
}
输出:
<test>
<test_1>a</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>a</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
<test>
<test_1>b</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>b</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
<test>
<test_1>c</test_1>
<test_2>d</test_2>
<test_3>e</test_3>
</test>
<test>
<test_1>c</test_1>
<test_2>d</test_2>
<test_3>f</test_3>
</test>
假设您有数组列表
List<String[]> list;
您只需要了解如何在基于变量的系统中递增数字。 它与以 10 为基础的常量没有什么不同。 您尝试递增最后一位数字,如果有溢出则将其设置为 0 并尝试递增下一位数字。
所以我们需要数字的表示。让我们创建包含数字的数组。
int[] number = new int[list.size()];
要增加它,我们需要一个当前数字的变量;
int current = 0;
我们将遍历我们的数字,直到我们可以在不溢出的情况下递增数字
while(true){
if(number[current] == list.get(current).length){
//Overflow happend
number[current] = 0;
current++;
} else {
//We can increment current digit
number[current]++;
break;
}
if(current == list.size()){
//we out of digits
throw new OverflowException();
}
}
现在我们知道如何增加我们的数字了。您可以循环执行直到发生异常并获取所有可能的数字。
将数字解码为 XML 很明显。
这是我能想到的最简单的解决方案:
public class Test {
public static String[][] array = {
{"a", "b", "c"},
{"d"},
{"e", "f"}
};
public void recurse(String result, int i) {
if(i >= array.length) {
printResult(result);
return;
}
for (String element : array[i]) {
recurse(result + element, i + 1);
}
}
public void printResult(String result) {
System.out.println("<test>");
for(int i = 0; i < result.length(); i++) {
System.out.print(" <test" + (i + 1) + ">" + result.charAt(i) + "</test" + (i + 1) + ">");
System.out.println();
}
System.out.println("</test>");
}
public static void main(String[] args) {
Test test = new Test();
test.recurse("", 0);
}
}