首页 > 代码库 > 数组引用赋值
数组引用赋值
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
数组引用赋值
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。