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

Discussion in 'Java' started by Denis Palas, Nov 11, 2006.

  1. Denis Palas

    Denis Palas Guest

    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[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[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 Class




    I 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
     
    Denis Palas, Nov 11, 2006
    #1
    1. Advertising

  2. Denis Palas

    hiwa Guest

    Denis Palas wrote:
    > 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[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[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 Class
    >
    >
    >
    >
    > I 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

    There are unknown elements in your current code and your requiremen for
    the application is also unclear. However, usage of System.arrayCopy()
    is simple:

    for (int i = 0; i < tempArray.length; ++i){
    System.arrayCopy(solArray3, 0, tempArray, 0,
    solArray3.length);
    }
     
    hiwa, Nov 11, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Shepherd

    System.arraycopy speed

    Rob Shepherd, Jun 16, 2004, in forum: Java
    Replies:
    9
    Views:
    7,864
    Yu SONG
    Jun 17, 2004
  2. -
    Replies:
    6
    Views:
    743
  3. mei
    Replies:
    13
    Views:
    3,519
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Mar 4, 2007
  4. Jordan
    Replies:
    1
    Views:
    315
  5. Florian Kaufmann
    Replies:
    1
    Views:
    380
Loading...

Share This Page