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?

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.

  • Auto-indexing is slow for arrays with 1 dimensions

    Hi,
    I was looking at the performance of operations on all individual elements in 3D arrays, especially the difference between auto-indexing (left image) and manual-indexing (right image, calling "Index array" and "Replace array subset" in the innermost loop). I'm a bit puzzled by the results and post it here for discussion and hopefully somebody's benefit in the future.
    Left: auto-indexing; right: manual-indexing
    In the tests I added a different random number to all individual elements in a 3D array. I found that auto-indexing is 1.2 to 1.5 times slower than manual-indexing. I also found that the performance of auto-indexing is much more dependent on the size the dimensions: an array with 1000x200x40 elements is 20% slower than an array with 1000x40x200 elements. For manual-indexing there is hardly any difference. The detailed results can be found at the end of this post.
    I was under the impression that auto-indexing was the preferred method for this kind of operation: it achieves exactly the same result and it is much clearer in the block diagram. I also expected that auto-indexing would have been optimized in LabView, but the the tests show this is clearly not the case.
    What is auto-indexing doing?
    With two tests I looked at how auto-index works.
    First, I looked if auto-indexing reorganizes the data in an inefficient way. To do this I made a method "fake-auto-indexing" which calls "Array subset" and "Reshape array" (or "Index array" for a 1D-array) when it enters _every_ loop and calls "Replace array subset" when exiting _every_ loop (as opposed to manual-indexing, where I do this only in the inner loop). I replicated this in a test (calling it fake-auto-indexing) and found that the performance is very similar to auto-indexing, especially looking at the trend for the different array lengths.
    Fake-auto-indexing
    Second, I looked if Locality of reference (how the 3D array is stored in memory and how efficiently you can iterate over that) may be an issue. Auto-indexing loops over pages-rows-columns (i.e. pages in the outer for-loop, rows in the middle for-loop, columns in the inner for-loop). This can't be changed for auto-indexing, but I did change it for manual and fake-indexing. The performance of manual-indexing is now similar to auto-indexing, except for two cases that I can't explain. Fake-auto-indexing performs way worse in all cases.
    It seems that the performance problem with auto-indexing is due to inefficient data organization.
    Other tests
    I did the same tests for 1D and 2D arrays. For 1D arrays the three methods perform identical. For 2D arrays auto-indexing is 15% slower than manual-indexing, while fake-auto-indexing is 8% slower than manual-indexing. I find it interesting that auto-indexing is the slowest of the three methods.
    Finally, I tested the performance of operating on entire columns (instead of every single element) of a 3D array. In all cases it is a lot faster than iterating over individual elements. Auto-indexing is more than 1.8 to 3.4 times slower than manual-indexing, while fake-auto-indexing is about 1.5 to 2.7 times slower. Because of the number of iterations that has to be done, the effect of the size of the column is important: an array with 1000x200x40 elements is in all cases much slower than an array with 1000x40x200 elements.
    Discussion & conclusions
    In all the cases I tested, auto-indexing is significantly slower than manual-indexing. Because auto-indexing is the standard way of indexing arrays in LabView I expected the method to be highly optimized. Judging by the tests I did, that is not the case. I find this puzzling.
    Does anybody know any best practices when it comes to working with >1D arrays? It seems there is a lack of documentation about the performance, surprising given the significant differences I found.
    It is of course possible that I made mistakes. I tried to keep the code as straightforward as possible to minimize that risk. Still, I hope somebody can double-check the work I did.
    Results
    I ran the tests on a computer with a i5-4570 @ 3.20 GHz processor (4 cores, but only 1 is used), 8 GB RAM running Windows 7 64-bit and LabView 2013 32-bit. The tests were averaged 10 times. The results are in milliseconds.
    3D-arrays, iterate pages-rows-columns
    pages x rows x cols : auto    manual  fake
       40 x  200 x 1000 : 268.9   202.0   268.8
       40 x 1000 x  200 : 276.9   204.1   263.8
      200 x   40 x 1000 : 264.6   202.8   260.6
      200 x 1000 x   40 : 306.9   205.0   300.0
     1000 x   40 x  200 : 253.7   203.1   259.3
     1000 x  200 x   40 : 307.2   206.2   288.5
      100 x  100 x  100 :  36.2    25.7    33.9
    3D-arrays, iterate columns-rows-pages
    pages x rows x cols : manual  fake
       40 x  200 x 1000 : 277.6   457       
       40 x 1000 x  200 : 291.6   461.5
      200 x   40 x 1000 : 277.4   431.9
      200 x 1000 x   40 : 452.5   572.1
     1000 x   40 x  200 : 298.1   460.4     
     1000 x  200 x   40 : 460.5   583.8
      100 x  100 x  100 :  31.7    51.9
    2D-arrays, iterate rows-columns
    rows  x cols  : auto     manual   fake
      200 x 20000 :  123.5    106.1    113.2    
    20000 x   200 :  119.5    106.1    115.0    
    10000 x 10000 : 3080.25  2659.65  2835.1
    1D-arrays, iterate over columns
    cols   : auto  manual  fake
    500000 : 11.5  11.8    11.6
    3D-arrays, iterate pages-rows, operate on columns
    pages x rows x cols : auto    manual  fake
       40 x  200 x 1000 :  83.9   23.3     62.9
       40 x 1000 x  200 :  89.6   31.9     69.0     
      200 x   40 x 1000 :  74.3   27.6     62.2
      200 x 1000 x   40 : 135.6   76.2    107.1
     1000 x   40 x  200 :  75.3   31.2     68.6
     1000 x  200 x   40 : 133.6   71.7    108.7     
      100 x  100 x  100 :  13.0    5.4      9.9
    VI's
    I attached the VI's I used for testing. "ND_add_random_number.vi" (where N is 1, 2 or 3) is where all the action happens, taking a selector with a method and an array with the N dimensions as input. It returns the result and time in milliseconds. Using "ND_add_random_number_automated_test.vi" I run this for a few different situations (auto/manual/fake-indexing, interchanging the dimensions). The VI's starting with "3D_locality_" are used for the locality tests. The VI's starting with "3D_norows_" are used for the iterations over pages and columns only.
    Attachments:
    3D_arrays_2.zip ‏222 KB

    Robert,
    the copy-thing is not specific for auto-indexing. It is common for all tunnels. A tunnel is first of all a unique data space.
    This sounds hard, but there is an optimization in the compiler trying to reduce the number of copies the VI will create. This optimization is called "in-placeness".
    The in-placeness algorithm checks, if the wire passing data to the is connected to anything else ("branch"). If there is no other connection then the tunnel, chance is high that the tunnel won't create an additional copy.
    Speaking of loops, tunnels always copies. The in-placeness algorithm cannot opt that out.
    You can do a small test to elaborate on this: Simply wire "0" (or anything less than the array sizy of the input array) to the 'N' terminal.......
    Norbert
    PS: Auto-indexing is perfect for a "by element" operation of analysis without modification of the element in the array. If you want to modify values, use shift registers and Replace Array Subset.
    CEO: What exactly is stopping us from doing this?
    Expert: Geometry
    Marketing Manager: Just ignore it.

  • 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

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

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

  • 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 timing

    I ran the following test to determine the relative efficiency of moving int and byte array members. It was run on a Windows XP Pro machine @ 2GHz.
    The duke is for the first person that correctly predicts the output. Actually running the code is definitely cheating.
    In an early version I had more complexity just to be sure the compilers didn't notice that the code was only overwriting some zeros with other zeros. They weren't that smart. A million array elements actually get shifted here.
    public class test {
        static final int K = 1024;
        static int tsize = K * K;
        static int[] ints = new int[ tsize ];
        static byte[] bytes = new byte[ tsize ];
        public static void main( String[] args ) {
            long start, stop;
            start = System.currentTimeMillis();
            System.arraycopy( ints, 0, ints, 1, tsize-1 );
            stop = System.currentTimeMillis();
            System.out.println("ints: " + (stop-start) );
            start = System.currentTimeMillis();
            System.arraycopy( bytes, 0, bytes, 1, tsize-1 );
            stop = System.currentTimeMillis();
            System.out.println("bytes: " + (stop-start) );
            System.exit( 0 );
        } // end of main()
    } // end of test

    I ran the following test to determine the relative
    efficiency of moving int and byte array members. It
    was run on a Windows XP Pro machine @ 2GHz.
    The duke is for the first person that correctly
    predicts the output. Actually running the code is
    definitely cheating.Well, I cheated. Does that make me a bad person? Anyway, my guess was that whichever test run first would be slightly slower, no matter if it was bytes or ints. I was wrong.
    >
    In an early version I had more complexity just to be
    sure the compilers didn't notice that the code was
    only overwriting some zeros with other zeros. They
    weren't that smart. A million array elements actually
    get shifted here.
    public class test {
    static final int K = 1024;
    static int tsize = K * K;
    static int[] ints = new int[ tsize ];
    static byte[] bytes = new byte[ tsize ];
    public static void main( String[] args ) {
    long start, stop;
    start = System.currentTimeMillis();
    System.arraycopy( ints, 0, ints, 1, tsize-1
    tsize-1 );
    stop = System.currentTimeMillis();
    System.out.println("ints: " + (stop-start) );
    start = System.currentTimeMillis();
    System.arraycopy( bytes, 0, bytes, 1, tsize-1
    tsize-1 );
    stop = System.currentTimeMillis();
    System.out.println("bytes: " + (stop-start)
    -start) );
    System.exit( 0 );
    } // end of main()
    } // end of test

  • 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

  • System.arraycopy() vs System.arraymove()

    I assume that there is a function arraycopy() in the class java.lang.System. This is a native and efficient function. I do think that behind this navitve function there is a C memcpy() function call.
    So with the same idea why there is no System.arraymove() inside the J2SE 5.0 ? And behind this native function we should place a C memmove() function call.
    I hope (if it is usefull and relevant) this will be available in JDK 1.5.1....
    Best regards.

    System.arraycopy() is already done as a memmove(), not as a memcpy():
    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.So there's no need for an arraymove().

  • Need help for array counting

    Any help for array question
    Hello to All:
    I want to tally or count some of the elements that I have in array but not sure how.
    I have for example: int myArray[] = {90,93,80,81,71,72,73,74};My objective is to tally all of the 90's, tally all of the 80's and tally all of the 70's.
    So, the result that I want to have would look something like the following:
    System.out.println ("The total tally number of 90's is " 2 );
    System.out.println ("The total tally number of 80's is " 2 );
    System.out.println ("The total tally number of 70's is " 4 );I do not want to add these numbers, just want to count them.
    Also I want to use a "forloop" to achieve the result intead of just declaring it at 2 or 4 etc..
    Any help Thankyou

    [u]First , This is not exactly what I have to
    do for homework. There is a lot more, a lot more
    involved with the program that I am working on.
    Second, this is an example, an example, an
    example of something that I need to achieve.
    Third, you are asking for a code, to me that
    sounds as if your asking for homework. Fourth,
    I did not ask for any rude comments. Fith, in
    the future please do not reply to my messages at ALL
    if you can not help!!!!
    Sixth, We did not ask for lazy goofs to post here.
    Seventh, In the future please do not post here. Take a hike - there's the virtual door.

  • How to implement mapping for a slowly changing dimension

    Hello,
    I don't have any experience with OWB and I need some help.
    I just don't know how to create the ETL process for a slowly changing dimension.
    My scenario is that I have 2 operative systems providing customer information, a staging area and a dwh with a customer dimension with SCD type 2 (created within OWB).
    The oltp data is already transferred to the staging area. But how should the mapping for the dwh table look like? Which operators have to be used?
    I have to check whether the customer record is new or just updated. How can I check every attribute? A new record shall be loaded, an updated record shall be historized (as I configured it in the SCD type 2). I just don't know how the trigger of the SCD is activated. Do I have to try an update on the trigger attribute and then automaticalle a new record is created? But with which operator can I do this? How should the mapping look like? Or is this impossible and do I have to implement this functionality with SQL code only?
    I know how to implement this with SQL code, but my task is to implement this in OWB.
    As you see I did not understand the logic of OWB so far and I hope somebody can help me.
    Greetings,
    Joerg

    Joerg,
    Check the blog below which provides good detail and also check the OWB documentation
    http://www.rittmanmead.com/2006/09/21/working-through-some-scd-2-and-3-examples-using-owb10gr2/
    Thanks,
    Sam.

  • Is it possible to make a 2D array (or whatever-dimension) array like..

    Is it possible to make a 2D array (or whatever-dimension) array like this...
            collumn 1                   collumn 2
    Emulated Address   Real Memory address*
               (int)                            (int *)
    +----------------------+----------------------------+
    |            0                |              0xA0               | <-- row 1
    |            1                |              0xA1               | <-- row 2
    |            2                |              0xA2               | <-- row 3
    +----------------------+----------------------------+
    * A = Address.
    is it possible to make an array like that?
    if it is, please tell me how to do it...
    thanks.
    ... I'm trying to make an emulator to emulate the simplest memory.

    Given your other posts, I'm assuming you mean in C, right?
    If so, the answer is yes, but specifically how will depend on a needed clarification of your question.  What you present doesn't really need to be a 2 dimensional array, just one: that looks like a simple list of memory addresses, right?
    At the simplest you can declare an array with two dimensions `iny myarray[2][3];` but to make the table you put up there you'd only need `int *myarray[3];`
    If you also wanted space allocated somewhere that each element of that list pointed to, you could allocate them separately:
    int *myarray[3];
    myarray[0] = (int *) malloc(sizeof(int));
    myarray[1] = (int *) malloc(sizeof(int));
    myarray[2] = (int *) malloc(sizeof(int));
    Obviously with many entries this should be in a loop.  Perhaps not as obviously, why would you not just malloc a larger block of memory to start with?
    What is the end goal?
    EDIT: actually, upon rereading your question, the mallocs are probably irrelevant.  `int *myarray[3]` will get you the array you are looking for.  Just realize that until you point those pointers to memory you 'own' you shouldn't assign to or read from them.
    Last edited by Trilby (2013-04-19 10:06:31)

