Results 1 to 12 of 12
  1. #1
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default 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();		
    		}

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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?

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Sorting a Collection After Removing an Item


  5. #5
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default 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.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Sorting a Collection After Removing an Item

    GRRRR!

    Due to crappy formatting I though size-- was inside the for loop.

  9. #9
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default 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.

    Sorting a Collection After Removing an Item-untitled.png

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Sorting a Collection After Removing an Item

    I have the correct code to find the min and max, the only problem is displaying the new list without the null space. I am currently working on your suggestion from post number 6. Thank you.

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2012, 08:53 PM
  2. Replies: 10
    Last Post: 09-04-2012, 09:12 AM
  3. Replies: 5
    Last Post: 07-08-2012, 10:46 PM
  4. Removing an Item from an array
    By chris1 in forum New To Java
    Replies: 3
    Last Post: 04-25-2011, 07:59 AM
  5. Getting an item from an array
    By ile4 in forum New To Java
    Replies: 9
    Last Post: 01-13-2011, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •