Results 1 to 12 of 12
- 09-24-2013, 03:28 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 35
- Rep Power
- 0
Sorting a Collection After Removing an Item
Hello, I am trying to get my program to remove and item from my collection and then move all the remaining items back together (removing the space from the removed item). My items are displayed through a GUI application as columns. My problem is once I remove the item my program leaves a space. I need it to move the remaining items to the right of the removed item a spot to the left to delete the space. I know it has something to do with making a new array that is a size smaller and displaying that, but I am having trouble displaying the new array.
Java Code:/* * Method is used to remove items */ public void remove() { if ((newArray != null) && (size > 0) && (itemSelected > EMPTY) && (itemSelected < size)) { for (int i = itemSelected + 1; i < size; i++) newArray[i - 1] = newArray[i]; size--; } itemSelected = EMPTY; }
Java Code:// This method removes an item public void removeAction() { myCollection.reset(selectedItem); myCollection.remove(); selectedItem=null; repaint(); }
- 09-24-2013, 04:25 AM #2
Re: Sorting a Collection After Removing an Item
When people say Collection they are referring to List, Queue, Set etc. Not arrays.
In the loop if the remove method you are decrementing size each time you move an item. Since you are removing a single item wouldn't you want to decrement size once?
- 09-24-2013, 04:29 AM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Sorting a Collection After Removing an Item
Why not use a List? When you sort it using Collections.sort, it will maintain its order when you delete items.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-24-2013, 04:37 AM #4
Re: Sorting a Collection After Removing an Item
Trying to sort a collection after removing an item in GUI
Duplicate post
- 09-24-2013, 04:38 AM #5
Member
- Join Date
- Jun 2012
- Posts
- 35
- Rep Power
- 0
Re: Sorting a Collection After Removing an Item
Im sorry I shouldnt have said sort. My collection contains random values and I have to remove either the min or max value. I do not actually want them sorted from high to low or vice versa. My mistake. I just cant figure out a way to copy the remaining values to a smaller collection, then display the new collection.
- 09-24-2013, 04:44 AM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Sorting a Collection After Removing an Item
Use a List (see ArrayList). Then iterate thru the list and find the min or max value using the normal procedure (I assume you know that).
Whenever it changes, save the index. Then when you are done, remove the item in the list at that index. The collection size will be adjusted.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-24-2013, 04:46 AM #7
Re: Sorting a Collection After Removing an Item
So reply #2 didn't help? If not perhaps you could better explain what your problem is. Providing sample input and output and what it should be would help.
- 09-24-2013, 04:51 AM #8
Re: Sorting a Collection After Removing an Item
GRRRR!
Due to crappy formatting I though size-- was inside the for loop.
- 09-24-2013, 04:56 AM #9
Member
- Join Date
- Jun 2012
- Posts
- 35
- Rep Power
- 0
Re: Sorting a Collection After Removing an Item
Im not exactly sure what you meant in post number 2. I am pretty new to java and really dont want to bother you guys asking stupid questions so I have been googling what both of you suggested. Here is a screenshot of what my output is. I generate random numbers, paint the collection, remove the min, but its supposed to move the remaining items to the right of the space to the left to fill in the null space.
- 09-24-2013, 05:03 AM #10
Re: Sorting a Collection After Removing an Item
Its a bit hard to help without seeing all the code. Create a SSCCE that does not include all the GUI code.
- 09-24-2013, 05:05 AM #11
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Sorting a Collection After Removing an Item
Assuming that you are just having problem finding a min or max and adjusting the list, did you see post #6? What do you not understand about it?
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-24-2013, 05:08 AM #12
Member
- Join Date
- Jun 2012
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
How to add an collection item into an array list in a different class
By ajw1993 in forum New To JavaReplies: 1Last Post: 11-14-2012, 08:53 PM -
remove *this item/object from arrayList and window(jframe) via item button
By droidtech1 in forum New To JavaReplies: 10Last Post: 09-04-2012, 09:12 AM -
Removing old selection Rectangle when new item selected
By FALL3N in forum AWT / SwingReplies: 5Last Post: 07-08-2012, 10:46 PM -
Removing an Item from an array
By chris1 in forum New To JavaReplies: 3Last Post: 04-25-2011, 07:59 AM -
Getting an item from an array
By ile4 in forum New To JavaReplies: 9Last Post: 01-13-2011, 04:42 PM
Bookmarks