日期:2014-05-20  浏览次数:20597 次

新手请教一道编程题目,corejava的
给定如下数组:
String[] ls={"A","B","C","V","G","H","K"};
String[] ses={"B","G","K"};
要求参照数组ses中的字母顺序对数组ls进行优先排序,对没有在ses数组中出现的字母排在数组的最后,不用排序,最后输出排好顺序的数组ls.

------解决方案--------------------
典型的伸手党,下面的人不用回了!
------解决方案--------------------
“对没有在ses数组中出现的字母排在数组的最后,不用排序”

不用排序的用保持原顺序么? 如果不用的话。。。。

Java code

package test;

public class Test1 {

    static String[] ls={"A","B","C","V","G","H","K"};
    static String[] ses={"B","G","K"};

    /**
     * @param args
     */
    public static void main(String[] args) {

        int indexSes = 0;
        int indexLs = 0;
        while (indexSes <= ses.length - 1) {
            while (true) {
                int indexTemp = search(ses[indexSes], indexLs);
                if (indexTemp != -1 && indexTemp != indexLs) {
                    String tempS = ls[indexLs];
                    ls[indexLs] = ls[indexTemp];
                    ls[indexTemp] = tempS;
                }
                if (indexTemp != -1)
                    indexLs++;
                else
                    break;
            }

            indexSes++;
        }
        for (String string : ls) {
            System.out.print(string + " ");
        }

    }

    private static int search(String c, int startIndex) {
        for (int i = startIndex; i <= ls.length - 1; i++) {
            if (ls[i].equals(c))
                return i;
        }
        return -1;
    }

}

------解决方案--------------------
探讨

典型的伸手党,下面的人不用回了!