Results 1 to 5 of 5
  1. #1
    topcat1325 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default Insert and Shift Array

    I have to add song to a song list and if I am adding a song to the play list at a position where the array element is not null then I need to shift all the elements to the right and insert the song.

    I am not shifting correctly - I am wondering if I am doing it correctly or if there is a more efficient way?


    this is a method in class
    the
    Java Code:
    size
    variable is to keep track how many items are in my array.

    I have tried:
    Java Code:
     for(int i = position-1; i<songList.length; i++){
      songList[i+1] = songList[i];
    }
    which the code obv doesn't work and spits out incorrect data.
    -----------------------------------------------------------------------------------

    my next attempt is bellow with my method - it doesn't seem to work - can someone point out what I am doing wrong?

    Thanks - any help is appreciated!

    Java Code:
      
        public void addSong(SongRecord mySong, int position) throws  FullPlayListException,
                IllegalArgumentException
        {
          
          if(position-1 > _SIZE || position-1 < 0)
              throw new IllegalArgumentException("Error adding song on position " + position + ". " +
                      "Please insert a valid number 1-50");
    
          if(size == _SIZE)
              throw new FullPlayListException("Playlist is full!");
    
          if(size != _SIZE)
          {
              if(songList[position-1] == null)
              {
                   songList[position-1] = mySong;
                   size += 1;
              } else if(songList[position-1] != null)
              {
                   try {
                       
                      System.arraycopy(songList, position-1, songList, position-1, size);
                   }catch(IndexOutOfBoundsException e){
                        System.out.print("Can not add song here!");
                   }
              }
          }
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Have you considered using an ArrayList or LinkedList here instead of your fixed size array? Or are you not allowed to do this due to assignment restrictions?

  3. #3
    topcat1325 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default reply

    Do to restrictions for the assignment I can not use any other data structure.

  4. #4
    topcat1325 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default extra thought

    I did some extra work - so no one thinks I am sitting around.

    this seems to remove the position that is to the right of the position I desire instead of shifting.

    Java Code:
     try {
                      int numElts = size ;
                      System.arraycopy(songList, position, songList, position-1, numElts);
                      songList[position-1] = mySong;
                   }catch(IndexOutOfBoundsException e){
                        System.out.print("Can not add song here!");
                   }

  5. #5
    topcat1325 is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default got it

    I believe I got it but still fuzzy why it works - can some one explains to me why this shifts?

    Java Code:
                   try {
                      int numElts = size ;
                      System.arraycopy(songList, position-1, songList, position, numElts);
                      songList[position-1] = mySong;
                      size += 1;
                   }catch(IndexOutOfBoundsException e){
                        System.out.print("Can not add song here!");
                   }

Similar Threads

  1. How to Insert elements into one array
    By zlich in forum New To Java
    Replies: 8
    Last Post: 01-02-2010, 02:37 PM
  2. Replies: 2
    Last Post: 10-07-2009, 06:24 PM
  3. Replies: 3
    Last Post: 02-28-2009, 09:17 AM
  4. How to insert a Value into a stored array ?
    By A.M.S in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 02:21 PM
  5. Anyway to fix the lines so they dun shift?
    By PeterFeng in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 10:26 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
  •