Reputation: 57
How can I take the contents of one array and put it in another array. I'm trying to call a function that takes a array and places it's contents into another array.
public String[] new_list;
public void setList(String list[]){
for (int i =0; i<list.length; i++)
list_command[i]= list[i];
}
Upvotes: 0
Views: 133
Reputation: 20406
Given array
String[] list = {"1", "2", "3"};
Option 1:
String[] newList = Arrays.copyOf(list, list.length); // create new and copy
Option 2:
String[] newList = new String[list.length]; // create new array
System.arraycopy(list, 0, newList, 0, list.length); // copy array content
Upvotes: 1
Reputation: 7899
One of these should help:
System.arraycopy()
Arrays.copyOf()
Arrays.copyOfRange()
The choice depends on whether you are copying an array into a new array, part of an array into part of another, or part of an array into a new array.
Upvotes: 1
Reputation: 76955
Arrays.copyOf
is what you are looking for. http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
Upvotes: 2