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 withdifferent parameters, so should be usefull create a new array for thistask.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 reachsin[i] I obtain an out of bound exception, also if creaLista() have anarray grater than 1 element.Who can help me???
|
|
0
|
|
|
|
Reply
|
mariano.calandra (49)
|
4/1/2007 5:08:08 PM |
|
In article <1175447288.612216.287770@n59g2000hsh.googlegroups.com>, "Mariano" <mariano.calandra@gmail.com> wrote:> 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] 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.
|
|
0
|
|
|
|
Reply
|
Gernot
|
4/1/2007 5:32:52 PM
|
|
On 1 Apr, 19:32, Gernot Reichel <ger...@die-reichels.de> wrote:> In article <1175447288.612216.287...@n59g2000hsh.googlegroups.com>,>>>> "Mariano" <mariano.calan...@gmail.com> wrote:> > 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] 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
|
|
0
|
|
|
|
Reply
|
Mariano
|
4/1/2007 5:48:32 PM
|
|
Gernot Reichel wrote:> 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[],%20int)>-- Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
4/1/2007 5:50:04 PM
|
|
Mariano wrote:> import java.lang.System> //...> arraycopy(creaLista(rs, "SINTOMO"), 0, SINTOMO, 2);> > but arraycopy() is not found by my classThat's because you didn't give the class name, as is always required to invoke static methods.System.arraycopy( ...-- Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
4/1/2007 5:51:09 PM
|
|
Lew wrote:> Gernot Reichel wrote:>> 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[],%20int)> Or simply .clone() an array.piotr
|
|
0
|
|
|
|
Reply
|
Piotr
|
4/1/2007 6:02:13 PM
|
|
Mariano wrote:
> 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] 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
|
|
0
|
|
|
|
Reply
|
Tom
|
4/1/2007 6:31:28 PM
|
|
Lew wrote:> Mariano wrote:>> 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( ...Since 1.5 you can actually do:import static java.lang.System.*;(not that I would recommend it)Arne
|
|
0
|
|
|
|
Reply
|
UTF
|
4/1/2007 6:45:42 PM
|
|
On 1 Apr, 20:45, Arne Vajh=F8j <a...@vajhoej.dk> wrote:> Lew wrote:> > Mariano wrote:> >> 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( ...>> Since 1.5 you can actually do:>> import static java.lang.System.*;>> (not that I would recommend it)>> ArneI've resolve the problem thank you all :)
|
|
0
|
|
|
|
Reply
|
Mariano
|
4/1/2007 8:38:26 PM
|
|
Lew wrote:>>> That's because you didn't give the class name, as is always required to>>> invoke static methods.>>> System.arraycopy( ...Arne Vajhøj <a...@vajhoej.dk> wrote:>> Since 1.5 you can actually do:>>>> import static java.lang.System.*;>>>> (not that I would recommend it)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
|
|
0
|
|
|
|
Reply
|
Lew
|
4/2/2007 1:33:19 AM
|
|
|
9 Replies
132 Views
(page loaded in 0.359 seconds)
|