Q: copy the contents of an array to another and deleting the contents of the old array I am trying to copy the contents of an array to another array after a button is clicked. I want the oldArray to be cleared of the previous data after it is copied to newArray. I have this code and I'm lost. can anyone please guide me on how to do this? here's what I have so far.
    final String [] oldArray;

    oldArray = new String[5];
    oldArray[0] = "c";
    oldArray[1] = "h";
    oldArray[2] = "a";
    oldArray[3] = "r";
    oldArray[4] = "m";

    for (i = 0; i < 5; i++);

        {
            oldarrayOut.setText(oldArray[i]);
        }

    bOk.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub



            String[] newArray = new String[5];

            for (i = 0; i < 5; i++);
                {
                    newArray[i] = oldArray[i];
                    newarrayOut.setText(newArray[i]);
                }



        }
    });

please feel free to notify me also if there's something wrong in my code. Thank you very much!

A: Just use:
oldArray = new String[5];

or
oldArray = null;

So the old array wont point to the really old array, but to a new one.
The first one is better if you know you're gonna use it again, and you know the length of the array. The other need to be treated safe as it may cause NPE if you access it.

A: I'm not sure what the oldarrayOut and newarrayOut fields are doing, but it seems like putting
oldArray = null;

outside the for loop in your onClick method would do it.
You seem to be copying the array successfully, just not initializing oldArray once you've finished copying.

A: You have a semicolon after the for loop:
for (i = 0; i < 5; i++);
                       ^-- here

Which transforms the for loop into a loop doing nothing at each iteration. Remove it.
To clear the old array, simply use
for (i = 0; i < 5; i++) {
    newArray[i] = oldArray[i];
    oldArray[i] = null;
    newarrayOut.setText(newArray[i]);
}


A: String[] newArray = (String[]) oldArray.clone();

copy the contents of an array to another.
Try this to copy oldArray to newArray.
Arrays.fill( oldArray, null );

deleting the contents of the old array
