• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I erase an object in a 2d array?  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone. This is my first time posting in here.

I have created a 2D array and created all objects inside the array. But now I need to erase an object in just one place. I set it to null to it does not work. Please help me~~~
So how can I erase an object I created?
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java only has 1-d arrays. What you have is an array that holds arrays which hold <whatever>.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sham Eron wrote:hello everyone. This is my first time posting in here.

Welcome to JavaRanch, Sham.

When you post code, please UseCodeTags (←click). I've done it for you this time; see how much better it looks?

Also, please read the page carefully. Very long lines tend to cause problems, so keep them short. I've also broken up the ones that were too long.

Thanks

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sham Eron wrote:I have created a 2D array and created all objects inside the array. But now I need to erase an object in just one place. I set it to null to it does not work.

I suggest you also read the ItDoesntWorkIsUseless page.

What doesn't work? What message did you get? Exactly.

Better still, back up and explain what you're trying to do, because that code is NOT "eras[ing] an object in just one place".

Winston
 
Sham Eron
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry for just saying 'it doesn't work' and thanks for helping me to show the code in the appropriate format.

Actually, Im writing a code to play a game like candy crush.
After swapping two gems, when there are three gems with same type, those three gems need to be disappear at the same time.
When I run the program, it can swap, but the three same kind of gems cannot be disappear. And the build result is success with no error message.
So Im wondering whether it is a suitable way to erase the object...
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, a couple of comments about these few lines of code:

Are you sure you mean to be calling scan() twice, and only reacting to the result of the second scan()? What happens if you call scan() twice, will the result of the second call be what you expect?

Second, using scan() == true is not the best way of doing things. Since scan() returns a boolean, you can use it directly i the conditional: if (scan()) {.

As to your question: Nulling a value in an array is the right way to remove an object from the array, but is not necessarily the right thing to do. For example, your for loops assume that there is a Gem in each test location, you get the Gem from the location and call the getPic() method on it. If you start to put nulls in the arrays then you will get NullPointerExceptions since you never check for a null value. You could add a bunch of null checks, or instead of using null for an empty spot, you could use a special Gem where the getPic() always returns something unique or has signal for 'Empty' in it. What exactly is appropriate - I don't know since I don't know what getPic() returns.

Also, how do you know the three gems do not disappear? Are you displaying the Gems somehow? After you make the changes do you make sure the Gems are re-displayed?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sham Eron wrote:Actually, Im writing a code to play a game like candy crush.
After swapping two gems, when there are three gems with same type, those three gems need to be disappear at the same time.

What 3? horizontally? vertically? diagonally? The only ones you seem to have allowed for are the first two, but I'm just guessing.

Also: if you swap a specific piece, surely you only need to check the section of the board that might be affected? I suppose there's nothing particularly wrong with checking the entire board, but most of it will be redundant.

When I run the program, it can swap, but the three same kind of gems cannot be disappear. And the build result is success with no error message.
So Im wondering whether it is a suitable way to erase the object...

Setting to null? Seems reasonable to me, providing the rest of your program understand that a null square is "empty".

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!