Java: How to set an array with another array?

  • A+
Category:Languages

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

So, I have an array:

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]; } 

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?

 


First and preferred option is:

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

second one could be what you are doing just add:

name = newName; 

after for loops.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: