Results 1 to 6 of 6
Thread: lost values
- 03-05-2011, 07:21 PM #1
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
lost values
Hi,
I am trying to resize an aray having the following method:
But it seems that values ar lost when copying, I have the following output:Java Code:private void resize(int size) { // resizing the array Item[] temp = (Item[]) new Object[size]; for (int i = 0; i < N; i++) { temp[i] = a[i]; // move the content to the new array System.out.println("1. Old array value: " + a[i] + "; @ index: " + i); System.out.println("2. Temp array value: " + temp[i] + "; @ index: " + i); a = temp; } }
Enqueued Item: 2; @ array index: 1; Array length: 2
1. Old array value: 1; @ index: 0
2. Temp array value: 1; @ index: 0
1. Old array value: null; @ index: 1
2. Temp array value: null; @ index: 1
a is a generic array declared as:
and initiated in the constructor:Java Code:public Item[] a; // queue items
The client populates the array, but each time it goes to resizing (enlarging) the old values are just NULL.Java Code:a = (Item[]) new Object[1];
Here is the method that adds items (called enqueue, but in fact is just pushing ellements into the array for now):
Dont know why when I call the class variable a[] to copy it, it has NULL values except at index [0]Java Code:public void enqueue(Item item) { // add an item if (N == a.length) { resize(2 * a.length); // double the array a[N] = item; } a[N++= item; }
Best Regards
-
What does this do inside the for loop?
Also, have you looked at System.arraycopy?Java Code:a = temp;
- 03-05-2011, 07:47 PM #3
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
what is system arraycopy?
It is schools assignment, we are to implement the resize() method ourselves.
a = temp; - supposed to assign back the whole new array object to the variable a
BTW, the resize() method is as is from our notebookLast edited by emosms; 03-05-2011 at 07:49 PM.
-
fair enough.
But as I mentioned before, look at where you're doing this -- inside the for loop. You need to think logically what this will do to the array that you're trying to convert.a = temp; - supposed to assign back the whole new array object to the variable a
BTW, the resize() method is as is from our notebook
- 03-05-2011, 09:12 PM #5
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
god, i've spent the whole afternoon not seeing it. this is insane.
thx!
-
Similar Threads
-
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
Ok i am lost
By jaisan72980 in forum New To JavaReplies: 3Last Post: 01-17-2011, 04:20 AM -
I'm lost :(
By leonardjr in forum New To JavaReplies: 8Last Post: 03-03-2009, 04:18 AM -
So Lost
By kandt in forum New To JavaReplies: 5Last Post: 12-13-2008, 09:55 PM -
Lost my javadocs
By orchid in forum EclipseReplies: 3Last Post: 04-30-2008, 09:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks