我遇到的问题是我有一个数组
String[] purposeAll= {"P1","P2"};
和数组二维
String[][] purpose = {
{ "P1", "a", "b", "c", "d", "e", "f", "h" },
{ "P2", "a", "b", "c", "d", "e", "g", "i" },
{ "P3", "l", "m", "n", "o", "p", "q" } };
我需要在数组二维中搜索一维数组的每个索引,结果将得到:
arrayOfPurpose[][]={
{ "P1", "a", "b", "c", "d", "e", "f", "h" },
{ "P2", "a", "b", "c", "d", "e", "g", "i" } };
我的代码是
public static void ArrayOfPurpose(String[][] purpose, String[] purposeAll) {
String[][][] arrayOfPurpose = new String[purposeAll.length][purpose.length][];
ArrayList<String[]> list = new ArrayList<String[]>();
for (int i = 0; i < purpose.length; i++) {
list.add(purpose[i]);
}
String[] tmp;
for (int k = 0; k < purposeAll.length; k++) {
arrayOfPurpose[k]= new String[purposeAll.length][];
System.out.print("ok");
for (int i = 0; i < list.size(); i++) {
tmp = list.get(i);
arrayOfPurpose[i]= new String[purposeAll.length][tmp.length];
for (int j = 0; j < purpose[i].length; j++) {
if (tmp[0] == (purposeAll[k])) {
arrayOfPurpose[k][i][j] = tmp[j];
System.out.println(arrayOfPurpose[k][i][j]);
}
}
}
}
}
非常感谢你! :)
答案 0 :(得分:2)
使用您在问题中的内容,以非Java8方式,您可以这样做:
for (String valueToCheck : purposeAll) {
for (String[] values : purpose) {
if (values[0].equals(valueToCheck)) {
list.add(values);
}
}
}
String[][] arrayOfPurpose = new String[list.size()][];
int index = 0;
for (String[] a : list) {
arrayOfPurpose[index++] = a;
}
System.out.println("Size of arrayOfPurpose: " + arrayOfPurpose.length);
答案 1 :(得分:1)
Java8流解决方案:
public class blah {
static String[] purposeAll= {"P1","P2"};
static String[][] purpose = {
{ "P1", "a", "b", "c", "d", "e", "f", "h" },
{ "P2", "a", "b", "c", "d", "e", "g", "i" },
{ "P3", "l", "m", "n", "o", "p", "q" } };
public static String[][] arrayOfPurpose(String[][] purpose, String[] purposeAll) {
return Arrays.asList(purpose).stream()
.filter(arr -> Arrays.asList(purposeAll).contains(arr[0]))
.toArray(String[][]::new);
}
public static void main(String[] args) {
System.out.println(Arrays.deepToString(arrayOfPurpose(purpose, purposeAll)));
// prints [[P1, a, b, c, d, e, f, h], [P2, a, b, c, d, e, g, i]]
}
}
答案 2 :(得分:1)
我希望我能正确理解这个问题。
String[] purposeAll= {"P1","P2"};
//String[] purposeAll= {"P3","P1"}; Testing Stuff.
String[][] purpose = {
{ "P1", "a", "b", "c", "d", "e", "f", "h" },
{ "P2", "a", "b", "c", "d", "e", "g", "i" },
{ "P3", "l", "m", "n", "o", "p", "q" } };
List<String[]> arrayOfPurposes = new ArrayList<>();
//Looping through the purposeAll to check purpose's first
//entry and seeing if it equals the first purposeAll value
for(int i = 0; i < purpose.length; i++){
for(int j = 0; j < purposeAll.length; j++){
if(purpose[i][0].equals(purposeAll[j])){
arrayOfPurposes.add(purpose[i]);
}
}
}
//One way to get the results into a String[][]
String[][] result = {arrayOfPurposes.get(0),arrayOfPurposes.get(1)};
//Displaying to console
for(int i = 0; i < result.length; i++){
for(int j = 0; j < result[i].length; j++){
System.out.print(result[i][j]);
}
System.out.println();
}
我觉得你应该使用List of String []而不是String [] [],因为结果的初始化非常简单。在大多数情况下,列表也更有效且更有帮助。