Это менее запутанным, чтобы поместить подмассивы во временную (1D) массивы: row1Temp
и row2Temp
.
import java.util.Arrays;
/**
<P>{@code java DoubleArrayXmpl}</P>
**/
public class DoubleArrayXmpl {
public static final void main(String[] igno_red) {
String[] row1Output = new String[10];
String[] row2Output = new String[10];
String[][] fruit = {{"apple", "banana", "kiwi"},{"kiwi", "oranges", "grapes"}};
String[] row1Temp = fruit[0];
String[] row2Temp = fruit[1];
for(int i = 0; i < row1Temp.length; i++) {
for(int j = 0; j < row2Temp.length; j++) {
if(row1Temp[i].equals(row2Temp[j])) {
System.out.println("Match found");
row1Output[i] = row1Temp[i];
row2Output[j] = row2Temp[j];
}else{
System.out.println("Not found");
}
}
}
System.out.println("row1Output");
System.out.println(Arrays.deepToString(row1Output));
System.out.println("row2Output");
System.out.println(Arrays.deepToString(row2Output));
}
}
Выход:
[C:\java_code\]java DoubleArrayXmpl
Not found
Not found
Not found
Not found
Not found
Not found
Match found
Not found
Not found
row1Output
[null, null, kiwi, null, null, null, null, null, null, null]
row2Output
[kiwi, null, null, null, null, null, null, null, null, null]
Я не знаю ваших требований, но это немного странно, чтобы положить эти повторяющиеся значения в двух различных массивов, не говоря уже о выходе так много значений NULL. Как насчет использования ArrayList
для хранения одиночный экземпляр подходящих фруктов?
import java.util.Arrays;
/**
<P>{@code java DoubleArrayXmpl}</P>
**/
public class DoubleArrayXmpl {
public static final void main(String[] igno_red) {
ArrayList<String> alMatches = new ArrayList<String>(3);
String[][] fruit = {{"apple", "banana", "kiwi"},{"kiwi", "oranges", "grapes"}};
String[] row1Temp = fruit[0];
String[] row2Temp = fruit[1];
for(int i = 0; i < row1Temp.length; i++) {
for(int j = 0; j < row2Temp.length; j++) {
if(row1Temp[i].equals(row2Temp[j])) {
System.out.println("Match found");
alMatches.add(row1Temp[i]);
}else{
System.out.println("Not found");
}
}
}
System.out.println("All matched fruits:");
for(String s : alMatches) {
System.out.println(s);
}
}
}
Выход:
Not found
Not found
Not found
Not found
Not found
Not found
Match found
Not found
Not found
All matched fruits:
kiwi
Или еще лучше, просто хранить индексирует из матчей:
import java.util.Arrays;
import java.util.ArrayList;
/**
<P>{@code java DoubleArrayToMatchedIdxListXmpl}</P>
**/
public class DoubleArrayToMatchedIdxListXmpl {
public static final void main(String[] igno_red) {
ArrayList<Integer> alMatchIdxsInRow1 = new ArrayList<Integer>(3);
String[][] fruit = {{"apple", "banana", "kiwi"},{"kiwi", "oranges", "grapes"}};
String[] row1Temp = fruit[0];
String[] row2Temp = fruit[1];
for(int i = 0; i < row1Temp.length; i++) {
for(int j = 0; j < row2Temp.length; j++) {
if(row1Temp[i].equals(row2Temp[j])) {
System.out.println("Match found");
alMatchIdxsInRow1.add(i);
}else{
System.out.println("Not found");
}
}
}
System.out.println("All matched fruits:");
for(int i : alMatchIdxsInRow1) {
System.out.println(fruit[0][i]);
}
}
}
Выход:
Not found
Not found
Not found
Not found
Not found
Not found
Match found
Not found
Not found
All matched fruits:
kiwi
Почему вы хотите хранить два разных t с одинаковыми значениями? –
То же, что и в той же точной строке и точный индекс? –
@Mason Мне нужно сохранить их для создания некоторых форм предложений SQL, которые я буду использовать позже. – user11998