• Post Reply Bookmark Topic Watch Topic
  • New Topic

shallow copy  RSS feed

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me what making a shallow copy is, when dealing with arrays. I need to do a toString() method which returns the entire list of obejects in my array as one string. Someone told me I probably need to look at making a shallow copy. Is this true?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object array in java simply hold references to objects. To do a shallow copy of an object array, you create a new array of the same type, and populate them with the same references as in the orig array. The other option is a deep copy. With a deep copy, you actually make separate copies of the individual objects, and have the new array reference them.

BTW, this has nothing to do with the toString() method.

Henry
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right it has nothing to do with the toString() method sorry ... I got it confussed with the other method i'm trying to do i'm trying to do a toArray method. it suppose to return an array with all my things in it. can do i go about do that.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am assuming that you need to return an array, but don't have an array. There is nothing magical here. Create the array of the size of the number of items. Iterate through whatever you have, and set the array members to the references (assuming shallow copy )

Henry
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but there inlies the problem. there is not a set amount of elements. i am going to be adding things to this array so I cannot just make a aarray of a certain size.....or can I
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Victoria Preston:
but there inlies the problem. there is not a set amount of elements. i am going to be adding things to this array so I cannot just make a aarray of a certain size.....or can I


Then you have two options... You can do two passes. The first pass to find out the size of the array, and the second pass to populate it... or you can just add everything to a container, like an ArrayList. And then call the container's toArray() method to get an array.

Henry
[ February 03, 2006: Message edited by: Henry Wong ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!