Maybe you are looking for

  • Pressure on T430 Lid Causes Right Click

    Hi, I exclusively use my T430 in a docking station and have been plagued with phantom right-clicking. At first I just thought, "Sticky Mouse" but the problem persisted even with a new one. Today however, I discovered that i can activate right click j

  • Camera Calibration in ACR & DNG Profile Editor

    I am having the hardest time figuring out how to use the camera profiles  generated with DNG Profile Editor in Camera Raw. I am using Photoshop CS4 in Windows 7. I have generated the .dcp file  with DNG Profile Editor and saved it in the folder: Prog

  • Examples on Web Dynpro Java ? For more practice purpose

    Hi EP experts     I want more examples on Web Dynpro Java for practice purpose     My project next phase is on full  webdynpro     Pl...provide me more worked examples but in SDN i found very few regards kiran LVS

  • Is using backup device better when backing up the database?

    Hello Community     I used sp_addumpdevice to perform a backup and restore.     Is there an advantage to using sp_addumpdevice when doing backups and restores or is it better to just issue the Backup Database command and specify to disk?     Thank yo

  • Absolutely USELESS Customer-LESS Service-LESS

    Absolutely USELESS Customer-LESS Service-LESS We have 10 devices with AT&T wireless and when my wife (authorized on the acct.) called to make changes - It was all DOWN HILL - 45 Min. & 4 people later, She was at the same place she began.  Everyone wa