Page 1 of 2 12 LastLast
Results 1 to 20 of 26
Like Tree2Likes

Thread: Making deep copy of object arraylist

  1. #1
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Making deep copy of object arraylist

    Hello,
    Im currently working on trying to pass an arraylist of "film" objects from the main class into an "actor" object class and then making a deep copy of that arraylist of objects into an arraylist in the actor object so he owns them. I then have to be able to clear the arraylist in the main and reuse it for anymore actors I want to use.
    Hopefully ye understand my question, because I can't for the life of me figure it out and have been at it for the last 2weeks! so would really appreciate any help on this one.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default Re: Making deep copy of object arraylist

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Making deep copy of object arraylist

    Ive tried everything I could think of and its not for the lack of effort that I am asking here but thanks for your sarcastic reply anyway!
    my most recent effort was

    public void setFilm(ArrayList<film> theFilms)
    {
    ArrayList<film> films = new ArrayList<film>();
    for (int a = 0; a > theFilms.size(); a++)
    {
    String name = theFilms.get(a).getName();
    film Copy = new film(name);
    this.actorsFilms.add(Copy);
    }
    Here I was trying to get the name of the film out of the film object in the "theFilms" arraylist, use that name to make a new object and put the new object into the "actorsFilms" arraylist which is the arraylist in the actor class

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default Re: Making deep copy of object arraylist

    What was the problem with the code that you posted? What happened when it was executed? What was wrong with the results?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Making deep copy of object arraylist

    when I tried to get the print out from the film objects inside the actorsFilms arraylist I was getting blanks so I assume that there was nothing being put into the arraylist in the first place. Not 100% sure whats going wrong... does it look to you like it should be working? I cant understand whats going on with it and why it wont just work for me! (thanks for the reply again)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default Re: Making deep copy of object arraylist

    Can you make a small, complete program that compiles, executes and shows the problem.
    The posted code does not show what actorsFilms is.

    BTW The spelling of names is not java standard: classes begin with Uppercase and variables with lowercase letters.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Making deep copy of object arraylist

    I could pm u the 3 java files if that would be any good to u? Instead of trying to make it smaller and put it up here

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default Re: Making deep copy of object arraylist

    Smaller is better.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Making deep copy of object arraylist

    Java Code:
    public static void main(String[] args) 
    	{code}
    Last edited by GoodThing007; 11-22-2013 at 12:15 PM.

  10. #10
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Making deep copy of object arraylist

    Thats the main and then where the arraylist is passed into the setFilm method in the actor class... The film class is just u need to send in film(name) to create the film object so I havent posted that as that isnt where the problem is coming from

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Making deep copy of object arraylist

    Making a deep copy of something, is being able to copy the members of something; an ArrayList can make shallow copy of itself; if you create a copy constructor for your Film class (not 'film' class!), you're in business:

    Java Code:
    List<Film> copy= new ArrayList<Film>(originalFilmList); // here you have a shallow copy
    for (int i= 0; i < copy.size(); i++) // walk over the copy
       copy.set(i, new Film(copy.get(i))); // copy film object i
    Note that the deep copy issue is solved by the Film(Film anotherFilm) constructor that you have to write.

    kind regards,

    Jos
    GoodThing007 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Making deep copy of object arraylist

    Perfect I'll give it a go... Thanks jos

  13. #13
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Making deep copy of object arraylist

    Perfect JosAH... got it working exactly how I wanted. THANKS!

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Making deep copy of object arraylist

    Quote Originally Posted by GoodThing007 View Post
    Perfect JosAH... got it working exactly how I wanted. THANKS!
    Good; glad it works; there's an alternative way to do this, not particularly more efficient, but it looks fancier (imho):

    Java Code:
    List<Film> copy= new ArrayList<Film>(); // here you have an empty copy
    for (Film film : originalFilmList)
       copy.add(new Film(film)); // copy a film
    kind regards,

    Jos
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,048
    Rep Power
    6

    Default Re: Making deep copy of object arraylist

    He :) And now you can use variable naming to make it almost English:

    Java Code:
    List<Film> copy= new ArrayList<Film>(); // here you have an empty copy
    for (Film fromOriginalFilm : originalFilmList) {
       copy.add(new Film(fromOriginalFilm)); // copy a film
    }
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Making deep copy of object arraylist

    Quote Originally Posted by gimbal2 View Post
    He :) And now you can use variable naming to make it almost English:

    Java Code:
    List<Film> copy= new ArrayList<Film>(); // here you have an empty copy
    for (Film fromOriginalFilm : originalFilmList) {
       copy.add(new Film(fromOriginalFilm)); // copy a film
    }
    Nah, that's way too readable; if you want SmallTalk you know where to find it; better use some of the Microsoft idiocy:

    Java Code:
    List<hndWndP> lpsZ= new ArrayList<hndWndP>(); // here you have an empty hndWndP
    for (hndWndP fi : poriglpsZL)
       {
       lpsZ.add(new hndWndP(fi)); // copy a fi
       }
    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,048
    Rep Power
    6

    Default Re: Making deep copy of object arraylist

    MY EYES.

    Yeah I've had to wade through plenty of Microsoft formatted code in my time to hate it with a passion.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Making deep copy of object arraylist

    Quote Originally Posted by gimbal2 View Post
    MY EYES.

    Yeah I've had to wade through plenty of Microsoft formatted code in my time to hate it with a passion.
    I only use it to make something look foolish; I refuse to read it in any serious way; the inventor(s) of that crap should be defenestrated head first.

    kind regarrds,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,048
    Rep Power
    6

    Default Re: Making deep copy of object arraylist

    Quote Originally Posted by JosAH View Post
    I only use it to make something look foolish; I refuse to read it in any serious way; the inventor(s) of that crap should be defenestrated head first.

    kind regarrds,

    Jos
    Apparently, it was one Hungarian guy. But don't blame the people who make the rule, blame the people who follow it blindly without ever questioning its sanity!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  20. #20
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Making deep copy of object arraylist

    Quote Originally Posted by gimbal2 View Post
    Apparently, it was one Hungarian guy. But don't blame the people who make the rule, blame the people who follow it blindly without ever questioning its sanity!
    Considering it was developed for a language which had a single data type (were they words?) it made sense to come up with a naming system to indicate what type a particular variable was expected to contain. It makes no sense at all for languages with strong typing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 7
    Last Post: 10-09-2013, 08:48 PM
  2. Deep copy arraylist (NO CLONE)
    By NoobieCode in forum New To Java
    Replies: 5
    Last Post: 05-02-2013, 04:24 PM
  3. Deep Copy Constructor
    By tngo in forum New To Java
    Replies: 3
    Last Post: 01-29-2013, 02:58 PM
  4. deep copy in java
    By kmm1977 in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 05:59 PM
  5. Deep copy?!
    By deepthought015 in forum New To Java
    Replies: 3
    Last Post: 05-07-2009, 06:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •