在二维数组中搜索一维数组的索引 return 逐行 java
Search index of one Dimensional array in two dimensional array and return row by row java
我有一个问题是我有一个一维数组
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]);
}
}
}
}
}
非常感谢!! :)
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]]
}
}
使用问题中的内容,以非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);
希望我理解正确。
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();
}
我觉得您应该使用 String[] 列表而不是 String[][],因为结果的初始化非常简单。在大多数情况下,列表也更有效率和帮助。
我有一个问题是我有一个一维数组
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]);
}
}
}
}
}
非常感谢!! :)
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]]
}
}
使用问题中的内容,以非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);
希望我理解正确。
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();
}
我觉得您应该使用 String[] 列表而不是 String[][],因为结果的初始化非常简单。在大多数情况下,列表也更有效率和帮助。