二维数组中一维数组的搜索索引并逐行返回java
我有一个问题,我有一个一维数组二维数组中一维数组的搜索索引并逐行返回java,java,arrays,search,multidimensional-array,bigdata,Java,Arrays,Search,Multidimensional Array,Bigdata,我有一个问题,我有一个一维数组 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" } }; 我需要在数组二维中搜
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流解决方案:
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);
使用问题中的内容,以非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[] 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();
}
我觉得您应该使用字符串[]的列表而不是字符串[],因为结果的初始化非常简单。此外,在大多数情况下,列表更有效率和帮助 我只是想说清楚。。。您希望从purposeAll[]中获取每个值,并检查其值是否是二维数组值列表中的第一个值。如果是这样,将其拉出并将整个字符串[]放入arrayOfPurpose?为什么arrayOfPurpose 3d出现在您的代码中?@AdrianLeonhard-这不是3d问题是什么?到目前为止,您的代码有什么问题?@Ascalonian String[][]arrayOfPurpose=新字符串[purposeAll.length][purpose.length][];我只是想说清楚。。。您希望从purposeAll[]中获取每个值,并检查其值是否是二维数组值列表中的第一个值。如果是这样,将其拉出并将整个字符串[]放入arrayOfPurpose?为什么arrayOfPurpose 3d出现在您的代码中?@AdrianLeonhard-这不是3d问题是什么?到目前为止,您的代码有什么问题?@Ascalonian String[][]arrayOfPurpose=新字符串[purposeAll.length][purpose.length][];我得到的结果是:P3lmnopq,但不是P1和P2。我得到的结果是:P3lmnopq,但不是P1和P2。没有数组。asList.contains?伙计,这和我的想法差不多,但我面临的问题是无法插入数组[][]:@DaraTith-看看新的评论。我用这种逻辑使它工作起来:-@AdrianLeonhard-有很多方法可以检查。由于OP似乎正在学习这方面的知识,我想我会给出一个更直接的示例no Arrays.asList.contains?buddy,这几乎与mind相似,但我面临的问题是无法插入数组[]:@DaraTith-看看新的评论。我用这种逻辑使它工作起来:-@AdrianLeonhard-有很多方法可以检查。由于OP似乎正在学习这方面的知识,我想我会给出一个更直接的例子