English 中文(简体)
我是否真知什么?
原标题:Ordering a string alphabetically - did I miss something obvious?
public class Anagram {
    public static void main(String[] args) {

        String a = "Despera tion-".toLowerCase();
        String b = "A Rope Ends It".toLowerCase();

        String aSorted = sortStringAlphabetically(a);
        String bSorted = sortStringAlphabetically(b);

        if(aSorted.equals(bSorted)){
            System.out.println("Anagram Found!");
        }else{
            System.out.println("No anagram was found");
        }

    }

    public static String sortStringAlphabetically(String s) {

        char[] ca = s.toCharArray();
        int cnt = 0;
        ArrayList al = new ArrayList();

        for (int i = 0; i < ca.length; i++) {
            if (Character.isLetter(ca[cnt])) 
                al.add(ca[cnt]);

            cnt++;
        }

        Collections.sort(al);
        return al.toString();
    }
}

作为一名学习者,我打上了这台蓝色分析器。 我所选择的解决办法是,采用一种分类法似乎太过了分类法——和;果园。 -> ArrayList ->String——鉴于我确实想比较两条插图,以检验一个短语是否是另一个短语的图象——我是否能够用较少的分类来做?

ps The tutors solution was a mile away from my attempt, and probably much better for a lot of reasons - but I am really trying to get a handle on all the different Collection types.

http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/doc/src-html/de/hska/java/exercises/arrays/Anagram.html#line.18

EDIT

在这里,公平与和解委员会是最初的挑战,我相信,我放弃了解决办法。

http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/arrays.html

我最初的kneejerk的反应是,仅仅通过阵列开展工作,打碎与阵列相匹配的果园,但这似乎要求我重建每一处的阵列——许多人感谢你们为教育我所做的一切努力。

最佳回答

There are different ways to improve this, if you go with this algorithm. First, you don t necessarily need to create a character array. You can use String.charAt() to access a specific character of your string.

Second, you don t need a list. If you used a SortedMultiSet or a SortedBag, you could just add things in sorted order. If you write a function that creates the SortedMultiSet from your string, you could just compare the sets without rebuilding the string.

Note: I don t know what libraries you re allowed to use (Google and Apache have these types), but you can always brew your own .

此外,确保使用通用物。 仅仅界定ArrayList是极危险的,IMHO。

问题回答

如果不使用一个清单,你就只能对扼杀进行分类:

public static String sortStringAlphabetically(String s) {
    String lettersOnly = s.replaceAll("\W", "");
    char[] chars = lettersOnly.toCharArray();
    Arrays.sort(chars);
    return new String(chars);
}

N.B. I haven t 实际上是试图操作该守则。

Your algorithm, but shorter (and yet, slower). The "type-juggling" is done "implicitly" in Java s various library classes:

public static boolean isAnagram(String a, String b) {
    List<String> listA = new ArrayList<String>(Arrays.asList(
      a.toLowerCase().replaceAll("\W", "").split("")));
    List<String> listB = new ArrayList<String>(Arrays.asList(
      b.toLowerCase().replaceAll("\W", "").split("")));

    Collections.sort(listA);
    Collections.sort(listB);

    return listA.equals(listB);
}

选择性地取代<代码>W的定期表述,以排除你不想为图表而考虑的那些信件。

public class Anagram {
    public static void main(String[] args) throws Exception {
        String s1 = "Despera tion-";
        String s2 = "A Rope Ends It";
        anagramCheck(s1, s2);
    }

    private static void anagramCheck(String s1, String s2) {
        if (isAnagram(s1, s2)) {
            System.out.println("Anagram Found!");
        } else {
            System.out.println("No anagram was found");
        }
    }

    private static boolean isAnagram(String s1, String s2) {
        return sort(s1).equals(sort(s2));
    }

    private static String sort(String s) {
        char[] array = s.replaceAll("\W", "").toLowerCase().toCharArray();
        Arrays.sort(array);
        return new String(array);
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签