Posted by: Sourav | April 21, 2015

Making the array size double in Java


import java.io.*;
class pp{
public static void main(String args[]) throws Exception
{
//int size=10;
int arr1[]=new int[10];
int count=0;
while(count<12)
{

if(count==arr1.length)
{

arr1=resize(arr1);
}
InputStreamReader s=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(s);
arr1[count]=Integer.parseInt(br.readLine());

count++;

}
for(int i=0;i<arr1.length;i++)
System.out.println(arr1[i]);
}
 static int[] resize(int arr1[])
{
int temp[]=new int[2*arr1.length];
System.arraycopy(arr1,0,temp,0,10);
arr1=temp;
return arr1;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: