dcsimg
Reassigning size of an array
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, September 26, 2002 11:14 PM

Lets say i have declared an array as

public static int[] num = new int[];

is there anyway to change the size of the array in another method in the same class?

Re: Reassigning size of an array

Posted By:   Ganesh_Venkatraman  
Posted On:   Thursday, September 26, 2002 11:46 PM

I suppose it should be possible if you know the size you want to increment before hand as shown from the example below. If you do not know the size before hand it is better to use structures like ArrayList or Vector. Hope this answers your query



public class Test {



static int num[] = new int[8];



public static void main(String[] args)
{


int i = 0;



System.out.println("length before change is :" +num.length);



for(i = 0;i < num.length;i++)
{


num[i] = i;


System.out.println("element is:" +num[i]); }



testing();




System.out.println("after length is :" +num.length);




for(int j = i; j < num.length;j++){


num[j] = j;


System.out.println("element is:" +num[j]);
}


}



public static void testing() {


num = new int[20];


}


}

About | Sitemap | Contact