You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

数组复制并替换指定值代码出错,请求帮忙排查问题

Hey there! Let's work through your two array operation problems step by step.

问题1:复制数组并修改值时出现异常

Most likely, you fell into the trap of copying the array reference instead of creating a real independent copy. Let's take an example of the problematic code you might have written:

int[] originalArray = {10, 20, 30};
int[] copiedArray = originalArray; // This only copies the reference, not the array itself

copiedArray[0] = 100; // You think you're modifying the copy
System.out.println(originalArray[0]); // Outputs 100 instead of the original 10!

In this case, both variables point to the same array in memory. Modifying one will always affect the other, which is what causes that "abnormal" behavior you noticed.

To fix this, you need to create a brand new array and copy elements from the original one:

  • Option 1: Manual copy with a loop
int[] copiedArray = new int[originalArray.length];
for (int i = 0; i < originalArray.length; i++) {
    copiedArray[i] = originalArray[i];
}
  • Option 2: Use utility methods (like Arrays.copyOf in Java)
import java.util.Arrays;
int[] copiedArray = Arrays.copyOf(originalArray, originalArray.length);

This way, the copy is completely independent—modifying it won't touch the original array.

问题2:编写替换oldVal为newVal的数组副本方法

First, here's a correct implementation (using Java as an example), followed by the common mistakes you might have made:

Correct Implementation

import java.util.Arrays;

public class ArrayUtils {
    public static int[] replaceOldWithNew(int[] original, int oldVal, int newVal) {
        // Handle null input to avoid NullPointerException
        if (original == null) {
            return null;
        }
        
        // Create an independent copy of the original array
        int[] copy = Arrays.copyOf(original, original.length);
        
        // Traverse the copy and replace target values
        for (int i = 0; i < copy.length; i++) {
            if (copy[i] == oldVal) {
                copy[i] = newVal;
            }
        }
        
        return copy;
    }

    // Test case
    public static void main(String[] args) {
        int[] testArr = {5, 3, 5, 7, 3};
        int[] result = replaceOldWithNew(testArr, 3, 9);
        // Original array testArr remains [5,3,5,7,3], copy result becomes [5,9,5,7,9]
    }
}

Common Mistakes You Might Have Made

  • Mistake 1: Modifying the original array directly:If your method doesn't create a copy and instead modifies the original array directly, the original data will be permanently altered—this violates the requirement to return a copy.
  • Mistake 2: Wrong array copying method:Using int[] copy = original; (reference assignment) again, which means modifying the copy changes the original array too.
  • Mistake 3: Flawed replacement logic:For example, using = (assignment) instead of == (equality check) in the condition, or setting an incorrect loop boundary (like i <= copy.length which causes an array out-of-bounds error).
  • Mistake 4: Ignoring null input:If the input array is null, your code will throw a NullPointerException—adding a null check makes the method more robust.

内容的提问来源于stack exchange,提问作者ar khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:02:38