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

Remove & replace array element  RSS feed

 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have what is essentially a deck of cards. I want to pick a card at random, then remove it from the deck. But i don't want to end up with a bunch of null indexes. I want to replace the removed element with the last value in the array. I have yet to find a proper explanation of how to do that. Can i get some help with this?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like what you really need is to not use an array. Instead, use a List (such as ArrayList or LinkedList). That's got a remove(int) method that will do exactly what I think you want - remove the object at that position from the list (moving all the following elements up one), and returning that object.

Basically, arrays aren't intended to change size. A List is a better option whenever you want the size to be able to change.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can shuffle your array and go through it in order.
 
Deep Purohit
Greenhorn
Posts: 9
Eclipse IDE Flex Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time you pick up a card from the deck, you will have to remove it from deck.
Lets say the card number n is picked up.. in a loop copy all n+1 value to n. This will leave the last index as empty.

Now you have 2 options:
1. Create a new array with length of existing array length -1. (Hint: System.arrayCopy) and use it.
2. Have a length variable with original length as 52 and keep on reducing whenever you select a card. And use this variable to do all iterative operation instead of using array.length
 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deep Purohit wrote:Every time you pick up a card from the deck, you will have to remove it from deck.
Lets say the card number n is picked up.. in a loop copy all n+1 value to n. This will leave the last index as empty.

Now you have 2 options:
1. Create a new array with length of existing array length -1. (Hint: System.arrayCopy) and use it.
2. Have a length variable with original length as 52 and keep on reducing whenever you select a card. And use this variable to do all iterative operation instead of using array.length


Well i plan on using option 2. my problem is the actual removal and replacement of the card. I've seen some explanaitions online, but i'm getting lost as far as the different variables and elements that they refer to. (the example below isn't literally what i want to do, but i think it conveys the message.)

12 4 7 15 6 9
12 4 7 9 6 X

I want to take the lat number (9) and place it where the removed number(15) used to be.
 
Deep Purohit
Greenhorn
Posts: 9
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
==> Removing 7
12 4 7 15 6 9
12 4 X 15 6 9
12 4 15 X 6 9
12 4 15 6 X 9
12 4 15 6 9 X

once you write above algorithm, you will find a way to optimize it.
And then,
2. Have a length variable with original length as 52 and keep on reducing whenever you select a card. And use this variable to do all iterative operation instead of using array.length

I hope, it might clear the fog around!
 
Kirstie Fran
Ranch Hand
Posts: 33
Android Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what you've demonstrated is shifting. in my demonstration I didn't shift all the values over. I only moved the last one to fill the empty space. that is what I want to do.
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:It sounds like what you really need is to not use an array. Instead, use a List (such as ArrayList or LinkedList). That's got a remove(int) method that will do exactly what I think you want - remove the object at that position from the list (moving all the following elements up one), and returning that object.

Basically, arrays aren't intended to change size. A List is a better option whenever you want the size to be able to change.



I think this is still the best solution for your problem!!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!