user3317823
user3317823

Reputation: 57

contents of one array to another array

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

Answers (4)

sergej shafarenka
sergej shafarenka

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

Eric Jablow
Eric Jablow

Reputation: 7899

One of these should help:

  1. System.arraycopy()
  2. Arrays.copyOf()
  3. 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

Rafe Kettler
Rafe Kettler

Reputation: 76955

Arrays.copyOf is what you are looking for. http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

Upvotes: 2

Kakarot
Kakarot

Reputation: 4252

You can use Arrays.copyOf for copying contents of one Array into another. Refer the documentation in the following link

Upvotes: 0

Related Questions