• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I remove objects from an array?  RSS feed

 
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am building a game. I have an array of "crystals", and I want to make it so that whenever my main character hits the crystals, it removes the crystals from the array. I already have all the code for rendering and updating, but I need to get the crystals out of the array. If I simply set it to null, by using crystal = null; it still works. How can I remove it?

If you need more information, please tell me which parts.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alix Ollivier wrote:I am building a game. I have an array of "crystals", and I want to make it so that whenever my main character hits the crystals, it removes the crystals from the array. I already have all the code for rendering and updating, but I need to get the crystals out of the array. If I simply set it to null, by using crystal = null; it still works. How can I remove it?

My suggestion would be to use a List (or possibly a Map) rather than an array.

Winston
 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are those? Built-in classes or do I have to make my own map?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alix Ollivier wrote:What are those? Built-in classes or do I have to make my own map?

Built in classes. Have a look at the java.util.List (←click) and java.util.Map docs; which are both interfaces - they will guide to the implementation classes (eg, ArrayList).

Winston
 
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If setting to null works for you, it is a clean enough solution.
 
Alix Ollivier
Ranch Hand
Posts: 71
Java Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it doesn't work. Here is my code with all the unessential parts taken out. And yet somehow the crystal is still kept in the array.

 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are creating a new variable in the for loop, then assigning that variable to null. You are not changing the value in the array. You will need to use the array[index] syntax to set the values in the array to null.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So depending on what you need, nulling out the element may or may not work.

If you have an egg carton that holds 12 eggs, you can take an egg or two out, but the carton is still big enough to hold 12 eggs. If you look at each slot, you will occasionally come across an empty spot. That may be OK, depending on your needs.

An array, once created, never changes size. If you need to shrink your array from 12 to 10, your only option is to create a new array that is the 'correct' size, and then copy the elements into it.

Or, as others have suggested, us a different collection.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!