2017-03-16 95 views
0

好吧,所以我不知道如何去從一堆字符串數組創建一個字符串。 我有這樣的方法,它在3個字符串數組將字符串數組與字符串結合的方法

public static String formatStory(String[] shortest, String[] medium, String[] longest){ 

    return formattedStory; 
} 

我想需要很長的話,中字和一個短字,並把它添加到字符串,然後重複循環,以做一遍所有直到字符串數組中沒有剩餘字詞。我是否會使用while循環,因爲我不知道需要進行多少次迭代?還是for循環?

+0

這是什麼語言?我在猜測C#?你應該用一種語言來標記你的問題,以便相應的專家能夠迴應它。另外,除了只寫這個方法的簽名外,你還試過做什麼?你有沒有試圖解決它? –

回答

0

雖然使用,但要確保在每次迭代中都有3個字符串值。 這是一個非常高的內存和cpu使用函數,可能你應該考慮如何選擇它。

我希望有幫助。

+0

在'while()'和'for()'循環之間java沒有區別。字節碼是相同的:http://stackoverflow.com/a/8880898/4985572 – Igoranze

0

您也可以使用for循環。

String s = ""; 
for (int i = 0; i < shortest.length; i++) { 
    s = s + longest[i] + medium[i] + shortest[i]; 
} 
return s; 
+0

我們寫一個大寫'S'的字符串,我希望它有一個錯字... – NewBie1234

+0

是的,這是一個錯字。感謝編輯 – Adeel

-1

您可以使用for-loop或while循環,因爲兩個循環都會採用與數組長度相同的條件。只有三個字符串數組的大小相同時,一個循環才能工作,否則必須以不同的方式編寫它以獲得最佳性能。 也可以使用StringBuilder而不是串聯來獲得更好的內存性能。

+0

Java在while()和for()循環之間沒有區別。字節碼是相同的:http://stackoverflow.com/a/8880898/4985572。通常使用JDK 1.6及以上版本,編譯器將使用StringBuilder自動將字符串連接在一起。但是在循環中使用'StringBuilder'確實更好! http://stackoverflow.com/a/14927864/4985572 – Igoranze

0

這不是那麼簡單,我不確定它只能用一個循環來解決。對於我來說,它需要兩個函數和Java相當於foreach循環來解決任務。另外,我建議使用列表。 首先,您需要按位置將三個數組中的單詞分組。 第二,結果字符串必須連接成一個。 檢查下面的代碼。

import java.util.*; 

/*Some code...*/ 

private static void insertArrayIntoList(List<String> items, String[] array) { 
    if(array == null) return; 
    int count = 0; 
    for(String item : array) { 
     if(items.size() > count) { 
      String temp = items.get(count); 
      temp += " " + item;   
      items.set(count, temp); 
     } else { 
      items.add(item); 
     } 
     count++; 
    } 
} 
public static String formatStory(String[] shortest, String[] medium, String[] longest){ 

    List<String> items = new ArrayList<String>(); 

    insertArrayIntoList(items, shortest); 
    insertArrayIntoList(items, medium); 
    insertArrayIntoList(items, longest); 

    String formattedStory = ""; 
    for(String item : items) { 
     formattedStory += ((formattedStory.length() == 0) ? "" : " ") + item;       
    } 
    return formattedStory; 
} 
0

如果你不知道該陣列是最短的,你可以先檢查一下,不是之後你會發現這一點,你可以做一個for loop讓你始終從所有三個陣列與最低值獲得的價值最薄弱的環節......像這樣:

private static void doStackOverflow() { 
    String[] shortest = {"one", "two", "three", "four"}; 
    String[] medium = {"a", "b", "c", "d", "e", "f"}; 
    String[] longest = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}; 

    System.out.println(formatStory(shortest, medium, longest)); 
} 

public static String formatStory(String[] shortest, String[] medium, String[] longest) { 

    int weakestLink = findWeakestLink(shortest.length, medium.length, longest.length); 

    String formattedStory = ""; 
    for (int i = 0; i < weakestLink; i++) { 
     formattedStory += shortest[i] + " " + medium[i] + " " + longest[i] + " "; 
    } 

    return formattedStory; 
} 

private static int findWeakestLink(int shortest, int medium, int longest) { 
    int result = shortest + medium + longest; 

    if (shortest < result) 
     result = shortest; 
    if (medium < result) 
     result = medium; 
    if (longest < result) 
     result = longest; 

    return result; 
} 
0

A「快速和骯髒」(IMO)的解決辦法是這樣的:

public static String formatStory(String[] shortest, String[] medium, String[] longest) { 

    int s = shortest.length; 
    int m = medium.length; 
    int l = longest.length; 

    int max = Integer.max(s, Integer.max(m, l)); 

    StringBuilder result = new StringBuilder(); 

    for (int i = 0; i < max; i++) { 
     if (i < l) 
      result.append(longest[i]).append(" "); 
     if (i < m) 
      result.append(medium[i]).append(" "); 
     if (i < s) 
      result.append(shortest[i]).append(" "); 
    } 

    return result.toString().trim(); 
} 

它只是需要最大的數組的長度,循環(for循環索引)遍歷所有數組,看起來,如果數組中還有一個元素(index < length)並追加它。如果這更符合你的風格,你也可以使用String並簡單地附加+