System.arraycopy and Objects

Hi,
I currently have a 100 objects created in my program. Each object has a number of double arrays assosiated with it.
During my code I use the System.arrayCopy method to copy the arrays from the best 50 objects into the worst 50 objects.
This seems to work fine. However someone has told me that when u use the arrayCopy method instead of the array being copied a pointer is just created pointing to the original array ?? . So that if I then modify one of the values in the original array the copied array will also be changed .
Is this correct ??? if it is how can I get around this problem ??
Thank you
Craig

Why don't you try for yourself?int[] a = new int[] {
    1,
    2
int[] b = new int[a.length];
System.arraycopy (a, 0, b, 0, a.length);
b[1] = 3;
System.out.println (a[1]);Kind regards,
Levi

Similar Messages

  • Performance of System.arraycopy and Arrays.fill

    I have some code where I call a function about 1,000,000 times. That function has to allocate a small array (around 15 elements). It would be much cheaper if the client could just allocate the array one single time outside of the function.
    Unfortunately, the function requires the array to have all of its elements set to null. With C++, I would be able to memset the contents of the array to null. In Java, the only methods I have available to me are System.arraycopy() and Arrays.fill().
    Apparently, Arrays.fill() is just a brain-dead loop. It costs more for Arrays.fill() to set the elements to null than it does to allocate a new array. (I'm ignoring possible garbage collection overhead).
    System.arraycopy is a native call (that apparently uses memcpy). Even with the JNI overhead, System.arraycopy runs faster than Arrays.fill(). Unfortunately, it's still slower to call System.arraycopy() than it is to just allocate a new array.
    So, the crux of the problem is that the heap allocations are too slow, and the existing means for bulk setting the elements of an array are even slower. Why doesn't the virtual machine have explicit support for both System.arraycopy() and Arrays.fill() so that they are performed with ultra-efficient memsets and memcpys and sans the method call and JNI overhead? I.E. something along the lines of two new JVM instructions - aarraycpy/aarrayset (and all of their primitive brethern).
    God bless,
    -Toby Reyelts

    A newly allocated array begins its life with null in its elements. There is no need to fill it with null.
    As Michael already stated, I'm not redundantly resetting all of the elements to null. Here's some code that demonstrates my point. You'll have to replace my PerfTimer with your own high performance timer. (i.e. sun.misc.Perf or whatever) Also note that the reason I'm only allocating half the array size in allocTest is to more accurately model my problem. The size of the array I need to allocate is variable. If I allocate the array outside of the function, I'll have to allocate it at a maximum. If I allocate inside the function, I can allocate it at exactly the right size.import java.util.*;
    public class AllocTest {
      private static final int count = 100000;
      public static void main( String[] args ) {
        for ( int i = 0; i < 10; ++i ) {
          allocTest();
        double allocStartTime = PerfTimer.time();
        allocTest();
        double allocTime = PerfTimer.time() - allocStartTime;
        for ( int i = 0; i < 10; ++i ) {
          copyTest();
        double copyStartTime = PerfTimer.time();
        copyTest();
        double copyTime = PerfTimer.time() - copyStartTime;
        for ( int i = 0; i < 10; ++i ) {
          fillTest();
        double fillStartTime = PerfTimer.time();
        fillTest();
        double fillTime = PerfTimer.time() - fillStartTime;
        System.out.println( "AllocTime (ms): " + allocTime / PerfTimer.freq() * 1000 );
        System.out.println( "CopyTime (ms): " + copyTime / PerfTimer.freq() * 1000 );
        System.out.println( "FillTime (ms): " + fillTime / PerfTimer.freq() * 1000 );
      private static void allocTest() {
        for ( int i = 0; i < count; ++i ) {
          Object[] objects = new Object[ 8 ];
      private static void copyTest() {
        Object[] objects = new Object[ 15 ];
        Object[] emptyArray = new Object[ 15 ];
        for ( int i = 0; i < count; ++i ) {
          System.arraycopy( emptyArray, 0, objects, 0, emptyArray.length );
      private static void fillTest() {
        Object[] objects = new Object[ 15 ];
        for ( int i = 0; i < count; ++i ) {
          Arrays.fill( objects, null );
    }I getH:\>java -cp . AllocTest
    AllocTime (ms): 9.749283777686829
    CopyTime (ms): 13.276827082771694
    FillTime (ms): 16.581995756443906So, to restate my point, all of these times are too slow just to perform a reset of all of the elements of an array. Since AllocTime actually represents dynamic heap allocation its number is good for what it does, but it's far too slow for simply resetting the elements of the array.
    CopyTime is far too slow for what it does. It should be much faster, because it should essentially resolve to an inline memmove. The reason it is so slow is because there is a lot of call overhead to get to the function that does the actual copy, and that function ends up not being an optimized memmove. Even so, one on one, System.arraycopy() still beats heap allocation. (Not reflected directly in the times above, but if you rerun the test with equal array sizes, CopyTime will be lower than AllocTime).
    FillTime is unbelievably slow, because it is a simple Java loop. FillTime should be the fastest, because it is the simplest operation and should resolve to an inline memset. Fills should run in single-digit nanosecond times.
    God bless,
    -Toby Reyelts

  • System.arraycopy for objects?

    I am getting weird results here. I create an array of Test2 objects. The Test2 toString method simply returns the args from the constructor. When I try to copy the array using arraycopy, I just get the number 3 in the second array. Why is this happening?
         Test2[] ar1 = new Test2[7];
              Test2[] ar2 = new Test2[9];
              ar1[0] = new Test2("1");
              ar1[1] = new Test2("2");
              ar1[2] = new Test2("3");
              ar1[3] = new Test2("4");
              ar1[4] = new Test2("5");
              ar1[5] = new Test2("6");
              ar1[6] = new Test2("7");
              for(int i = 0; i < ar1.length; i++){
                   System.out.print(ar1[i] + ", ");
              System.out.println();
              System.arraycopy(ar1, 0, ar2, 0, 5);
              for(int i = 0; i < ar2.length; i++){
                   System.out.print(ar1[2] + ", ");
              }This is the output:
    1, 2, 3, 4, 5, 6, 7,
    3, 3, 3, 3, 3, 3, 3, 3, 3,
    My textbook says that copying the array will copy the reference to the object. Why isn't it working? I must be missing something obvious. Thanks in advance.
    ps. When I remove the toString method, all the elements of the second array have the reference of the third object. This is the address for the third element of the first array:
    Test2@3bad086a, Test2@3bad086a, Test2@3bad086a, Test2@3bad086a, Test2@3bad086a, Test2@3bad086a
    Edited by: 837443 on Feb 27, 2011 10:40 AM

    837443 wrote:
    I must be missing something obvious. Um, yeah.
              for(int i = 0; i < ar2.length; i++){
                   System.out.print(ar1[2] + ", ");
    ps. When I remove the toString method, all the elements of the second array have the reference of the third object. No, they don't.
    This is the address for the third element of the first array:It's not the address.

  • System.arraycopy (2 dim array) and growth of 2 dim array

    Hi everybody
    I am working on a program which contains a module that can perform Cartesian product on number of sets.
    The code I have developed so far is :
    import java.lang.reflect.Array;
    public class Cart5 {
    public static void main(String[] args) throws Exception
    int pubnewlength;
    // declare SolArray
    int[][] solArray;
    // initialize solArray
    solArray=new int[1][4];
    // Use for method
    for (int ii=0 ; ii<4 ; ii++)
    solver(solArray,ii);
    // Print the array ?
    System.out.println("\n  The array was changed ... " );
    }  // End main
    public void solver(int Solarray2[][] , int abi)
    int[][]  A  =  {  {1,2,3,5},
                      {4,6,7},
                      {11,22,9,10},
                      {17,33}
      jointwoArrays(solarray2,A,abi);
    // some other operations
    } // End Solver method
    public void jointwoArrays(int solarray3[][] , int aArray[][],int indexA)
    int y,u;
    int[][] tempArray;
    // calculate growth of rows:
    pubnewlength=solArray3.length * aArray[indexA].length;
    //Fill TempArray
    y=solArray3[0].length;
    u=solArray3.length;
    tempArray=new int[u][y];
    // Use system.arraycopy to copy solArray3 into tempArray -- How ?
    // Change the size of arrow to proper size -- How ?
    solArray3 = (int[][]) arrayGrow(solArray3);
    // Join operation - Still under construction
    for(int i = 0, k = 0; i < tempArray.length; i++)
                   for(int j = 0; j < set3.length; j++)
                                     for (q=0;q<=2;q++)             
                                      { solArray3[k][q] = tempArray[i][q];}
                                     solArray3[k][q]= aArray[indexA][j];
                                     ++k;
    } // End jointwoArrays method
    // This module is from http://www.java2s.com/ExampleCode/Language-Basics/Growarray.htm
        static Object arrayGrow(Object a) {
        Class cl = a.getClass();
        if (!cl.isArray())
          return null;
        Class componentType = a.getClass().getComponentType();
        int length = Array.getLength(a);
        int newLength = pubnewlength;
        Object newArray = Array.newInstance(componentType, newLength);
        System.arraycopy(a, 0, newArray, 0, length);
        return newArray;
    } // End ClassI deeply appreciate your help with these 3 questions :
    1. How can I use system.arraycopy to copy my two dimensional array? I have searched but examples seem to be about one dim arrays.
    2. How can I change the "static Object arrayGrow(Object a)" , to grow my two dimensional array ?
    3. If you know any codes or articles or java code regarding cartesian products , please tell me.
    Thank you
    Denis

    1. How can I use system.arraycopy to copy my two
    dimensional array? I have searched but examples seem
    to be about one dim arrays.That's because you can't do it in one call. You need to create a loop which copies each 'row".
    >
    2. How can I change the "static Object
    arrayGrow(Object a)" , to grow my two dimensional
    array ?Why do you make it so complicated (generic). Make it take an int[][] array instead, and see the answer from above.
    >
    3. If you know any codes or articles or java code
    regarding cartesian products , please tell me.There are probably lots of them if you google.
    Kaj

  • System and Object privileges question

    hello everyone.
    I was really making it a priority to really understand both system and object privileges for users. I have setup a couple of 'sandboxes' at home and have done lots of testing. So far, it has gone very well in helping me understand all the security involved with Oralce (which, IMHO, is flat out awesome!).
    Anyway, a couple of quick questions.
    As a normal user, what view can I use to see what permissions I have in general? what about permissions on other schemas?
    I know I can do a:
    select * from session_privs
    which lists my session privileges.
    What other views (are they views/data dictionary?) that I can use to see what I have? Since this is a normal user, they don't have access to any of the DBA_ views.
    I'll start here for now, but being able to see everything this user has, would be fantastic.
    Cheers,
    TCG

    Sorry. should have elaborated more.
    In SQLPLUS, (logged in while logged into my Linux OS), I am working to try and get sqlplus to display the results of my query so it is easy to read. Right now, it just displays using the first 1/4 or 1/3 of the monitor screen to the left. Make sense? So it does not stretch the results out to utilize the full screen. it is hard to break down and read the results because they are "stacked" on top of each other.
    Would be nice if I could adjust sqlplus so the results are easier to read.
    HTH.
    Jason

  • Query created in production system and object changeability

    Hi,
    The production BW has the object changeability for query elements set to 'Changeable original'. Some queries which have been created in this system can be changed, others, which have also been created in the system, can not.
    All queries are assigned to the development packate $TMP. None of them have been transported anywhere, nor have they been created somewhere else and transported into the system.
    The system is BW 3.5, SP 17.
    Has anyone got any idea what the problem could be?
    Best regards,
    Rita

    Hi,
    The object changeability in the system is set to 'Changeable Original' and I know where to switch from 'not changeable' to 'changeable original' to 'everything changeable'.
    The normal development cycle sees reports being created in the development system and then being transported through to the production system. Here, they should not be changeable. However, users with the relevant authorisation should be able to either -
      - create new copies of these reports and change them or
      - create new ad-hoc reports and also change these
    We have several of these reports on the production system. The object changeability is set to 'changeable original', as I mentioned. I would expect that all queries which have been created on this system and never transported (either in or out of the system) should be changeable, based on this setting. However, some are, some ar not. The error message when trying to change the 'non-changeable' ones is 'Operation falied! No error message available from the server'. If I make a copy of one of these queries, hoping to save is as an ad-hoc query and change it, the error message is 'query could not be saved due to a problem in transport'.
    How come some can be changed, others can't? Is there anywhere I can check what the difference is between the changeable and non-changeable reports which have been created on the system?
    Best regards,
    Rita

  • Change Object Server in SLD after system copy and SLD move?

    Hello,
    we did the following:
    - we migrated the SLD to the production system in our landscape
    - we copied the NWDI to a new system
    - the old object server was named sapsrv88 (object server attribute in the SLD) which was the hostname of the old SLD server
    Do I have to change the object server attribute to the new hostname of the SLD? Does this have any influence on the software components that we allready developed?
    Thanks and best regards
    Jens

    Hello Jens,
    you can rename the object server - details you'll find in SAP note 935245
    When you rename it, the ID of the objects in the SLD is changed to the new ID, so normally you shouldn't have any problems with that.
    If you're unsure about it, you can backup your SLD content using "Administration => Content => Backup => All Instances" - that backs up everything under /sld/active
    or backup the software components you devoped using http://<hostname>:5<instance-no.>00/SCD/SCDownload
    Best regards
    Cornelia

  • Difference between usage of "System -- Services for object" and GOS direct

    Hey folks,
    i was wondering if you know an answer for that problem:
    1) Start ME23N (no SAP Gui classic design!) and go to System - Services for Objects in order to start the GOS Toolbox. Try to to store a business document. In my case i don't have the authorisation S_WFAR_OBJ so i get the  Message no. 00398 "You do not have authorization for this function" Thats the behavior a want
    2) Start ME23N and activate GOS Toolbox by clicking directly on the button on the top left corner. Try to a store a business document. But there is no message coming up?! Same user, same PO! It seems that the Toolbox is just beeing restartet and thats it.
    Same behavior in IW53/IW33 too, no auth error message if GOS is started via "System --> Services for Objects".
    Thats our system:
    ERP ECC 6.04 with NW 7.01
    SAP Gui 7.20 PL 3 (signature design)
    Thanks for your help and best regards
    Olli
    Edited by: Oliver Grewenig on Jan 18, 2012 11:30 AM

    Hi Oliver,
    I have done a similar testing for Tcode FB03 since I have done Archivelink configuration for this FI object.
    Case 1:
    =======
    Start FB03 (no SAP Gui classic design!) and go to System - Services for Objects in order to start the GOS Toolbox. Try to to store a business document. In my case i don't have the authorisation S_WFAR_OBJ so i get the Message no. 00398 "You do not have authorization for this function"
    Case 2:
    ========
    Start FB03 and activate GOS Toolbox by clicking directly on the button on the top left corner. Try to a store a business document. Still got the same error message "You do not have authorization for this function".
    Later I added the required object in the authorization profile and it worked in both the cases mentioned above.
    Since the same program is being executed behind both the cases, it will check the same authorization object as designed.
    What I would suggest is that you perform this test again and ensure that no one modifies the authorization during your testing period.
    Regards,
    Deepak Kori

  • The System.arraycopy Functionality and copying array question

    When created arrays such as String[] myStringArray (for example), is it general good practice to use the System.arraycopy function to copy the array?
    Why isn't it good practice to use equal instead? Would this work just as well?
    String[] myStringArray = new String[] { "My", " Test"};
    String[] myStringArrayCopy = new String[myStringArray.length};
    myStringArrayCopy = myStringArrayCopy;Or is that just going to make them the same element in memory and if I change myStringArray in antoher part of the program that means myStringArrayCopy will change as well since it is the same thing?

    Youre right, the equals sign just assigns the new array same reference in memory as the old array so they are pointing to the same data.
    Im 90% sure of that.
    If you want to work with an array without changing the original array id suggest using the System.arraycopy method. If you dont mind the contents changing then use the = sign..(but then why use a new array at all?)
    Hope this helps, if not theres loads of more experienced people on the boards...
    Ocelot

  • Question on System.arraycopy method and multidimensional array

    I'm trying to copy from single dimensional to multidimensional array using System.arraycopy method. The following is my problem.
    1) I need to specify the index of the multidimensional array while copying. Can I do that ? If yes , how???
    eg ; int a[] = new int[3];
    int b[] = new int[3][2]; I need to copy from a to b
    I tired the following and I'm getting an error.
    System.arraycopy(a,0,b[][1],0,3);
    How Can I achieve the above?? PLease Help --------------

    Java doesn't have multidimensional arrays. When you see an int[][] it's an array of arrays of ints. The arrays of ints might have different lengths like this one:int[][] arr =
    {{1,2,3,4},
    {1,2,3},
    {1,2},
    {1}
    };Do I need to say that arraycopy as you see it would fail in this case?
    If you know what kind of arrays you'll have you can simply implement your own arraycopy method (but it will not be as effecient as System.arraycopy) with a simple for-loop.

  • Difference between system level privilege and object level privilege

    hi
    i just want to know the difference between system level privileges and object level privilege.
    please correct me if i am wrong
    with system level privilege user can create objects such as creating tables,view,synonyms etc
    where as in object privilege we can only manipulate operations on object i.e perform dml not ddl
    please help

    Hi,
    810345 wrote:
    hi
    i just want to know the difference between system level privileges and object level privilege.
    please correct me if i am wrong
    with system level privilege user can create objects such as creating tables,view,synonyms etc
    where as in object privilege we can only manipulate operations on object i.e perform dml not ddl There are some system privileges that only concern manipulating objects: SELECT ANY TABLE, for example.
    The main difference is that the system-level privileges tend to cover all objects of a certain type, including objects that haven't been created yet.
    Object-level privileges usually apply only to one specifi object, such as one particular table, and are lost if the object is dropped. (For example, if I create a table called table_x, give you SELECT pivileges on it, then you can query my table. But if I then drop table_x and re-create it, you will not be able to see it unless I grant the privilege again.)

  • Clone() and object copys

    I have been reading about String and arrays and trying to understand the difference between clone() and system.arraycopy..... Here are my questions regarding this:
    1. Why isn't there a deep copy for String, especially if we all need to keep writing code to make up for it?
    2. What happens when using the assignment operator for a non-array object, say String? Is it getting a reference, or new memory with a copy of the values? eg.
    String strThis= "Hello Java";
    String strNew = strThis;

    I have been reading about String and arrays and trying
    to understand the difference between clone() and
    system.arraycopy..... Look at the arguments for each method and read the JavaDoc - clone creates an object, whereas arraycopy copies a subarray to an existing subarray.
    Here are my questions regarding
    this:
    1. Why isn't there a deep copy for String, especially
    if we all need to keep writing code to make up for
    it?Sting objects are immutable, so what would you gain from a deep copy? What code do "we all need to keep writing to make up for it"?
    2. What happens when using the assignment operator for
    a non-array object, say String? Is it getting a
    reference, or new memory with a copy of the values?
    eg.
    String strThis= "Hello Java";
    String strNew = strThis;It gets a copy of the reference.

  • ArrayStoreException by System.arraycopy

    Hi!
    I've made a simple code, which uses System.arraycopy, but it constantly throws ArrayStoreException, even though I can't see any reason for this.
    (I've even run through it and watched the variables at breakpoints.) It seems as if it cannot convert Integer to Integer, but why?
              My code is here:
              import java.lang.reflect.Array;
    public class ArrayHandler {
         public static void main(String[] args) {
              try {
                   Integer[] startArray=new Integer[]{new Integer(1)};
                   Integer[] endArray=new Integer[]{new Integer(2)};
                   Object[] result=new ArrayHandler().appendArray(startArray, endArray);
              }catch(Throwable exception){
                   exception.printStackTrace();
    http://java.sun.com/j2se/1.3/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29
    Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified:
          * The src argument refers to an object that is not an array.
          * The dst argument refers to an object that is not an array.
          * The src argument and dst argument refer to arrays whose component types are different primitive types.
          * The src argument refers to an array with a primitive component type and the dst argument refers to an array with a reference component type.
          * The src argument refers to an array with a reference component type and the dst argument refers to an array with a primitive component type.
        Otherwise, if any actual component of the source array
        from position srcOffset through srcOffset+length-1
        cannot be converted to the component type of the destination array by assignment conversion,
        an ArrayStoreException is thrown.
        In this case, let k be the smallest nonnegative integer less than length
        such that src[srcOffset+k] cannot be converted to the component type of the destination array;
        when the exception is thrown,
        source array components from positions srcOffset through srcOffset+k-1
        will already have been copied to destination array positions dstOffset through dstOffset+k-1
        and no other positions of the destination array will have been modified.
        (Because of the restrictions already itemized,
        this paragraph effectively applies only to the situation
        where both arrays have component types that are reference types.)
         public Object[] appendArray(Object[] startArray,Object[] endArray) {
              Object[] outputArray=startArray;   
              Class startArrayClass=startArray.getClass();
              System.out.println(startArrayClass);
              Class endArrayClass=startArray.getClass();
              System.out.println(endArrayClass);
              if(startArrayClass !=endArrayClass){
                   //throw new Exception("startArrayClass !=endArrayClass");
              }else{
                   try{
                        outputArray=(Object[])Array.newInstance(startArrayClass, startArray.length+endArray.length);   
                        System.arraycopy(startArray,0,outputArray,0,startArray.length);
                        System.arraycopy(endArray,0,outputArray,startArray.length,endArray.length);
                        return outputArray;
                   }catch(Throwable exception){
                        exception.printStackTrace();
              return outputArray;
         }//appendArray
    }Could anybody share his experience, please.
    My best regards.

    astlanda wrote:(I've even run through it and watched the variables at breakpoints.)Thank's for your time.
    I'm using Eclipse and I know a bit how to debug with it.
    Everything was as expected, but I can't see into a native method like
         * Copies an array from the specified source array, beginning at the
         * specified position, to the specified position of the destination array.
         * A subsequence of array components are copied from the source
         * array referenced by <code>src</code> to the destination array
         * referenced by <code>dest</code>. The number of components copied is
         * equal to the <code>length</code> argument. The components at
         * positions <code>srcPos</code> through
         * <code>srcPos+length-1</code> in the source array are copied into
         * positions <code>destPos</code> through
         * <code>destPos+length-1</code>, respectively, of the destination
         * array.
         * <p>
         * If the <code>src</code> and <code>dest</code> arguments refer to the
         * same array object, then the copying is performed as if the
         * components at positions <code>srcPos</code> through
         * <code>srcPos+length-1</code> were first copied to a temporary
         * array with <code>length</code> components and then the contents of
         * the temporary array were copied into positions
         * <code>destPos</code> through <code>destPos+length-1</code> of the
         * destination array.
         * <p>
         * If <code>dest</code> is <code>null</code>, then a
         * <code>NullPointerException</code> is thrown.
         * <p>
         * If <code>src</code> is <code>null</code>, then a
         * <code>NullPointerException</code> is thrown and the destination
         * array is not modified.
         * <p>
         * Otherwise, if any of the following is true, an
         * <code>ArrayStoreException</code> is thrown and the destination is
         * not modified:
         * <ul>
         * <li>The <code>src</code> argument refers to an object that is not an
         *     array.
         * <li>The <code>dest</code> argument refers to an object that is not an
         *     array.
         * <li>The <code>src</code> argument and <code>dest</code> argument refer
         *     to arrays whose component types are different primitive types.
         * <li>The <code>src</code> argument refers to an array with a primitive
         *    component type and the <code>dest</code> argument refers to an array
         *     with a reference component type.
         * <li>The <code>src</code> argument refers to an array with a reference
         *    component type and the <code>dest</code> argument refers to an array
         *     with a primitive component type.
         * </ul>
         * <p>
         * Otherwise, if any of the following is true, an
         * <code>IndexOutOfBoundsException</code> is
         * thrown and the destination is not modified:
         * <ul>
         * <li>The <code>srcPos</code> argument is negative.
         * <li>The <code>destPos</code> argument is negative.
         * <li>The <code>length</code> argument is negative.
         * <li><code>srcPos+length</code> is greater than
         *     <code>src.length</code>, the length of the source array.
         * <li><code>destPos+length</code> is greater than
         *     <code>dest.length</code>, the length of the destination array.
         * </ul>
         * <p>
         * Otherwise, if any actual component of the source array from
         * position <code>srcPos</code> through
         * <code>srcPos+length-1</code> cannot be converted to the component
         * type of the destination array by assignment conversion, an
         * <code>ArrayStoreException</code> is thrown. In this case, let
         * <b><i>k</i></b> be the smallest nonnegative integer less than
         * length such that <code>src[srcPos+</code><i>k</i><code>]</code>
         * cannot be converted to the component type of the destination
         * array; when the exception is thrown, source array components from
         * positions <code>srcPos</code> through
         * <code>srcPos+</code><i>k</i><code>-1</code>
         * will already have been copied to destination array positions
         * <code>destPos</code> through
         * <code>destPos+</code><i>k</I><code>-1</code> and no other
         * positions of the destination array will have been modified.
         * (Because of the restrictions already itemized, this
         * paragraph effectively applies only to the situation where both
         * arrays have component types that are reference types.)
         * @param      src      the source array.
         * @param      srcPos   starting position in the source array.
         * @param      dest     the destination array.
         * @param      destPos  starting position in the destination data.
         * @param      length   the number of array elements to be copied.
         * @exception  IndexOutOfBoundsException  if copying would cause
         *               access of data outside array bounds.
         * @exception  ArrayStoreException  if an element in the <code>src</code>
         *               array could not be stored into the <code>dest</code> array
         *               because of a type mismatch.
         * @exception  NullPointerException if either <code>src</code> or
         *               <code>dest</code> is <code>null</code>.
        public static native void arraycopy(Object src,  int  srcPos,
                                            Object dest, int destPos,
                                            int length);

  • System.arraycopy for arrays of  2 dimensions?

    Can it be used for 2 dimensional arrays? How? My problem becomes more specific what the length parameter concerns.
    Thanks
    static void      arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

    That means, that if i want to create a deep copy of a 2dimensional array, i have to:
    -either create a new array, scan the first one and place the values in the new one
    -or do something like this:
        Object[][] gameBoardCopy()
            Object[][] cloneBoard=new Object[8][8];
            System.arraycopy(this.board, 0, cloneBoard, 0,  8);
            for (int i=0; i<8; i++)
                System.arraycopy(this.board, 0, cloneBoard[i],0, 8);
    What do you suggest?

  • Using System.arraycopy to copy an array into itself.

    I was wondering if there are any potential problems with using code similar to below?
    System.arraycopy(Global.queueUrgent, 1, Global.queueUrgent, 0, Global.queueUrgent.length-1); I have an array which works as a job queue. Another thread looks at this queue and then acts on it according to the data held in Global.queueUrgent[0]. Once this task has been accomplished, the first job is removed and the rest of the queue is brought forward one (and hence the code sample).
    I understand the risks of a race condition which might occur in my application and can prevent it as much as possible but I was wondering if there were any other concerns I should address. It's important that I use an array such as this for my project.
    I haven't tried to implement the code as of yet as I would like to hear your thoughts on it first.
    Regards,
    Robert (1BillionHex).

    user13702320 wrote:
    "If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array. "
    Yeah I understand that. I was just wondering if there was anything that I was missing.
    It says "as if" it is copied to a temporary array. Does it actually do this? If so and if I use a large array, would this have an impact on memory usage?It doesn't matter if it actually does it using a temporary array. The point is, it is safe to use the same array for src and dest. Note that java.util.ArrayList's 'remove' method uses exactly the code that you are using. I don't know about memory usage effects. If you do this manually, you do need to set Global.queueUrgent[Global.queueUrgent.length-1] to null to ensure that the objects will be able to be garbage-collected properly when it is time (see what ArrayList.remove does). If you don't, then if you had a 5-element array and remove all 5 elements, you will end up with 5 references to what was originally only in queueUrgent[4], and the object won't be eligible for garbage-collection.
    I did have the same question as Kayaman--why not use a real queue? There are several built-in classes that you can use, instead of using an array directly. You claimed that "It's important that I use an array such as this for my project.", but you didn't explain why you think you must use an array.

Maybe you are looking for

  • Exchange 2007 - unable to connect to smtp after severall hours

    Hi, We have a Windows SBS 2008 server with Exchange 2007 SP3. After severall hours we are unable to connect to the server with telnet on port 25 from outside. Sometimes a local telnet on the server still works but not always. The only solution is to

  • Final Cut loading and running extremely slow...

    Hi, I posted about this a few days ago but in my usual panic (work to do!) I didn't make it clear, basically, here's the problem... I recently had problems with FCP and generally all apps on my machine, when they loaded there was a slight pause when

  • When I save a Word document as a PDF file and open it, some of the boxes around the text don't show

    Hello, I currently have Abobe Reader X (10.1.2) on Windows 7 and I need to save a Word document currently in A3 as a PDF file to print in A4. For some documents I have no problem printing and all of the text, images, boxes, lines etc. all show up no

  • WEBI : Dos & Donts for SAP BI

    Hi All, I have my BEx Queries ready, While moving to WEBI what are the considerations need to take from SAP BI point of you & SAP BO point of view. Dos & donu2019tsu2026. for WEBI Best Practicesu2026. keeping view on Performance Thanks in advance.. K

  • Connection LinuxClient on Oracle8-NT

    We have a customer with an Oracle8 Server on NT. He wants to run his WebApplication with an Apagee Webserver with Linux on another host. 1) How should he connect to the NT-Database ? 2) Should an Oracle Client be installed on the Linux-Tier ? (And if