码迷,mamicode.com
首页 > 编程语言 > 详细

数组引用赋值

时间:2014-12-16 00:54:44      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   color   sp   for   java   on   div   

 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 nums1
27         
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 nums2
34         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

数组引用赋值

标签:style   blog   ar   color   sp   for   java   on   div   

原文地址:http://www.cnblogs.com/fatoland/p/4166125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!