這是寫得不是很好,但它是一個快速解決你所要求的。基本上,我將所有數組添加到列表中,然後遍歷該列表,檢查所選元素是否匹配任何給定顏色。
public static void main(String[] args)
{
String[] s1 = {"red", "white", "black", "blue"};
String[] s2 = {"red", "black", "green"};
String[] s3 = {"red", "green"};
int red = 0;
int black =0;
int green = 0;
int white= 0;
int blue= 0;
List <String> list = new ArrayList <String>();
list.addAll(Arrays.asList(s1));
list.addAll(Arrays.asList(s2));
list.addAll(Arrays.asList(s3));
for(String s: list)
{
if(s.equals("red"))
{
red++;
}
else if(s.equals("white"))
{
white++;
}
else if(s.equals("black"))
{
black++;
}
else if(s.equals("green"))
{
green++;
}
else if(s.equals("blue"))
{
blue++;
}
}
System.out.println(red + " Red found\n" + green + " Green Found\n" + white + " White Found\n" + black + " Black Found\n" + blue + " Blue Found");
}
}
輸出:
3 Red found
2 Green Found
1 White Found
2 Black Found
1 Blue Found
你在使用 – 2014-10-17 14:21:05
而正是你想要做的語言嗎?你想顯示任何發生在兩個以上數組中的元素嗎? – Mureinik 2014-10-17 14:22:58
http://java67.blogspot.com/2014/05/how-to-compare-two-arrays-in-java-string-int-example.html 這是你想要的嗎? – 2014-10-17 14:27:21