编写JAVA程序,实现两个数组的合并,并按升序排列合并后的数组

假定现有两个数组,分别为:
int[ ] arr1={3, 1, 23};
int[ ] arr2={27, 7, 2};
设计程序,将两个数组合并成一个数组,并按升序排列合并后的数组,输出合并前和合并后的数组信息

package test;

import java.util.Arrays;
import java.util.Comparator;

public class JButtonTest
{
public static void main ( String[] args )
{
int[] arr1 = { 3, 1, 23 };
int[] arr2 = { 27, 7, 2 };
String temp = Arrays.toString (arr1) + Arrays.toString (arr2);
temp = temp.replaceAll ("\\]\\[", ",").replaceAll ("\\s", "").replaceAll ("[\\[\\]]", "");
String[] result = temp.split ("\\,");
System.out.println (Arrays.toString (result));
Arrays.sort (result, new Comparator<String> ()
{
@Override
public int compare ( String o1, String o2 )
{
int a = Integer.parseInt (o1), b = Integer.parseInt (o2);
if (a > b)
{
return 1;
}
else if (a < b)
{
return -1;
}
else
{
return 0;
}
}
});
System.out.println (Arrays.toString (result));
}
}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答