如何设置一个阵列的价值观在Java另一个数组的值? [英] How do you set one array's values to another array's values in Java?

查看:144
本文介绍了如何设置一个阵列的价值观在Java另一个数组的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说你有两个数组:

    int[] a = {2, 3, 4};
    int[] b = {4, 5, 6};

你会如何设置阵列以数组b中,并保持他们不同的不同的对象?就像我想这样做的:

How would you set array a to array b and keep them different different objects? Like I thought of doing this:

    a = b; 

但是,这并不工作,因为它只是使一引用数组b。那么,是设置两个数组相等的唯一途径,同时保持他们单独的对象,遍历一个阵列的每个元素,并将其设置为其他?

But that doesn't work since it just makes "a" reference array b. So, is the only way to set two arrays equal, while keeping them separate objects, to loop through every element of one array and set it to the other?

和什么有关的ArrayList?你怎么总会有ArrayList中设置为其他相等的,当你在其中有对象吗?

And what about ArrayList? How would you set one ArrayList equal to another when you have objects in them?

推荐答案

您可能需要使用克隆

a = b.clone();

或使用 arraycopy(对象源,诠释sourcePosition,对象目的地,诠释destinationPosition,诠释numberOfElements)

System.arraycopy(b, 0, a, 0, b.length());

这篇关于如何设置一个阵列的价值观在Java另一个数组的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