根据添加在一起的字母值对字符串进行排序。 (JAVA)

时间:2015-03-22 02:40:25

标签: java sorting

这是我在这里发表的第一篇文章,请耐心等待。我正在自学Java,试图为计算机科学课程做准备。我打算明年参加考试,但我遇到了一个问题。

问题是我要将值分配给a = 1, b = 2...z = 26的小写字母。然后我将编写一个方法,给出一个字符串数组String[] names将根据哪个数组的最大字母值加在一起来对它们进行排序。例如,“ed”的值为9,因为e为5,d为4.

另一个例子,给定annie, bonnie, liz,该方法将排序为bonnie, liz, annie

我对如何在代码方面解决这个问题感到困惑,并且正在寻求一些帮助。提前谢谢!

2 个答案:

答案 0 :(得分:1)

我不会为你解决确切的问题,因为你通过尝试自己来学习更多...但是你可能会采取这种灵感

public class Sorting {
    public static Map<Character, Integer> characters = new HashMap<Character, Integer>();
    static{
        characters.put('a', 1);
        characters.put('b',2);
    }

    static class StringValue implements Comparable<StringValue> {
        private Integer value = 0;
        private String name = "";
        public StringValue(String name){
            this.name = name;
            for(char aChar : name.toCharArray()){
                value += Sorting.characters.get(aChar);
            }
        }
        public int compareTo(StringValue o) {
            return value.compareTo(o.value);
        }
        public String getName() {
            return name;
        }
        public Integer getValue() {
            return value;
        }
    }

    public static void main(String [] args){
        String [] values = {"bb", "a", "aa" , "abaa"};
        List<StringValue> stringVals = new ArrayList<StringValue>();
        for(String val : values){
            stringVals.add(new StringValue(val));
        }
        Collections.sort(stringVals);
        for(int i = 1; i <= stringVals.size(); i++){
            StringValue aVal = stringVals.get(i-1);
            System.out.println(aVal.getName() + " has value " + aVal.getValue() + " and rank " + i);
        }
    }
}

输出:

a has value 1 and rank 1
aa has value 2 and rank 2
bb has value 4 and rank 3
abaa has value 5 and rank 4

答案 1 :(得分:0)

您可以使用类似的代码。

public class J2 {

    public static void main(String[] args) {

        J2 j = new J2();

        String s[] = {"annie", "bonnie", "liz"};
        j.sortArray(s);

    }

    void sortArray(String[] arr2) {
        int ww[] = new int[arr2.length];

        //find the integer value total of words and put in to array
        for (int i = 0; i < arr2.length; i++) {
            String s = arr2[i];
            char[] c = s.toCharArray();
            int w = 0;
            for (int j = 0; j < s.length(); j++) {
                w += (int) c[j] - 96;
            }
            ww[i] = w;
        }

        // sort the array acordind to values
        for (int i = 0; i < arr2.length; i++) {
            for (int j = 0; j < arr2.length - 1; j++) {
                if (ww[j] < ww[j + 1]) {
                    int t = ww[j];
                    ww[j] = ww[j + 1];
                    ww[j + 1] = t;

                    String s = arr2[j];
                    arr2[j] = arr2[j + 1];
                    arr2[j + 1] = s;
                }
            }
        }

        //print the array 
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }

    }

}