• Post Reply Bookmark Topic Watch Topic
  • New Topic

Clearing an array without changing the array given to a certain object  RSS feed

 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I would like to assign an arraylist of Strings to a certain object (called Player). During the program the arraylist will be changed but when assigned to a certain object (a player) I do not want to change it anymore. However, currently if I assign the arraylist to a player and afterwards clear the arraylist then the arraylist assigned to my player (in its constructor) is cleared as well. What it should do:
- Make arraylist of siblings (borther 1 and brother 2)
- assign this arraylist to a player (Bart)
- clear arraylist and give it new siblings (e.g. sister 1 and sister 2)
- assign this 'updated' arraylist to a new player (e.g. John)
- After clearing the arraylist again, Bart should still have an arraylist with brother 1 and brother 2, whereas John should have an arraylist containing sister 1 and sister 2)

Example Code:
import java.util.ArrayList;

 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is caused by using direct references to the List. Create what's called a defensive copy of the ArrayList in your setter.  The ArrayList class has a copy constructor, where you can pass a source List to the constructor and the new instance will basically be a copy of the elements of the List that was passed in.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are at least four possibilities for returning Lists (don't say array, because an array list is not an array), which I wrote about recently. Check that link.
 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed it by making a clone of the array! Can anyone tell me if this a 'proper' solution?

Thanks guys for your answers.
 
Bart Boersma
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the 'solved' version's code:
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave a link, Campbell gave a link, did you follow any of them to see what they led to? Because they have suggestions that are better than cloning, in my opinion.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Boersma wrote:Here is the 'solved' version's code:
With these changes it is not clear to me as to why cloning would still be necessary.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The OP's "solution" is a kludge. It does not really address the issue of changing a widely-shared variable like the names ArrayList. The code simple prints something other than the thing that is shared. It's like OP putting a nicely arranged plate of cookies on the table and then complaining that people are just coming and taking cookies and messing up the arrangement. So, to "fix" the problem, OP takes a picture of the plate of cookies and says, "Look, I solved it: now my plate of cookies never runs out and never gets messed up!" even though the real plate of cookies is still getting eaten from and messed up just as it was before "the fix."
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bart Boersma wrote:I fixed it by making a clone of the array! Can anyone tell me if this a 'proper' solution?
To repeat what I said yesterday: what array? You are using a List not an array.

The clone() method wouldn't be my first choice; I would prefer a copy constructor, but it shou‍ld work.
Thanks guys for your answers.
That's a pleasure

Don't give variables name starting with CapitalLetters. Don't declare variables as ArrayList: declare them as List. Always give your fields private access, so you can't access the List directly outside the Player class. That of course vitiates all your hard work copying the List; other code can access it and change its state without your knowing about it. You would want a method to get names directly from the Player object, e.g. getSiblingName(int). There is a risk of such a method throwing an out of bounds Exception if you pass the wrong number. I think the cast in line 49 shows that clone() isn't such a good solution.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main issues in the code that contribute directly or indirectly to the stated problem include:

1. The program is not really object-oriented. A lot of code that should be in an object are actually outside of it, making it more procedural code.

2. The widely-shared variable, names, is an ArrayList that originally contained the names of a Person object's siblings but as of the newest version, doesn't even seem to have a clear purpose.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!