• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

How can I remove objects from an array?

 
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are those? Built-in classes or do I have to make my own map?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If setting to null works for you, it is a clean enough solution.
 
Alix Ollivier
Ranch Hand
Posts: 71
Mac Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic