• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

shallow copy

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic