Java people - how do i clone an array?

Discussion in 'OT Technology' started by Gonzani, May 9, 2006.

  1. Gonzani

    Gonzani Active Member

    Joined:
    Aug 12, 2003
    Messages:
    1,825
    Likes Received:
    0
    Location:
    Houston, TX
    i need to create a distinct array that has the same entries as another array that already has entries initialized. how do i do this?

    edit: checked google, gave me a bunch of stuff about cloning that we havent talked about in class :hs:
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    <Vector> class has a bunch of stuff like that which is awesome if you're working with objects
     
  3. Gonzani

    Gonzani Active Member

    Joined:
    Aug 12, 2003
    Messages:
    1,825
    Likes Received:
    0
    Location:
    Houston, TX
    havent learned anything about vector class either :(

    it's an intro to java course...we didnt go too far
     
  4. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    werd. fuck array's. Vectors ftw
     
  5. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    well time to color outside the lines then. show some initiation and learn ahead. i have never seen any of my software dev profs fail someone cause they did extra work or learnt ahead. they actually did better
     
  6. Gonzani

    Gonzani Active Member

    Joined:
    Aug 12, 2003
    Messages:
    1,825
    Likes Received:
    0
    Location:
    Houston, TX
    ehhh it's a review question for the final so i doubt she's even going to ask anything about vector class
     
  7. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    :squint:
     
  8. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    <Vector> is easy dude

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html

    thatll tell you all about it and if youre using netbeans itll give you all the member functions in a drop down once you "." Unless you're doing something super easy like an array of ints or something vector is the only way to fly
     
  9. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    for your purposes, vectors will probably work just as well any you will never notice. hell, they even resize accordingly.
     
  10. Gonzani

    Gonzani Active Member

    Joined:
    Aug 12, 2003
    Messages:
    1,825
    Likes Received:
    0
    Location:
    Houston, TX
    yall are thinking way more advanced than what ive learned. it's basic stuff (arrays of ints and strings). she hasnt covered vectors at all so it wont be on the final.
     
  11. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
  12. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    just create another array with the length of the original and copy the contents

    Code:
    // Create a copy of int array
    
    int[] a, b;
    a = [1,2,3,4,5,6];
    
    for (int i = 0; i < a.length; i++) {
          b[i] = a[i];
    }
    
     
  13. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    or wouldn't

    b = a;

    work as well?
     
  14. aphoric

    aphoric Even if god did exist, it would be necessary to ab

    Joined:
    Aug 29, 2003
    Messages:
    918
    Likes Received:
    0
    Location:
    Leaving Afghanistan
    That's not going to be a value copy, it will be a reference copy.
    What it sounds like you want to do is like this:

    byte[] bytes = new byte[length];
    System.arraycopy(buff, 0, bytes, 0, length);
     
  15. Gonzani

    Gonzani Active Member

    Joined:
    Aug 12, 2003
    Messages:
    1,825
    Likes Received:
    0
    Location:
    Houston, TX
    12. I've declared the array myArray as follows:
    char[] myArray = new char[25];
    Assume I've initialized the entries in myArray.

    13. Now write Java code that creates a distinct array that has the same entries as myArray (see #12).

    That's the prompt..

    HardTech i think what you put sounds right....but isn't that just making b point to the stuff in array a? i dont know, i suck at teh java. :(
     
  16. aphoric

    aphoric Even if god did exist, it would be necessary to ab

    Joined:
    Aug 29, 2003
    Messages:
    918
    Likes Received:
    0
    Location:
    Leaving Afghanistan
    that would be two references to the same array, I think he needs a new array, if he has not learned about System.arrayCopy(), then the loop is prolly what they are looking for
     
  17. aphoric

    aphoric Even if god did exist, it would be necessary to ab

    Joined:
    Aug 29, 2003
    Messages:
    918
    Likes Received:
    0
    Location:
    Leaving Afghanistan
    make sure you initialize before you fill
     
  18. Gonzani

    Gonzani Active Member

    Joined:
    Aug 12, 2003
    Messages:
    1,825
    Likes Received:
    0
    Location:
    Houston, TX
    sooo then System.arraycopy() is the same as cloning an array? i thought it was different?
     
  19. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    oh oops... for some reason, I thought that the array would already be initialized, kind of like a regular object.
     
  20. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    if this is going to be for an introductory programming class, don't worry about that stuff... just worry about the logic

    to do what you're asking, you have to do the following:

    1. initialize the new array, assuming the array you want to copy is already initialized and populated

    2. create a loop that iterates as many times as there are elements in the array to copy (for example, an array of 10 elements will loop 10 times)

    3. copy the elements of the array to copy to the new array, one at a time
     
  21. Gonzani

    Gonzani Active Member

    Joined:
    Aug 12, 2003
    Messages:
    1,825
    Likes Received:
    0
    Location:
    Houston, TX
    ya you were right. all she wanted was the stuff copied, i dont know why i kept saying clone. thank you!
     

Share This Page