Ask Question

Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; String [] data2; In Java 6 and later, which statement copies the data array to the data2 array?

+2
Answers (1)
  1. 14 September, 06:23
    0
    String[] data2 = Arrays. copyOf (data, 4); is the statement which copies the data array to the data2 array in java 6.

    Explanation:

    The Arrays. copyOf () function copies the data from first array to another array in java. We pass the two argument in this function first argument is the name of first array and second the length of first array.

    Following are the program in java

    import java. util. Arrays; / / import package

    public class Main

    {

    public static void main (String[] args) / / main function

    {

    String[] data = { "abc", "def", "ghi", "jkl" }; / / string declaration

    / / printing the array data1

    System. out. println ("before copy new array:");

    for (int k = 0; k < data. length; k++)

    {

    System. out. println (data[k]);

    }

    String[] data2 = Arrays. copyOf (data, 4);

    / / printing the array data2

    System. out. println ("after copy new array:");

    for (int k = 0; k < data2. length; k++)

    {

    System. out. println (data2[k]);

    }}}

    Output:

    before copy new array:

    abc

    def

    ghi

    jkl

    after copy new array:

    abc

    def

    ghi

    jkl
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; String [] data2; In Java 6 and later, which statement ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers