Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Clone an array?  RSS feed

 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I go about doing this? Create a new ArrayList<People> PeopleListClone object, use its AddAll method to Add “peopleList” to itself twice. You will have 6 elements for PeopleListClone. Display all six elements.
After this, use remove method in “PeopleListClone” to remove the student objects in this ArrayList, two student elements will be removed. Display all four elements left.


As of right now, this is what I have.


Thank you.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go to ArrayList Javadoc, search allAll, add and remove method.. You can get idea there..

In your code you are no where creating clone list.. You are only sorting the list and displaying it..
 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Go to ArrayList Javadoc, search allAll, add and remove method.. You can get idea there..

In your code you are no where creating clone list.. You are only sorting the list and displaying it..


I added it (I think).



I just don't know how to get rid of the redundancy. Also, I can't get them completely sorted. It's treating the two lists separately. Here's my output:
////////////////// People list clone initialized.
Bruce 161257235
Clark 199242003
Diana 111222333
Bruce 161257235
Clark 199242003
Diana 111222333
////////////////// People list after student elements removed.
Bruce 161257235
Clark 199242003
Bruce 161257235
Clark 199242003
////////////////// People list clone sorted in reverse.
Clark 199242003
Bruce 161257235
Clark 199242003
Bruce 161257235
////////////////// People list clone is cleared.



 
Paul Clapham
Sheriff
Posts: 22471
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chase Harris wrote:I just don't know how to get rid of the redundancy. Also, I can't get them completely sorted. It's treating the two lists separately.


I'm a bit confused about that statement. I read the instructions in your original post and it looks like you did that correctly (except you have some extra code not mentioned by the requirements). But they say you should have four elements left at the end, and you do indeed have four elements left. I don't really see any problems (except for the extra code I mentioned).

And the requirements don't say anything about getting rid of redundancy, and they don't say anything about sorting the "cloned" list. So I'm not sure why you're concerned about those things, unless there are other requirements which we didn't see.
 
Chase Harris
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Chase Harris wrote:I just don't know how to get rid of the redundancy. Also, I can't get them completely sorted. It's treating the two lists separately.


I'm a bit confused about that statement. I read the instructions in your original post and it looks like you did that correctly (except you have some extra code not mentioned by the requirements). But they say you should have four elements left at the end, and you do indeed have four elements left. I don't really see any problems (except for the extra code I mentioned).

And the requirements don't say anything about getting rid of redundancy, and they don't say anything about sorting the "cloned" list. So I'm not sure why you're concerned about those things, unless there are other requirements which we didn't see.
The extra code was for another part of the problem. By redundancy, I wasn't sure if it was a good idea to use something like
or if there was a more punctuated way of coding that.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the removeIf method (Java8 only).
 
Paul Clapham
Sheriff
Posts: 22471
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chase Harris wrote:By redundancy, I wasn't sure if it was a good idea to use something like
or if there was a more punctuated way of coding that.


It's a highly artificial piece of code with not much relationship to real-life work, so don't worry about that particular thing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!