java实现合并排序,且输出每次合并的两个数组段,及合并结果

如图

第1个回答  2018-04-08
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)); }}本回答被网友采纳
相似回答