0

I've been working with arrays and I'm trying to find a way to copy one array to another solely with a for loop (I've seen things like arrayCopy and clone in other threads, but for this exercise I need to just copy from one array to another using a for loop).

Right now my code executes, but just spits this out: "[D@133c5982". The code I'm using is:

public class sorted 
{
    public static void main(String[] args)
    {
        int [] unSortedArray = {8,12,6,5,19};
        System.out.println(copyArray(unSortedArray));
        //System.out.println(sortedArray(unSortedArray));
    }

    public static int[] copyArray(int[] array)
    {
        int [] copyArray;
        copyArray = new int[array.length];
        for (int i = 1; i < array.length; i++)
    {
        copyArray[i] = array[i];
    }
    return copyArray;
}
2
  • 1
    You can't print an array like this. Loop through it printing element by element or convert to string. Commented May 24, 2013 at 6:16
  • 3
    That's the default toString() implementation for arrays. You should use Arrays.toString(array). Commented May 24, 2013 at 6:16

6 Answers 6

4

First of all, you are not copying whole array, as you starting your index with 1

as in your code

for (int i = 1; i < array.length; i++)
{
        copyArray[i] = array[i];
}

start the index with 0

and second you can make use of Arrays.deepToString(array) or Arrays.toString(array) to print array in readable format

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for showing me the method deepToString, I didnt know it existed!
no problem! if this answer solved your question then please accept it by clicking tick make to left answer
1

Your code is OK, it does copy the array. When you do System.out.println it prints out the default implementation of the array's toString() method). To see that it is indeed copied, you can do

for (int i = 0; i<unsortedArray.length; i++) {
    System.out.println(unsortedArray[i]);
}

for (int i = 0; i<copiedArray.length; i++) {
    System.out.println(copiedArray[i]);
}

EDIT: see the comments for what you code actually prints out

1 Comment

Actually, it prints out the array object's toString which is the type signature plus hashcode for the object, which may or may not be an address.
0

copyArray returns an array and that's what you are printing. What you see is the Java Virtual Machine way of representing objects as strings. @12345 is the object's ID.

There is nothing wrong with the code provided, except a } at the end (possibly omitted while copying).

Comments

0

When printing you are using the arrays default toString which doesn't print the content of the Strings.

Since Java 1.5 you can use Arrays.toString(array) to get a nice String representation of the array.

See the API here.

Use this with System.out.println to get a nice printout.

Comments

0

What you have done is correct. But if you print an array using System.out.println(copyArray(unSortedArray)); it only prints out the location of the array. Instead, try printing each element of the array in a for loop.

for (int i = 1; i < array.length; i++)
{
   system.out.println(array[i]);
}
//Arrays.toString(<arrayname>) can also be used in place of the for loop. 

Also, array index starts at 0. So, your method should be as follows.

public static int[] copyArray(int[] array)
{
    int [] copyArray;
    copyArray = new int[array.length];
    for (int i = 0; i < array.length; i++)
{
    copyArray[i] = array[i];
}
return copyArray;

3 Comments

@Makoto Oh. I always thought it was the hash value.Thanks for the info.
Nah, I was off. I went back, printed out the hex value of the hash code, and they match.
@Makoto Aah. So it is both hash code hex value and the memory location.Or just the hash value?
0

try this

 package naveed.workingfiles;

 import java.util.Arrays;

 public class Array 
{
public static void main(String[] args)
{
    int [] unSortedArray = {8,12,6,5,19};
    int [] unSortedCopyArray =new  int [unSortedArray.length];

    //System.out.println(sortedArray(unSortedArray));
    for(int i=0;i<unSortedArray.length;i++)
    {
        unSortedCopyArray[i]=unSortedArray[i];
    }           
    System.out.println(Arrays.toString(unSortedArray));//exist array
    System.out.println(Arrays.toString(unSortedCopyArray));//copied array


}

}

1 Comment

Nope - you can print the array in one go without having to use a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.