英文:
assigning an element to an empty array
问题
如何在Java中将两个数组中相同的元素赋值给另一个空数组?
我无法弄清楚如何从两个数组中选择相同的元素并将它们赋值给另一个空数组。我不知道如何在不使用**ArrayList**的情况下进行操作。
public class Main {
public static void main(String[] args) {
findDuplicate();
}
public static void findDuplicate() {
try {
int[] array1 = {12, 15, 6, 3, 9, 8, 1, 3, 99};
int[] array2 = new int[]{1, 5, 3, 3, 8, 99};
int[] sameElements = {};
int count = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array2.length; j++) {
if (array1[i] == array2[j]) {
count++;
for (int k = 0; k < count; k++) {
sameElements[k] = array1[i];
System.out.println("sameElements[k] = " + sameElements[k]);
}
}
}
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
英文:
How do I assign the same elements from two arrays to another empty array in Java?
I can't figure out how to select the same elements from two arrays and assign them to another empty array. I don't know how to do without using ArrayList.
public class Main {
public static void main(String[] args) {
findDuplicate();
}
public static void findDuplicate() {
try {
int[] array1 = {12, 15, 6, 3, 9, 8, 1, 3, 99};
int[] array2 = new int[]{1, 5, 3, 3, 8, 99};
int[] sameElements = {};
int count = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array2.length; j++) {
if (array1[i] == array2[j]) {
count++;
for (int k = 0; k < count; k++) {
sameElements[k] = array1[i];
System.out.println("sameElements[k] = " + sameElements[k]);
}
}
}
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
答案1
得分: 0
public class Main {
public static void main(String[] args) {
findDuplicate();
}
public static void findDuplicate() {
int[] array1 = { 12, 15, 6, 3, 9, 8, 1, 3, 99 };
int[] array2 = { 1, 5, 3, 3, 8, 99 };
// Array of the smaller length (out of the lengths of array1 and array2)
int[] sameElements = new int[array1.length < array2.length ? array1.length : array2.length];
int i, j, k, count = 0;
for (i = 0; i < array1.length; i++) {
for (j = 0; j < array2.length; j++) {
if (array1[i] == array2[j]) {
// If 0 is present in both, array1[] and array2[]
if (array1[i] == 0) {
count++;
continue;
}
// Search if array1[i] exists in sameElements[]
for (k = 0; k < count; k++) {
if (sameElements[k] == array1[i]) {
break;
}
}
// If array1[i] is not already in sameElements[], put it into sameElements[]
if (k == count) {
sameElements[count++] = array1[i];
}
}
}
}
System.out.print("Duplicates: ");
for (i = 0; i < count; i++) {
System.out.print(sameElements[i] + " ");
}
}
}
Output:
Duplicates: 3 8 1 99
<details>
<summary>英文:</summary>
Do it as follows:
public class Main {
public static void main(String[] args) {
findDuplicate();
}
public static void findDuplicate() {
int[] array1 = { 12, 15, 6, 3, 9, 8, 1, 3, 99 };
int[] array2 = { 1, 5, 3, 3, 8, 99 };
// Array of the smaller length (out of the lengths of array1 and array2)
int[] sameElements = new int[array1.length < array2.length ? array1.length : array2.length];
int i, j, k, count = 0;
for (i = 0; i < array1.length; i++) {
for (j = 0; j < array2.length; j++) {
if (array1[i] == array2[j]) {
// If 0 is present in both, array1[] and array2[]
if (array1[i] == 0) {
count++;
continue;
}
// Search if array1[i] exists in sameElements[]
for (k = 0; k < count; k++) {
if (sameElements[k] == array1[i]) {
break;
}
}
// If array1[i] is not already in sameElements[], put it into sameElements[]
if (k == count) {
sameElements[count++] = array1[i];
}
}
}
}
System.out.print("Duplicates: ");
for (i = 0; i < count; i++) {
System.out.print(sameElements[i] + " ");
}
}
}
**Output:**
Duplicates: 3 8 1 99
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论