问题描述
我想尝试实现一种迭代2d数组中值的所有可能变化的方式。
类似于一组一维数组可以完成的操作,例如:
fun setYgosuData(tmpDoc: Elements?) : ArrayList<YgosuData> {
var tmpList = ArrayList<YgosuData>()
if (tmpDoc != null) {
//Log.d(TAG,"element : $tmpDoc")
for (e in tmpDoc) {
var title = e.select("td.tit a")
var name = e.select("td.name a")
var read = e.select("td.read")
var date = e.select("td.date")
var url = e.select("td.tit a").attr("href").toString()
var tmpYgosuData = YgosuData(
title = title.text(),name = name.text(),read = read.text(),date = date.text(),url = url
)
tmpList.add(tmpYgosuData)
}
}
Log.d(TAG,"여기2 ${tmpList.toString()}")
return tmpList
}
,然后使用嵌套的for循环进行遍历:
String[] pos = {"1","2","3","4","5"};
String[] color = {"green","red","white","black","blue"};
String[] cars = {"xtrail","camry","civic","accent","barina"};
String[] home = {"canada","british","french","chinese","indian"};
String[] location = {"sydney","goldcoast","newcastle","tamworth","portmaq"};
String[] time = {"5am","6am","7am","8am","9am"};
for (int a = 0; a < 5; a++) {
for (int b = 0; b < 5; b++) {
for (int c = 0; c < 5; c++) {
for (int d = 0; d < 5; d++) {
for (int e = 0; e < 5; e++) {
for (int f = 0; f < 5; f++) {
String[] ans = {pos[a],color[b],cars[c],home[d],location[e],time[f]};
}
}
}
}
}
}
数组保存着可能的汽车的当前迭代。
可以使用2d数组而不是5个不同的1d数组来完成此操作吗?
ans
要获得与String[][] travel = { { "1 ","green ","xtrail ","canada ","sydney ","5am" },{ "2 ","red ","camry ","british ","goldcoast ","6am" },{ "3 ","white ","civic ","newcastle ","7am" },{ "4 ","black ","accent ","chinese ","tamworth ","8am" },{ "5 ","blue ","barina ","indian ","portmaq ","9am" } };
相同的结果,只是同时使用2d阵列同时组合5辆汽车的所有可能组合?
我知道这不是我尝试做的最好的方法,但是我正在尝试学习如何使用这些数组。预先感谢。
解决方法
您可以使用数组的转置或以该顺序遍历数组。类似于此代码
import java.util.*;
class test {
static String[][] travel = {
{ "1 ","2 ","3 ","4 ","5 " },{ "green ","red ","white ","black ","blue " },{ "xtrail ","camry ","civic ","accent ","barina " },{ "canada ","british ","french","chinese ","indian " },{ "sydney ","goldcoast ","newcastle ","tamworth ","portmaq " },{ "5am","6am","7am","8am","9am" }
};
public static void generate(String travel[][]) {
_generate(travel,new String[travel.length]);
}
private static void _generate(String travel[][],int level,String ans[]) {
for (int i = 0; i < travel[level].length; i++) {
ans[level] = travel[level][i];
if (level + 1 == travel.length) {
/* consume the answer here */
System.out.println(Arrays.toString(ans));
} else {
_generate(travel,level + 1,ans);
}
}
}
/* usage */
public static void main(String a[]) {
generate(travel);
}
}
更通用的答案是使用使用者界面并推广该解决方案
import java.util.*;
import java.util.function.*;
class test {
static String[][] travel = {
{ "1 ","9am" }
};
public static void generate(String travel[][],Consumer<String[]> f) {
_generate(travel,new String[travel.length],f);
}
private static void _generate(String travel[][],String ans[],Consumer<String[]> f) {
for (int i = 0; i < travel[level].length; i++) {
ans[level] = travel[level][i];
if (level + 1 == travel.length) {
f.accept(ans);
} else {
_generate(travel,ans,f);
}
}
}
/* usage */
public static void main(String a[]) {
generate(travel,x -> System.out.println(Arrays.toString(x)));
}
}
,
这可以通过找到行的每一列的排列并将其存储在一维数组outputRow
中来解决。
这是通过递归调用print
函数来实现的,如下所示:
public class StackOverflow
{
public static void main(String[] args)
{
String[][] travel =
{
{ "1 ","green ","xtrail ","canada ","sydney ","5am" },{ "2 ","6am" },{ "3 ","7am" },{ "4 ","8am" },{ "5 ","blue ","barina ","indian ","portmaq ","9am" }
};
int row = travel.length;
int col = travel[0].length;
String[] outputRow = new String[col];
print(0,row,col,travel,outputRow);
}
static void print(int j,int row,int col,String[][] first,String[] outputRow)
{
for (int i = 0; i < row; i++)
{
outputRow[j] = first[i][j];
//Recursively continue to populate outputRow until we reach the last column i.e. (j == col -1)
if (j < col - 1)
print(j + 1,first,outputRow);
// We have reached the last column (j == col -1),so we print the current permutation.
if (j == col - 1)
{
for (int k = 0; k < col; k++)
System.out.print(" " + outputRow[k]);
System.out.println();
}
}
}
}
- 在这里,我们创建一个名为
outputRow
的一维String数组,并将其作为参数之一传递给print
函数。 -
print
函数每次递归处理一列,并继续将其保存在outputRow
数组中。 - 列处理从
j == 0
开始,这是传递给print
函数的第一个参数。 - 到达最后一列
j == col - 1;
时,将打印当前行的排列。
以上4点代表一行中每个唯一排列的1个递归循环。对2D数组中的所有行重复此循环。
,通过这种方式输入2D数组将更容易解决它:
String[][] travel = {
{ "1","2","3" },{ "green","red","white" },{ "xtrail","camry","civic" }
};
固定大小的2D数组的解决方案(出于解释目的,我将大小减小为3。解决方法不变)
for (String str : travel[0]) {
for (String str1 : travel[1]) {
for (String str2 : travel[2]) {
String[] ans = { str,str1,str2 };
System.out.println(Arrays.asList(ans));
}
}
}
对于通用解决方案,您可以创建一个递归函数,如下所示:
static void addOps(String[] ans,String[][] travel,int i) {
if (i == travel.length) {
System.out.println(Arrays.asList(ans));
} else if (i < travel.length) {
for (String str : travel[i]) {
ans[i] = str;
addOps(ans,i+1);
}
}
}
然后从main方法调用它:
String[] ans = new String[travel.length];
addOps(ans,0);