Results 1 to 6 of 6

Thread: lost values

  1. #1
    emosms is offline Member
    Join Date
    May 2010
    Posts
    29
    Rep Power
    0

    Default lost values

    Hi,
    I am trying to resize an aray having the following method:
    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;
            }
        }
    But it seems that values ar lost when copying, I have the following output:

    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:
    Java Code:
    public Item[] a; // queue items
    and initiated in the constructor:
    Java Code:
    a = (Item[]) new Object[1];
    The client populates the array, but each time it goes to resizing (enlarging) the old values are just NULL.

    Here is the method that adds items (called enqueue, but in fact is just pushing ellements into the array for now):
    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;
    }
    Dont know why when I call the class variable a[] to copy it, it has NULL values except at index [0]

    Best Regards

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

    Default

    What does this do inside the for loop?

    Java Code:
    a = temp;
    Also, have you looked at System.arraycopy?

  3. #3
    emosms is offline Member
    Join Date
    May 2010
    Posts
    29
    Rep Power
    0

    Default

    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 notebook
    Last edited by emosms; 03-05-2011 at 07:49 PM.

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

    Default

    Quote Originally Posted by emosms View Post
    what is system arraycopy?
    It is schools assignment, we are to implement the resize() method ourselves.
    fair enough.



    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
    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.

  5. #5
    emosms is offline Member
    Join Date
    May 2010
    Posts
    29
    Rep Power
    0

    Default

    god, i've spent the whole afternoon not seeing it. this is insane.
    thx!

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

Similar Threads

  1. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  2. Ok i am lost
    By jaisan72980 in forum New To Java
    Replies: 3
    Last Post: 01-17-2011, 04:20 AM
  3. I'm lost :(
    By leonardjr in forum New To Java
    Replies: 8
    Last Post: 03-03-2009, 04:18 AM
  4. So Lost
    By kandt in forum New To Java
    Replies: 5
    Last Post: 12-13-2008, 09:55 PM
  5. Lost my javadocs
    By orchid in forum Eclipse
    Replies: 3
    Last Post: 04-30-2008, 09:45 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
  •