how to clone an array


M

Mariano

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???
 
Ad

Advertisements

G

Gernot Reichel

"Mariano said:
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???


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

Mariano

Mariano said:
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???

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.


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

but arraycopy() is not found by my class
 
L

Lew

Gernot said:
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.

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
 
L

Lew

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

but arraycopy() is not found by my class

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

System.arraycopy( ...

-- Lew
 
P

Piotr Kobzda

Lew said:
Gernot said:
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.

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

Or simply .clone() an array.


piotr
 
Ad

Advertisements

T

Tom Hawtin

Mariano said:
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.


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
 
G

Guest

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

System.arraycopy( ...

Since 1.5 you can actually do:

import static java.lang.System.*;

(not that I would recommend it)

Arne
 
Ad

Advertisements

L

Lew

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
 

Top