• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Proper way to remove and shift elements of an array

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 3648
    16
    Android Mac OS X Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Mike Osterhout
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    THis is my new code, unfortunately it still gives me a blank element at the beginning.

     
    Mike Osterhout
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 3648
    16
    Android Mac OS X Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic