import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* Declare two arrays of 5 nos each. the third array should contain 10 nos.
* Populate the first two arrays. transfer the contents of the first array
* into the odd positions of the third array and the contents of the second array
* to the even positions of the third array
*/
public class ArrayPopulation
{
/**
* displays the content of two arrays and resultant array in which
* the resultant array stores the content of first array in odd position and
* content of second array in even posion.
* @throws Exception
*/
public static void arrayPopulation() throws Exception
{
// source arrays
final int[] intFirstArray={1,3,5,7,9};
final int[] intSecondArray={0,2,4,6,8};
// resultant array
final int[] intResultArray=new int[10];
// initialize each element of resultArray with 0
for(int i=0;i<intFirstArray.length;i++)
{
intResultArray[(i*2)]=intSecondArray[i];
intResultArray[(i*2)+1]=intFirstArray[i];
}
for(int k=0;k<3;k++)
{
if(k==0)
System.out.println("\nContent of first array is : ");
else if(k==1)
System.out.println("\nContent of second array is : ");
else if(k==2)
System.out.println("\nContent of resultant array is : ");
for(int i=0;i<intFirstArray.length;i++)
{
switch(k)
{
case 0:
System.out.println("firstArray[" + i +"] = " + intFirstArray[i]);
break;
case 1:
System.out.println("secondArray[" + i +"] = " + intSecondArray[i]);
break;
case 2:
System.out.println("resultArray[" + (i*2) +"] = " + intResultArray[(i*2)]);
System.out.println("resultArray[" + ((i*2)+1) +"] = " + intResultArray[(i*2)+1]);
break;
}
}
}
}
/**
* excecution begin from here
* @param args
*/
public static void main(String[] args)
{
try
{
arrayPopulation();
}
catch(Exception e)
{
System.out.println("Error occured!!");
}
}
}
No comments:
Post a Comment