# Thread: Need help with circular arrays

1. Member
Join Date
Oct 2010
Posts
20
Rep Power
0

## 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];
for(int i=0; i<size; i++)
{
newData[i] = data[j];
j = (j+1)%size;
}
tail = size - 1;
size = newSize;
data = new String[size];

}```
Any help would be appreciated.

2. Moderator
Join Date
Apr 2009
Posts
13,323
Rep Power
23
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. Originally Posted by bap2
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];
for(int i=0; i<size; i++)
{
newData[i] = data[j];
j = (j+1)%size;
}
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. Member
Join Date
Oct 2010
Posts
20
Rep Power
0
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. Originally Posted by bap2
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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
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.

7. Member
Join Date
Oct 2010
Posts
20
Rep Power
0
Sorry I won't ask for code again.

#### Posting Permissions

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