• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array list copying  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. First time here. Nice to meet you all and I really appreciate help and tips for a beginner

I have to create this copy constructor

landscape is a 2D array of type int
Fish and Plants are arraylists

Is my copying right?


Thanks again.

 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeremy, welcome to the Ranch!

Well, first of all you create an array of arrays named "copy" but then you don't use it at all. I don't know why you would create an array of arrays at all, since you're supposed to be updating the state of the Model object. So I think there's something wrong with that. Not quite sure what, though.

And then you've written code which updates the "other" object. In a copy constructor you're supposed to be taking data from the "other" object and updating the state of "this" object. So that's backwards.

And the comment says "each fish and each plant must be copied". You aren't doing that, even though you might think you are. You're just copying references to fish and references to plants, which is called a SHALLOW copy. Somewhere in the Fish class there must be a way to copy a Fish object -- maybe it has a copy constructor?
 
Jeremy Ng
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok here is the copy constructor for fish:


How would I use the for each loop and copy that. im so lost and new ahah.

How do I reference copy the 2D array. I created a 2D array called copy and i thought I was making copy = the original.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Ng wrote:ok here is the copy constructor for fish:


How would I use the for each loop and copy that. im so lost and new ahah.

How do I reference copy the 2D array. I created a 2D array called copy and i thought I was making copy = the original.


I don't exactly understand your requirements, but for copying a Fish, it doesn't look like you need to do anything with any arrays.

Does a Fish have array member variables? It looks like not. It looks like just row, col, size, direction.

Assuming that's the case, then, as already pointed out, the above is backward.



Inside our constructor, we are working on a newly created Fish object. Its fields all have their default values--0/false/null. We were passed a reference to an existing Fish object, in the that parameter.

Whatever fields values that has, we want to copy them into our "current" Fish.

To that end:
1. If I say "put the value from Y into X", do you know how to do that?

2. Do you know how to get the values out of another object?

3. Do you know how to set the values in the "current" object--the one referred to by "this"?

Get the copy constructor for a single Fish working first, then worry about the arrays and the looping.
 
Jeremy Ng
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im going to try to be thorough. Sorry about not making it clear.

I have these


Has these inside
Model(int numRows, int numCols, int numRocks, int numFish, int numPlants)


So Right now I and creating the copy constructor of Model:
_______________________________________________________________
public Model(Model other)
Copy Constructor.
Since landscape is immutable, it is be copied with just a reference copy.
Fish and Plants are mutable, so they must be copied with a DEEP copy!
(WARNING: Each fish and each plant must be copied.)
_______________________________________________-

I am so lost on how to create a reference copy for the 2D array

I am so lost on how to create DEEP copy for the 2 arraylists. I thought I was doing it right with the for each statement.

 
Jeremy Ng
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Copy constructr for Fish

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You kind of ignored my advice to focus on the Fish copy constructor for now. We'll come back to that.

Jeremy Ng wrote:here's my attempt


Okay, landscape is fine. That was the easy one.

Now, for fish and plants, you've got two problems.

1. You're really just doing the same thing right now. Each call to other.addFish() just causes a reference copy in the ArrayList, so that now there are two ArrayList entries pointing to the same fish, just like the landscape line.

2. You're adding it to the wrong list.

You're saying, "For every fish in the other's fish list, add another reference to that fish into the other's fish list." So your new fish list will be empty, and the original fish list will be a double of its original self.

Oops, what's reall goingon: You're saying, "For every fish in my new fish list (which is empty), add a reference to that fish into the other's fish list." So you're saying "add no fish to the original fish list." Which, technically, yeah, you don't want to add any fish to the original model's fish list, but you dont need to write a loop to do that.


What you need to do is: 1) Create a new list. 2) For every Fish in the original list, make a copy of that Fish, and 3) Stick a reference to that newly created Fish copy into your new list.

So, going back to what I said previously: Read my previous post, and focus on the Fish copy for now. It should be trivial, since you've already done the same thing for the landscape part of copying a Model.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Ng wrote:Copy constructr for Fish



Looks about right. Why not use "other" like you did for Model? It's more explicit, and it's consistent with what you're doing elsewhere. Both good habits to get into.
 
Jeremy Ng
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need to do is: 1) Create a new list. 2) For every Fish in the original list, make a copy of that Fish, and 3) Stick a reference to that newly created Fish copy into your new list.
Alright Ill try this:


Is this somewhat close?
 
Jeremy Ng
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's my recent revision

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Ng wrote:here's my recent revision


That looks right to me. But don't trust me. Test it.

  • Make sure the fish and plants in the new Model match those of the original.
  • Change the contents of the fish and plants in one of the Models, and make sure that it doesn't affect the other.
  • Add or remove fish and plants to one of the models, and make sure that doesn't affect the other one either.
  •  
    Jeremy Ng
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It worked. Thanks for sticking with me. I hope I can learn more from you later on. hhaah THANKS.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome. Good job working it out.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!