Results 1 to 7 of 7
Thread: Need help with circular arrays
- 11-04-2010, 02:51 PM #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.
Any help would be appreciated.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]; }
- 11-04-2010, 03:53 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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];"?Java Code:for(int i=0; i<size; i++) { newData[i] = data[j]; j = (j+1)%size; }
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 04:05 PM. Reason: Oops wrote 1 instead of i.
- 11-04-2010, 03:58 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
- 11-04-2010, 07:27 PM #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.
- 11-04-2010, 07:38 PM #5
- 11-04-2010, 07:46 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
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.)
- 11-04-2010, 08:39 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Basic Circular Linked List - addFirst() method works improperly
By carlodelmundo in forum New To JavaReplies: 9Last Post: 11-04-2011, 03:09 AM -
Problem prioritizing a circular queue
By Metastar in forum New To JavaReplies: 1Last Post: 10-03-2010, 11:40 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Circular Double Linked List
By theonly in forum Advanced JavaReplies: 3Last Post: 12-06-2009, 05:10 PM -
Trouble Developing Singly Linked Circular List
By VinceGuad in forum New To JavaReplies: 14Last Post: 02-25-2009, 04:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks