Java:如何用另一个数组设置一个数组? [英] Java: How to set an array with another array?

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

问题描述

我对Java还是很陌生,希望您能提出意见.

I'm very new to Java, and I would like your inputs.

所以,我有一个数组:

String[] names = {"Anna", "Jo"};
String[] newNames = {"Bob", "Sue", "Jane"};
int totalLength = names.length + newNames.length;
String[] allNames = new String[totalLength];

我通过以下方式将它们组合在一起:

And I am combining them through:

for (int i = 0; i < names.length; i++) {
    allNames[i] = names[i];
}
for (int i = 0; i < newNames.length; i++}
    allNames[i + names.length] = newNames[i];
}

我的问题是如何将allNames数组设置为原始名称数组?像是"Anna","Jo","Bob","Sue","Jane".我知道有些方法可以做到这一点,但是您将如何手动进行呢?

My question is how do I set the allNames array into the original names array? Like, names would be "Anna", "Jo", "Bob", "Sue", "Jane". I know that there are methods that can do this, but how would you do it manually?

推荐答案

第一个首选方法是:

name = (String[])ArrayUtils.addAll(names, newNames);

第二个可能就是您正在做的事情,只需添加:

second one could be what you are doing just add:

name = newName;

for 循环之后

.

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

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