我需要创建一个值同时出现在两个给定数组中的数组。
我正在考虑遍历每个数组并比较值,如果它们匹配,则增加一个“计数器”变量,该变量将是新数组的长度,遍历新数组并将值分配给数组元素。
我需要找出一个带有循环的解决方案,下面的代码是我到目前为止所掌握的
class New {
public static void main(String[] args) {
int arr1[] = {2, 4, 5, 7, 9, 10};
int arr2[] = {1, 2, 5, 6, 8};
int counter = 0;
int combined[] = new int[counter];
for (int s = 0; s < arr1.length; s++) {
for (int x = 0; x < arr2.length; x++) {
for (int i = 0; i < combined.length; i++) {
if (arr1[s] == arr2[x]) {
counter++;
combined[i] = arr1[s];
}
}
}
for (int i = 0; i < combined.length; i++) {
System.out.print(combined[i] + " ");
}
}
}
}
答案 0 :(得分:1)
public static int[] intersection(int[] arr1, int[] arr2) {
Set<Integer> elements = IntStream.of(arr1).boxed().collect(Collectors.toSet());
return IntStream.of(arr2).filter(elements::contains).toArray();
}
或仅使用int[]
:
// create temporary array to not modify input data
int[] tmp = Arrays.copyOf(arr1, arr1.length);
Arrays.sort(tmp);
for(int v : arr2)
if(Arrays.binarySearch(tmp, v) >= 0)
System.out.print(v + " ");
答案 1 :(得分:0)
您在这里:
int[] intersection = IntStream.of(arr1)
.filter(v1 -> IntStream.of(arr2).anyMatch(v2 -> v2 == v1))
.toArray();
答案 2 :(得分:0)
这可以解决问题:
private void getNetworkStatsClient() {
mStartTXClient = TrafficStats.getTotalTxBytes();
mStartRXClient = TrafficStats.getTotalRxBytes();
mHandler.postDelayed(mRunnableClient, 1000);
}
mRunnableClient = new Runnable() {
public void run() {
long[] res = new long[2];
res[0] = TrafficStats.getTotalTxBytes() - mStartTXClient;
res[1] = TrafficStats.getTotalRxBytes() - mStartRXClient;
System.out.println("Value of Rx: " + res[0]);
System.out.println("Value of Tx: " + res[1]);
((TextView) findViewById(R.id.data_buyer)).setText(String.valueOf(((double) (res[0] + res[1])) / 1048576) + "MB");
mHandler.postDelayed(mRunnableClient, 10000);
}
};
public static void main(String[] args) {
int arr1[] = { 2, 4, 5, 7, 9, 10 };
int arr2[] = { 1, 2, 5, 4, 8 };
int combined[] = new int[arr1.length];
for (int x = 0; x < arr1.length; x++) {
for (int i = 0; i < arr2.length; i++) {
if (arr1[x] == arr2[i]) {
combined[x] = arr1[x];
}
}
}
for (int i = 0; i < combined.length; i++) {
System.out.print(combined[i] + " ");
}
}
被定义为具有与较大数组相同的大小(以避免出现任何combined
)ArrayOutOfBoundsException
将仅保留
使用combined
的 size 初始化时,重复的条目以及一些零。 (作为练习,您可以更改循环以过滤出仅表示重复项的非零元素!)答案 3 :(得分:0)
如果您只想使用数组和迭代来解决此问题, 您可以:
代码:
public static void main(String[] args) {
...
// make sure input arrays are sorted
Arrays.sort(arr1);
Arrays.sort(arr2);
List<Integer> common = new ArrayList<Integer>();
int i = 0, j = 0;
while (i < arr1.length && j < arr2.length) {
int v1 = arr1[i];
int v2 = arr2[j];
if (v1 == v2) {
common.add(v1);
i++;
j++;
} else if (v1 < v2) {
i++;
} else {
j++;
}
}
System.out.println(common);
}
在每次迭代中前进:
无ArrayList版本:
public static void main(String[] args) {
...
Arrays.sort(arr1);
Arrays.sort(arr2);
int found = 0;
int[] common = new int[Math.min(arr1.length, arr2.length)];
int i = 0, j = 0;
while (i < arr1.length && j < arr2.length) {
int v1 = arr1[i];
int v2 = arr2[j];
if (v1 == v2) {
common[found] = v1;
found++;
i++;
j++;
} else if (v1 < v2) {
i++;
} else {
j++;
}
}
for (int k = 0; k < found; k++) {
System.out.println("common: " + common[k]);
}
}
答案 4 :(得分:0)
第一个问题是您将combined
数组初始化为0,因此它不包含任何内容,第二个问题是您有一个for
循环到许多循环,其中一个循环到{{1 }}数组。
相反,您需要创建一个临时数组,该数组的大小足以覆盖所有可能的重复项,然后将其从临时数组复制到正确大小的数组。
combined
如果您不想使用public static void main(String[] args) {
int arr1[] = { 2, 4, 5, 7, 9, 10 };
int arr2[] = { 1, 2, 5, 6, 8 };
int min = Math.min(arr1.length, arr2.length); //We can never have more duplicates than the number of elements in the smallest array
int counter = 0;
int combined[] = new int[min];
for (int s = 0; s < arr1.length; s++) {
for (int x = 0; x < arr2.length; x++) {
if (arr1[s] == arr2[x]) {
combined[counter] = arr1[s];
counter++;
break; //We have found a duplicate, exit inner for loop and check next digit in outer loop
}
}
}
int[] result = Arrays.copyOf(combined, counter); //This makes a copy of the array but only with the number of elements that are used
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + " ");
}
}
,则可以自己复制,只需将该行替换为
Arrays.copy
答案 5 :(得分:0)
在Java 8中,您可以尝试:
userid, fname,lastname,rollno,gender
1 , john, doe ,1001, M
2 , Rose, Mary ,1002, F
3 , Jack, Jill ,1003, M
输出将是:
t.references