0

I want to write a method that copies my PaperPublication array:

public static PaperPublication[] copyBooks(PaperPublication[] array)
{

    PaperPublication[] copy = new PaperPublication[array.length];
    for (int i = 0; i < copy.length; i++) {
        PaperPublication temp = array[i];
        if (temp != null) {
            copy[i] = new PaperPublication(temp);
        }
    }
    return copy;
}

when I try and print out the copied array like this,

PaperPublication[] copy = driverClass.copyBooks(original);
for (int i = 0; i < 12; i++)
    {
        System.out.println(copy.toString());
    }

it yields the location, and not the values:

[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742

How do I write the code so it returns the values instead? (I already have an overwritten toString() method in my classes)

Mike J
  • 93
  • 1
  • 8

2 Answers2

0

You are trying to print PaperPublication objects which are of type Object. There should be a method in the PaperPublication class which returns its value. A specific answer cannot be given without knowing the PaperPublication class.

Imal
  • 481
  • 1
  • 6
  • 14
0

How do I write the code so it returns the values instead? (I already have an overwritten toString() method in my classes)

You are printing your entire array and not individual elements.

Change:

System.out.println(copy.toString());

To:

System.out.println(copy[i]);

It is redundant to write copy[i].toString() since Java on its own will access the toString() method to get a String representation for your PaperPublication object.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • When I do this, it now gives me a NullPointerException.. any ideas why? In my PaperPublication class I have a copy constructor and a overwritten toString method – Mike J Oct 10 '17 at 19:26
  • @MikeJ That is because not all your array elements have an instantiated object. You probably encounter the Exception while looping through an array of PaperPublication objects. You should have a variable to keep track number of actual elements you have in your array while looping the array. Alternatively, you can use arraylist, then you don't have to keep track number of instantiated element by yourself. – user3437460 Oct 10 '17 at 19:42
  • @MikeJ If my solution helps, remember to accept my solution by clicking the hollow looking tick beside my answer. – user3437460 Oct 10 '17 at 19:43
  • I'm sorry but could you please write out the copyBooks() method for me so that it wont give me the error? I am very confused. I have accepted your solution but it would be very helpful if you could further assist me. Thanks – Mike J Oct 10 '17 at 22:26