Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

how to clone an array

Thread view: 
Mariano - 01 Apr 2007 18:08 GMT
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???
Gernot Reichel - 01 Apr 2007 18:32 GMT
> Then, i've a method:
>
[quoted text clipped - 26 lines]
>
> 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.
Mariano - 01 Apr 2007 18:48 GMT
> In article <1175447288.612216.287...@n59g2000hsh.googlegroups.com>,
>
[quoted text clipped - 37 lines]
> 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
Lew - 01 Apr 2007 18:51 GMT
> 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
Arne Vajhøj - 01 Apr 2007 19:45 GMT
>> import java.lang.System
>> //...
[quoted text clipped - 6 lines]
>
> System.arraycopy( ...

Since 1.5 you can actually do:

import static java.lang.System.*;

(not that I would recommend it)

Arne
Mariano - 01 Apr 2007 21:38 GMT
> >> import java.lang.System
> >> //...
[quoted text clipped - 14 lines]
>
> Arne

I've resolve the problem thank you all :)
Lew - 02 Apr 2007 02:33 GMT
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
Lew - 01 Apr 2007 18:50 GMT
> in order to create a clone of any kind of (Object)-array I would use the
> static method arraycopy from java.lang.System:
[quoted text clipped - 3 lines]
> 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
Piotr Kobzda - 01 Apr 2007 19:02 GMT
>> in order to create a clone of any kind of (Object)-array I would use
>> the static method arraycopy from java.lang.System:
[quoted text clipped - 6 lines]
> 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
Tom Hawtin - 01 Apr 2007 19:31 GMT
> public class Paziente extends javax.swing.JFrame {
>         private String[] sin;
[quoted text clipped - 10 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.