changing array dimension in a method

D

Daniel Malek

Dear All

I am new to java and appreciate your help with my question:

I have a two dimensional array (myArray) and need to change its dimensions
to different rows and columns depending on certain criteria . I have a
method that takes the array to reinitialize it to change its dimensions.
However , the original array dimensions can not be changed within the
method.


public static void main(String[] args) throws Exception {
int[][] myArray;
myArray=new int[1][4];
..........

for (i=0;i<4;i++)

{
changeDimMyArray(myArray);
.....
}

public void changeDimMyArray(int myArray2[][])

{
// Some operations to calculate n1 and n2.....
n1=newRow;
n2=newCol;

myArray2=new int[newRow][newCol];

}



Could you please advise how I can change the dim of array from inside the
method "changeDimMyArray"?


Thank you
 
S

Stefan Ram

Daniel Malek said:
I have a two dimensional array (myArray) and need to change its dimensions

You can not change the dimension of an array in the same
sense that suggest that you can not change the value of
the number 3.

You can, however, create a new array with another dimension
at run-time.

The following example code shows how to create and populate an
array, when the dimension becomes only known at run-time.

In this example, an array with 3 dimensions of sizes 4, 5, and
6, respectively, is created, filled, and printed.

public class Main
{
public static int[] cdr( final int[] list )
{ return java.util.Arrays.copyOfRange( list, 1, list.length ); }

public static java.lang.Object build( final int ... extensions )
{ final java.lang.Object array = java.lang.reflect.Array.newInstance
( java.lang.Integer.TYPE, extensions );
for( int i = 0; i < extensions[ 0 ]; ++i )if( extensions.length > 1 )
java.lang.reflect.Array.set( array, i, build( cdr( extensions )));
else java.lang.reflect.Array.setInt(( int[] )array, i, i );
return array; }

public static void print( final java.lang.Object array )
{ for( int i = 0; i < java.lang.reflect.Array.getLength( array ); ++i )
if( array.getClass().getName().startsWith( "[[" ))
print( java.lang.reflect.Array.get( array, i )); else
java.lang.System.out.print( java.lang.reflect.Array.getInt( array, i )); }

public static void main( final java.lang.String[] args )
{ print( build( 4, 5, 6 )); }}
 
S

Stefan Ram

final java.lang.Object array = java.lang.reflect.Array.newInstance

Less reflection magic is needed if an array of arbitrary dimension
is implemented as an array of one dimension, so that, for example,
to implement a two-dimensional array with extensions 2 and 3, a
one-dimensional array of size 2 · 3 is allocated and then the
access methods (with a varargs-parameter list) recalculate the
index values, like i = i0 · 2 + i1 for the example above.
 
T

Ted Hopp

The reason the array isn't changed is that object references are passed by
value. So when you assign a new array to the formal parameter myArray2
inside changeDimMyArray(), the array reference myArray in main() is not
affected at all. So you can't "change the dim of array from inside the
method". But you can easily do something nearly as good: change the
changeDimMyArray() method to return the redimensioned array. It might look
something like this:

public void changeDimMyArray(int myArray[][])
{
// figure out the desired new array sizes

myNewArray = new int[newRow][newCol];

// do more stuff, like copy values from myArray to myNewArray

return myNewArray;
}

Then, back in main, you can call it like this:

myArray = changeDimMyArray(myArray);

That will change the object to which myArray refers. Bear in mind that
unless you've assigned the original array reference to another variable, the
original array will be beyond reach of your program and headed for the
garbage collector.

Hope this helps,

Ted Hopp
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top