给两个排序好的数组,怎么求两个数组合并后的中位数
展开全部
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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));
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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));
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询