shallowcopy.java

来自「《A first book of java》by Gary J.Bronson 」· Java 代码 · 共 23 行

JAVA
23
字号
public class ShallowCopy
{
  public static void main(String[] args)
  {
    
    int i;
    int nums[] = {2, 18, 1, 27, 16};
    int newnums[] = new int[nums.length];
    
    // this produces a shallow copy
    newnums = nums;  // this only copies the address
    // display newnums
    for (i = 0; i < newnums.length; i++)
      System.out.println("newnums[" + i + "] is " + newnums[i]);
    newnums[2] = 50;  // this affects both newnums and nums, because
                      // they are the same array
    System.out.println();
    // display nums and note the change
    for (i = 0; i < nums.length; i++)
      System.out.println("nums[" + i + "] is " + nums[i]);
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?