how to clone an array

Discussion in 'Java' started by Mariano, Apr 1, 2007.

  1. Mariano

    Mariano Guest

    Then, i've a method:

    private String[] creaLista(ResultSet cursore, String type)

    this return me an array of string.

    Now, since i need to use this method many time in my class always with
    different parameters, so should be usefull create a new array for this
    task.

    Then my class is:

    ============================
    // ...
    public class Paziente extends javax.swing.JFrame {
    private String[] sin;
    // ...
    private void formWindowOpened(java.awt.event.WindowEvent evt) {
    // ...
    sin = creaLista(myRs, myType);
    // ...
    }
    // ...
    ============================

    at the my sin array contain only one element sin[0]; if I try to reach
    sin I obtain an out of bound exception, also if creaLista() have an
    array grater than 1 element.

    Who can help me???
     
    Mariano, Apr 1, 2007
    #1
    1. Advertisements



  2. Hello Marino,
    in order to create a clone of any kind of (Object)-array I would use the
    static method arraycopy from java.lang.System:

    arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
              Copies an array from the specified source array, beginning at
    the specified position, to the specified position of the destination
    array.
     
    Gernot Reichel, Apr 1, 2007
    #2
    1. Advertisements

  3. Mariano

    Mariano Guest



    import java.lang.System
    //...
    arraycopy(creaLista(rs, "SINTOMO"), 0, SINTOMO, 2);

    but arraycopy() is not found by my class
     
    Mariano, Apr 1, 2007
    #3
  4. Mariano

    Lew Guest

    Or java.util.Arrays.copyOf(T[], int) and its cousins.
    <http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#copyOf(T[], int)>

    -- Lew
     
    Lew, Apr 1, 2007
    #4
  5. Mariano

    Lew Guest

    That's because you didn't give the class name, as is always required to invoke
    static methods.

    System.arraycopy( ...

    -- Lew
     
    Lew, Apr 1, 2007
    #5
  6. Mariano

    Piotr Kobzda Guest

    Or simply .clone() an array.


    piotr
     
    Piotr Kobzda, Apr 1, 2007
    #6
  7. Mariano

    Tom Hawtin Guest



    I'm a bit confused as to what your code is attempting. The code above
    wont OOBE. Is creaLista actually returning a reference to the array that
    is already assigned to sin? If so, that is very confusing. Either don't
    return it, or don't update the instance variable.

    Other replies point to the mechanics of copying into an array of an
    appropriate size. However rather than doing that, I suggest declaring
    and initialising sin as:

    private final List<String> sin = new java.util.ArrayList<String>();

    There is no need to complicate your code with low level details which
    have already be solved elsewhere.

    Tom hawtin
     
    Tom Hawtin, Apr 1, 2007
    #7
  8. Mariano

    Guest Guest

    Since 1.5 you can actually do:

    import static java.lang.System.*;

    (not that I would recommend it)

    Arne
     
    Guest, Apr 1, 2007
    #8
  9. Mariano

    Mariano Guest

    I've resolve the problem thank you all :)
     
    Mariano, Apr 1, 2007
    #9
  10. Mariano

    Lew Guest

    Which is another way to give the class name, as is always required to invoke
    static methods.

    (not that I would recommend it - except that others have pointed out certain
    situations where it could benefit, maybe)

    -- Lew
     
    Lew, Apr 2, 2007
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.