我可以在Java中从另一个数组引用一个数组的元素吗?
是的,您可以-
int [] myArray1 = {23, 45, 78, 90, 10};
int [] myArray2 = {23, 45, myArray1[2], 90, 10};但是,一旦这样做,第二个数组将存储值的引用,而不是整个数组的引用。出于这个原因,数组中的任何更新都不会影响参考值-
示例
import java.util.Arrays;
public class RefferencingAnotherArray {
public static void main(String args[]) {
int [] myArray1 = {23, 45, 78, 90, 10};
int [] myArray2 = {23, 45, myArray1[2], 90, 10};
System.out.println("Contents of the 2nd array");
System.out.println(Arrays.toString(myArray2));
myArray1[2] = 2000;
System.out.println("Contents of the 2nd array after updating ::");
System.out.println(Arrays.toString(myArray2));
System.out.println("Contents of the 1stnd array after updating ::");
System.out.println(Arrays.toString(myArray1));
}
}输出结果
Contents of the 2nd array [23, 45, 78, 90, 10] Contents of the 2nd array after updating :: [23, 45, 78, 90, 10] Contents of the 1stnd array after updating :: [23, 45, 2000, 90, 10]
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短