2016-04-17 31 views
0

我有排序的單詞列表:根據人物的名單在Java中

List<String> alphabet; (contains 26 unique characters as elements, for example 
qwertyuiosapdfghjklzcxvbnm) 

List<String> wordsToArrange; contains words as elements, for example: 

- apple 
- stream 
- posthouse 
- sea 
- seed 

我需要根據我所做的字母來排列的話。

我現在的做法是用循環。

alphabet(i) compare it with all the words charAt(0) 
if only 1 is found i put it to a new list arrangedList 

but if 2 is found i go alphabet(i+1) till the letter is found and now i can put them in a right order to arrangedList.... 

then move back to alphabet(i+1) till alphabet(26) and now all should be arranged correctly... 

我已經寫了一些基地,爲這個代碼,但我要問什麼是其他的辦法之前,我開始認真「爲cyclying」。

謝謝!

後續 Changing specific characters in List of Strings in Java

回答

0

我想:

  1. 地圖,你的話每個字母進行排序到您的字母「逆」。例如:
    • 'a'是串的第11個字母,因此將其映射到'k'
    • 'b'是字符串的24個字母,因此將其映射到'x'
  2. 使用排序列表Collections.sort
  3. 將排序後的單詞中的每個字母映射回其原始字母,例如'k' -> 'a'; 'x' -> 'b'
+0

如果我理解正確的話,那就只爲「qwertyuiosapdfghjklzcxvbnm」的工作,但我需要它爲每一個組合的編輯工作:但如果我現在想更深入地瞭解它,然後這將如果用字母作爲指標而努力映射到字母 – JavaJuniorSoon2bSenior

+0

「例如」。 –

+0

謝謝!對不起,英語是我的第二語言,是的,這是可行的。 :) – JavaJuniorSoon2bSenior

0

我會使用Java 8的過濾器和流過濾出以特定字符開頭的那些。然後我會對結果進行排序。如果它需要在一個數組中,那麼結合結果。

import java.util.Arrays; 
/** 
* Created by Brandon on 2016-04-17. 
*/ 
public class Main { 
    public static void main(String[] args) { 

     String[] array = new String[]{"apple", "stream", "posthouse", "sea", "seed"}; 
     //char[] indices = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; 
     char[] indices2 = new char[]{'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'}; 

     for (char key : indices2) { 
      String[] result = Arrays.stream(array).filter(value -> value.charAt(0) == key).toArray(length -> new String[length]); 

      if (result.length > 0) { 
       Arrays.sort(result); 
       System.out.println(Arrays.toString(result)); 
      } 
     } 
    } 
} 

如果使用indices,那麼結果是:

[apple] 
[posthouse] 
[sea, seed, stream] 

如果使用indices2,那麼結果是:

[posthouse] 
[apple] 
[sea, seed, stream] 
+0

這看起來很有趣,謝謝! – JavaJuniorSoon2bSenior

0

什麼包裹串在一個新的類,它實現可比?

可能是我沒有測試過的一些邊緣案例錯誤。

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

public class CompString { 

    public static void main(String[] args) { 
     List<ComparableString> list = new ArrayList<ComparableString>(); 

     list.add(new ComparableString("apple")); 
     list.add(new ComparableString("stream")); 
     list.add(new ComparableString("posthouse")); 
     list.add(new ComparableString("sea")); 
     list.add(new ComparableString("seed")); 

     Collections.sort(list); 

     System.out.println(list); 
    } 

} 

class ComparableString implements Comparable<ComparableString> { 

    String str; 
    static String sortOrder = "qwertyuiosapdfghjklzcxvbnm"; 

    public ComparableString(String string) { 
     str = string; 
    } 

    @Override 
    public String toString() { 
     return str; 
    } 

    @Override 
    public int compareTo(ComparableString other) { 
     for (int i = 0; i < Math.min(this.str.length(), other.str.length()); i++) { 
      int thisOrder = ComparableString.sortOrder.indexOf(this.str.charAt(i)); 
      int thatOrder = ComparableString.sortOrder.indexOf(other.str.charAt(i)); 

      int order = thisOrder - thatOrder; 
      if (order != 0) { 
       return order; 
      } 

     } 

     if (this.str.length() > other.str.length()) { 
      return -1; 
     } else if (this.str.length() < other.str.length()) { 
      return 1; 
     } 
     return 0; 
    } 
}