Results 1 to 7 of 7
  1. #1
    bap2 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default Need help with circular arrays

    I need to double the capacity and copy the array into the new one. This is the code that I have right now, but i can't figure out what I need to do from this point on.

    Java Code:
    //Expand the array to double its current size if the queue is full and //reallocate the array
        private void expand()
        {
           int newSize = 2*size;
           String[] newData = new String[data.length];
           int j = head;
           for(int i=0; i<size; i++)
           {
               newData[i] = data[j];
               j = (j+1)%size;
           }
           head = 0;
           tail = size - 1;
           size = newSize;
           data = new String[size];
           
        }
    Any help would be appreciated.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Java Code:
           for(int i=0; i<size; i++)
           {
               newData[i] = data[j];
               j = (j+1)%size;
           }
    Assuming this is code to create a copy of the current array then what is all the stuff to do with "j"? Why not simply "newData[i] = data[i];"?

    Anyway, why not make newData the new size rather than the current size, then you can simply go "data = newData;" at the end?
    Last edited by Tolls; 11-04-2010 at 05:05 PM. Reason: Oops wrote 1 instead of i.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bap2 View Post
    I need to double the capacity and copy the array into the new one. This is the code that I have right now, but i can't figure out what I need to do from this point on.

    Java Code:
    //Expand the array to double its current size if the queue is full and //reallocate the array
        private void expand()
        {
           int newSize = 2*size;
           String[] newData = new String[data.length];
           int j = head;
           for(int i=0; i<size; i++)
           {
               newData[i] = data[j];
               j = (j+1)%size;
           }
           head = 0;
           tail = size - 1;
           size = newSize;
           data = new String[size];
           
        }
    Any help would be appreciated.
    You're allocating the new data with the wrong size (i.e. the size of the old array) and at the end of your method you're allocating it again (of the correct size).

    kind regards,

    Jos

  4. #4
    bap2 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    Could someone post what the code should look like. Because I have been working on this assignment for two days now and still can't get it right.

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by bap2 View Post
    Could someone post what the code should look like.
    Sorry, this isn't a homework service. By your code, I can tell that you understand the material. Tolls and Jos have given you a push in the right direction and told you everything wrong with your current code--try fixing it up.

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

    Default

    I suggest you address both of Jos' observations.

    "You're allocating the new data with the wrong size (i.e. the size of the old array)". Do you see where you are creating a new array with the with wrong size (the size of the old array)? If so, change that line so the array is made the size you mean it to be.

    "at the end of your method you're allocating it again". Do you see the line of code that is assigning to data an array the correct size but full of null values? If so, change that line so it assigns to data the array that you really want to function as the data.

    (And if you don't understand either of the points that were made, ask about that.)

  7. #7
    bap2 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

Similar Threads

  1. Replies: 9
    Last Post: 11-04-2011, 04:09 AM
  2. Problem prioritizing a circular queue
    By Metastar in forum New To Java
    Replies: 1
    Last Post: 10-04-2010, 12:40 AM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  4. Circular Double Linked List
    By theonly in forum Advanced Java
    Replies: 3
    Last Post: 12-06-2009, 06:10 PM
  5. Trouble Developing Singly Linked Circular List
    By VinceGuad in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 05:38 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
  •