我试图比较两行与2D数组不同的行,并存储两行中相同的元素。以下是我所制作的一个例子:
String[] row1 = new String[10];
String[] row2 = new String[10];
String[][] fruit = {{"apple", "banana", "kiwi"},{"kiwi", "oranges", "grapes"}};
for (int i = 0; i < fruit.length; i++ ) {
for (int j = 0; j < fruit[i].length; j++){
if(fruit[0][j].equals(fruit[1][j])) {
row1[j] = fruit[0][j];
row2[j] = fruit[1][j];
System.out.println("Match found");
}else{
System.out.println("Not found");
}
}
}
System.out.println("row1");
System.out.println(Arrays.deepToString(row1));
System.out.println("row2");
System.out.println(Arrays.deepToString(row2));
我希望row1 []和row2 []存储相同的元素(在本例中为kiwi)。但问题是.equals函数只检测匹配模式。上面的示例仅打印出row1和row2中的空值。
它应该打印出来:
row1
[kiwi]
row2
[kiwi]
注意:我不想声明... String check = "kiwi";
,因为用户可以在2D数组中输入任何内容。
有什么建议吗?我觉得我越来越近了。我看到一个类似的例子,有人使用.equals并且有效,但它只适用于单个数组。
答案 0 :(得分:2)
for-loops的限制已经搞砸了,以及为了比较而访问的数组元素。我想你想要这样的东西......
import java.util.Arrays;
public class RowCompare
{
public static void main(String[] args)
{
String[] row1 = new String[10];
String[] row2 = new String[10];
String[][] fruit = {{"apple", "banana", "kiwi"},{"kiwi", "oranges", "grapes"}};
for (int i = 0; i < fruit[0].length; i++ ) {
for (int j = 0; j < fruit[1].length; j++){
if(fruit[0][i].equals(fruit[1][j])) {
row1[i] = fruit[0][i];
row2[j] = fruit[1][j];
System.out.println("Match found");
}else{
System.out.println("Not found");
}
}
}
System.out.println("row1");
System.out.println(Arrays.deepToString(row1));
System.out.println("row2");
System.out.println(Arrays.deepToString(row2));
}
}
但您应该描述您想要对结果做些什么。这些固定大小的结果数组(String [10])看起来很可疑,并且当前勾画的代码不能容易地推广到超过2行。使用Set
和List
s ....
答案 1 :(得分:2)
你的循环中的逻辑略有错误。
如果你看起来正在比较
fruit[0][0].equals(fruit[1][0])
然后
fruit[0][1].equals(fruit[1][1])
将if语句更改为
if(fruit[0][i].equals(fruit[1][j])) {
答案 2 :(得分:1)
您的解决方案非常无效,因为您没有利用哈希等优化算法:
如果包含元素,您应该使用HashSet或HashMap快速查找。
此外,集合还有像retainAll()这样的现成方法,只保留现有元素(HashSet实现集合,因此它可以满足您的需求)
retainAll:仅保留此集合中包含的元素 指定集合(可选操作)
HashSet row1 = new HashSet();
row1.add("Kiwi");
...
HashSet row2 = new HashSet();
row2.add...
System.out.println( row1.retainAll(row2) );
答案 3 :(得分:1)
将子数组放入临时(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]
我不知道你的要求,但将这些重复值放入两个不同的数组中有点奇怪,更不用说将这么多的值留空了。如何使用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