我需要两个输入数组A和数组B。 答案将是这样-如果输入A为
{5,2,6,8}
输入B是
{6,5,5,8,5,6}
然后输出将是:
2 (0 times), 5 (3 times), 6 (2 times), 8 ( 1 times)
答案 0 :(得分:0)
这是一个如何实现它的示例。
public static void main(String[] args) {
int[] A={5,2,6,8},
B={6,5,5,8,5,6};
int[] count = new int[A.length]; // An array counting the occurences
StringBuilder result = new StringBuilder(); // The String to be displayed. StringBuilder is better than String, when multiple concatenations occur.
Arrays.sort(A); // Sorts array A in ascending order (by default).
// This loop counts the occurence of each value of A inside B
for (int i = 0; i <A.length; i++){
for (int j = 0; j < B.length; j++){
if (A[i] == B[j])
count[i]++;
}
// After each value of A, add the number of occurence in the String
result.append(A[i]).append(" (").append(count[i]).append(" times), ");
}
// Displays the String in the console, removing the last ", "
System.out.println(result.substring(0,result.length()-2));
}