«

»

Nov 22

Merging and Sorting two arrays using Java

This is the question asked by Genesis on Nov 20, 2012

[code language=”java”]

<import java.util.Arrays;

public class MergingTwoArrays {

public static void main(String[] args)
{
int[] array1 = new int[3];
array1[0] = 1;
array1[1] = 38;
array1[2] = 34;

int[] array2 = new int[3];
array2[0] = 17;
array2[1] = 23;
array2[2] = 2;

int[] array3 = new int[array1.length+array2.length];

int CurrentPosition = 0;

for(int i=0;i<3;i++)
{
array3[CurrentPosition]=array1[i];
CurrentPosition++;
}

for(int j=0;j<3;j++)
{
array3[CurrentPosition]=array2[j];
CurrentPosition++;
}

Arrays.sort(array3,0,6);
for(int k=0;k<array3.length;k++)
{
System.out.println(array3[k]);
}

}
}

[/code]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>