将一个元素赋值给空数组。

huangapple 未分类评论49阅读模式
英文:

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 &lt; array1.length; i++) {
                for (int j = 0; j &lt; array2.length; j++) {
                    if (array1[i] == array2[j]) {
                        count++;
                        for (int k = 0; k &lt; count; k++) {
                            sameElements[k] = array1[i];
                            System.out.println(&quot;sameElements[k] = &quot; + sameElements[k]);
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(&quot;Error: &quot; + 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 &lt; array2.length ? array1.length : array2.length];
    
    		int i, j, k, count = 0;
    		for (i = 0; i &lt; array1.length; i++) {
    			for (j = 0; j &lt; 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 &lt; 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(&quot;Duplicates: &quot;);
    		for (i = 0; i &lt; count; i++) {
    			System.out.print(sameElements[i] + &quot; &quot;);
    		}
    	}
    }

**Output:**

    Duplicates: 3 8 1 99 

</details>



huangapple
  • 本文由 发表于 2020年5月4日 05:34:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/61581919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定