Results 1 to 4 of 4
  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Compacting arrays.

    Hey guys im workin on a problem to remove a cd from an array of cd's and shift the array to fill in gaps.I have no idea how to go about doing it.

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    If you are allowed by the assignment requirements, don't use an array but rather an ArrayList. It's built for just this sort of thing.

  3. #3
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Yea i actually need to just add a "remove method" to one of Cd collection classes in the book. Dont no what to do.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    In very general terms you would get the index of the CD in the collection then, starting from the next position in the array copy each element one place towards the start. Think about what you would do when you reach the end of the array.

    Java Code:
          to delete == arr[4]
             |
             v
    A  B  C  D  E  F  G  H
    
    A  B  C  E  E  F  G  H   copy arr[5]->arr[4]
    A  B  C  E  F  F  G  H   [6]->[5]
    A  B  C  E  F  G  H  H   [7]->[6]
    A  B  C  E  F  G  H  ?   ?
    Bear in mind that we don't have this book so it's rather hard to give more than very general advice without seeing some code (and preferably your attempt: either of the previous paragraph or something you can come up with.)

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  3. 2D Arrays
    By Major90 in forum New To Java
    Replies: 6
    Last Post: 11-06-2008, 02:08 PM
  4. Need help with Arrays
    By dietgal in forum New To Java
    Replies: 21
    Last Post: 10-08-2008, 01:59 PM
  5. Help with Arrays
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 05:12 AM

Posting Permissions

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