首页 > 代码库 > 数组引用赋值

数组引用赋值

 1 package com.java7; 2  3 class AssignARef { 4     public static void main(String[] args) { 5         int i; 6          7         int nums1[] = new int[10]; 8         int nums2[] = new int[10]; 9         10         for(i = 0; i < 10; i++)11             nums1[i] = i;12         13         for(i = 0; i < 10; i++)14             nums2[i] = i;15         16         System.out.print("Here is nums1: ");17         for(i = 0; i < 10; i++)18             System.out.print(nums1[i] + " ");19         System.out.println();20         21         System.out.print("Here is nums2: ");22         for(i = 0; i < 10; i++)23             System.out.print(nums2[i] + " ");24         System.out.println();25         26         nums2 = nums1; // now nums2 refers to nums127         28         System.out.println("Here is nums2 after assignment: ");29         for(i = 0; i < 10; i++)30             System.out.print(nums2[i] + " ");31         System.out.println();32         33         // now operate on sums1 array through nums234         nums2[3] = 99;35         36         System.out.println("Here is nums1 after change through nums2: ");37         for(i = 0; i < 10; i++)38             System.out.print(nums1[i] + " ");39         System.out.println();40     }41 }

 

执行结果:

Here is nums1: 0 1 2 3 4 5 6 7 8 9
Here is nums2: 0 1 2 3 4 5 6 7 8 9
Here is nums2 after assignment:
0 1 2 3 4 5 6 7 8 9
Here is nums1 after change through nums2:
0 1 2 99 4 5 6 7 8 9

数组引用赋值