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 / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Denis Palas - 11 Nov 2006 08:33 GMT
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 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
hiwa - 11 Nov 2006 09:22 GMT
> Hi everybody
>
[quoted text clipped - 116 lines]
> 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[i], 0, tempArray[i], 0,
solArray3[i].length);
}


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



©2008 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.