Java 将元素分配给空数组
在Java中,如何将两个数组中的相同元素分配给另一个空数组? 我不知道如何从两个数组中选择相同的元素并将它们分配给另一个空数组。我不知道不使用ArrayList怎么办。Java 将元素分配给空数组,java,arrays,Java,Arrays,在Java中,如何将两个数组中的相同元素分配给另一个空数组? 我不知道如何从两个数组中选择相同的元素并将它们分配给另一个空数组。我不知道不使用ArrayList怎么办。 public class Main { public static void main(String[] args) { findDuplicate(); } public static void findDuplicate() { try { in
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);
}
}
}
公共类主{
公共静态void main(字符串[]args){
findDuplicate();
}
公共静态无效findDuplicate(){
试一试{
int[]数组1={12,15,6,3,9,8,1,3,99};
int[]array2=新的int[]{1,5,3,3,8,99};
int[]SameeElements={};
整数计数=0;
for(int i=0;i
按以下步骤操作:
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] + " ");
}
}
}
按如下方式操作:
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] + " ");
}
}
}
张贴代码的文本。永远不要发布代码的截图。阅读以了解为什么需要剪切和粘贴代码,而不是链接图片。我本来想帮你回答问题的,但我懒得把你所有的代码都重新键入。当你要求陌生人免费帮助你时,你需要让他们很容易做到。您还需要解释您的代码试图做什么,以及您认为什么不起作用。您当前的解释含糊不清,毫无意义。请使用数组列表代替ArrayList myNumbers=new ArrayList();MyNumber.add(数组1[j]);张贴代码的文本。永远不要发布代码的截图。阅读以了解为什么需要剪切和粘贴代码,而不是链接图片。我本来想帮你回答问题的,但我懒得把你所有的代码都重新键入。当你要求陌生人免费帮助你时,你需要让他们很容易做到。您还需要解释您的代码试图做什么,以及您认为什么不起作用。您当前的解释含糊不清,毫无意义。请使用数组列表代替ArrayList myNumbers=new ArrayList();MyNumber.add(数组1[j]);