aspose file tools*
The moose likes Beginning Java and the fly likes Proper way to remove and shift elements of an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Proper way to remove and shift elements of an array" Watch "Proper way to remove and shift elements of an array" New topic
Author

Proper way to remove and shift elements of an array

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
I am attempting to remove an element of an array(make it null) and then shift all non null elements to the front of the array. ie have no blanks.

Pretty much everywhere I look it says to use an arraylist, but I think that is beyond me for now. Also my book provides pictures and a few short words with no code on this subject. It mentions using a delete int and a last int to get this done.

There is also an explanation on a way similar to what I have below, but they dont show how to move the elements up in the list.



Thanks for the input.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2627
    
    9

Hello there,

An array is fixed size once you create it. Eg int[] intAry = new int[5]; will always have 5 elements in it.

To "manipulate" an array especially removing elements and making no blanks... you need to use a (array) list (a form of collection). The idea of array list is that it represents the array "as a" list.



K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You can try dissecting ArrayList and see how it does it In your Java™ installation folder there is a file called src.zip. Unzip that, and explore it (-->java-->util-->ArrayList).

To delete an element from the middle of an array:
  • There is no need to do anything with the element deleted.
  • Iterate along the array from 1 after the deleted element, to the last element.
  • Copy each element into the location 1 before it.
  • Set the last element to null
  • .

    This is much easier if you keep a count of how many elements you have in your array. If you have 15 elements in a 20-member array, and you delete that at index 7,
  • Set index 7 element equal to index 8
  • Set index 8 element equal to index 9
  • etc
  • Set index 13 element equal to index 14
  • Remember the last index in a 15-element array is 14.
  • Set index 14 element equal to null or zero.
  • [list]Reduce count by 1.[/listAdding an element at index 7 would be similar, but backwards, and there is no need to set anything null.

    There is a method in the System class which may be able to do the array copying for you more quickly.
    Mike Osterhout
    Ranch Hand

    Joined: Jun 29, 2009
    Posts: 84
    I am actually not able to use an array list for this project. I am looking for solutions to removing an element from an actual array and then making sure there arent any blanks...
    K. Tsang
    Bartender

    Joined: Sep 13, 2007
    Posts: 2627
        
        9

    If you are not allow to use array list then you probably want to use 2 arrays. One for the source and the other destination. Once you remove an item (make it null) from the source, you loop the source and check for null and put in destination array. Now I'm not showing the code.
    Mike Osterhout
    Ranch Hand

    Joined: Jun 29, 2009
    Posts: 84
    K. Tsang wrote: you loop the source and check for null and put in destination array.


    What do you mean by this? I realize I need to do a loop but I would think once it hits a null that loop would stop and there could be elements after that null element that are not copied over.
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 19073
        
      40

    Mike Osterhout wrote:
    What do you mean by this? I realize I need to do a loop but I would think once it hits a null that loop would stop and there could be elements after that null element that are not copied over.


    A loop "would stop" when you, as a developer, says it should stop. If it stops when "it hits a null", it is because you coded it as such. If you don't want your loop to stop at a null, then don't code it that way.

    Henry


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Mike Osterhout
    Ranch Hand

    Joined: Jun 29, 2009
    Posts: 84
    Ok well, the best solution that I have seen would be to set an element in the array to null like I have done above, and then shift all elements after the removed element down one and lastly add a null reference to the end of the array to keep the array the same size.

    Unfortunately my book only talks about this, and shows a picture. I have given it some serious thought, but I am not sure how I would go about shifting the elements down one based on the removed element/ the number entered by the user...

    if(element == null){
    skip it}

    if(element != null){
    add it to the new array}

    //somehow ensure that the new array is 200 long


    How is that?
    Mike Osterhout
    Ranch Hand

    Joined: Jun 29, 2009
    Posts: 84
    THis is my new code, unfortunately it still gives me a blank element at the beginning.

    Mike Osterhout
    Ranch Hand

    Joined: Jun 29, 2009
    Posts: 84
    Actually, I think my issue stems from using the count integer.

    Is there a way to get the index # of the array? That way when I print the inventory it prints the index rather than my count variable, as the id.

    Also I just found this tutorial which explains arrays very well...
    http://www.geekpedia.com/tutorial271_Introduction-to-Arrays-in-Java.html
    K. Tsang
    Bartender

    Joined: Sep 13, 2007
    Posts: 2627
        
        9

    Ok I think you are making it more complicated than it sounds.



    Do check the different combination because it may be wrong or not what you expect.
    Mike Osterhout
    Ranch Hand

    Joined: Jun 29, 2009
    Posts: 84
    looks good. but is there anyway to print the index number of an array to the screen rather than using a count?

    maybe something like.


    I found this bit of code


    However k is a value and not an index...
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    It would be easier to understand if you wrote an array-deleting class. Write a class which incorporates an array, deletes an element from the middle of the array, and shifts all the other elements.

    You will learn more easily if you modularise your code. Create code which does one thing, get it working, then incorporate it into your code. And I shall give you a hint how you can use itWhen you get that working, you have a nice utility method. Better design that inlining the code. You can do the whole thing without using two arrays.

    When you have got that working, how about writing a similar insertMemberIntoArray(Object[], int) method. Beware: If you pass a "full" array, you will have problems about what you do with the "last" element. You can create a copyArrayIntoLargerArray method. Note that class should have only static members, and the private constructor prevents anybody instantiating it.

    I shall have a look at your "how to print index" thread and make a suggestion there.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Proper way to remove and shift elements of an array