有两个数组a1和a2,都是有序的,现在要求将两个数组合并为一个有序的数组。(可能会有很多种情况,两个都是升序,一个升序一个降序...)请各位大哥教教小弟。小弟先谢谢了!

解决方案 »

  1.   


        public static Object[] heBin(Object[] objs1, Object[] objs2)
        {
            List<Object> list = new ArrayList<Object>();
            
            for(Object obj: objs1)
            {
                list.add(obj);
            }
            
            for(Object obj: objs2)
            {
                list.add(obj);
            }
            
            Object[] ret = list.toArray();
            Arrays.sort(ret);
            
            return ret;
        }
      

  2.   

    测试代码
        public static void main(String args[])
        {
            String[] a1 = {"01", "03"};
            String[] a2 = {"00", "05"};
            
            for(Object obj: heBin(a1, a2))
            {
                System.out.println(obj);
            }
        }
    结果
    00
    01
    03
    05
      

  3.   


    import java.util.TreeSet;public class Tester { public static void main(String[] args) {
    TreeSet<String> ts = new TreeSet<String>();
    String[] array1 = {"1", "2", "3"};
    String[] array2 = {"2", "3", "4"};
    for (String s : array1) ts.add(s);
    for (String s : array2) ts.add(s);
    String[] r = ts.toArray(new String[ts.size()]);
    for (String s : r) System.out.println(s);
    }}
      

  4.   

    直接合并,然后再重新排序
    JAVA资料太多?选中想收藏的文字(图片),右键选“收录到易载”,搞定!