2015-02-23 62 views
0

我需要有下面的聲明:聲明兩個字符串數組,一個延伸的其他Java中

private static String[] BASE = new String[] { "a", "b", "c" }; 
private static String[] EXTENDED = BASE + new String[] { "d", "e", "f" }; 

第一行聲明具有三個(或更多)字符串值的字符串數組。 第二行應使用BASE中的所有字符串值聲明一個字符串數組,然後添加三個(或更多)字符串值。

這可能嗎?如果是...如何?

回答

6

如果您使用的是Java 8,這是一個簡單的一行:

鑑於你的問題的兩個數組,像這樣:

private static String[] BASE = new String[] { "a", "b", "c" }; 
private static String[] EXTENSION = new String[] { "d", "e", "f" }; 

的解決辦法是:

String[] EXTENDED = Stream.concat(Arrays.stream(BASE), Arrays.stream(EXTENSION)) 
         .toArray(String[]::new); 
+1

嗯,這是一個班輪。我不確定我會說這是*簡單*因此...... – 2015-02-23 13:27:30

+1

您可能想用'Stream.of(「d」,「e」,「f」)替換'Arrays.stream(EXTENDED)' '。另一種選擇:'private static String [] EXTENDED = Stream.of(BASE,new String [] {「d」,「e」,「f」})。flatMap(Arrays :: stream).toArray(String []: :new);'雖然不如'concat'短。 – 2015-02-23 13:32:18

+0

@JonSkeet你是對的。 Java 8仍然是相當新的,並且很多人都沒有對流API進行充分的探索。 – 2015-02-23 17:57:40

4

不是這樣,沒有。您可以使用:

private static String[] EXTENDED = new String[BASE.length + 3]; 

static { 
    System.arraycopy(BASE, 0, EXTENDED, 0, BASE.length); 
    EXTENDED[BASE.length] = "d"; 
    EXTENDED[BASE.length + 1] = "e"; 
    EXTENDED[BASE.length + 2] = "f"; 
} 

或者寫一個方法來連接兩個數組,然後調用它:

private static String[] BASE = new String[] { "a", "b", "c" }; 
private static String[] EXTENDED = 
    ArrayUtils.concat(BASE, new String[] { "d", "e", "f" }); 

我不知道在JRE這樣的方法,但它不會很難寫 - 或者如果你想使用流API。

1

Apache Commons Lang庫中有解決方案: ArrayUtils.addAll(T[], T...)

String [] both = ArrayUtils.addAll(firstArray,secondArray);

+0

即使有了Java 8的改進,我仍然經常使用[Apache Commons Lang](http://commons.apache.org/proper/commons-lang/)...這是我最喜歡的通用庫。在這種特殊情況下,您發佈的代碼比Java 8單線解決方案更容易閱讀,所以如果性能不是考慮因素,我會使用您的解決方案。 – Paul 2015-02-23 14:08:25

0

不依賴於Java 8,這裏是另一種解決方案:

String[] BASE = new String[] { "a", "b", "c" }; 
String[] EXT = new String[] { "d", "e", "f" }; 

String[] CONCAT = Arrays.copyOf (BASE, BASE.length + EXT.length); 
System.arraycopy(EXT, 0, CONCAT, BASE.length, EXT.length); 
0

我前一陣子放在一起JoinedArray類,可以幫助。如果你不想經常這樣做,這有點矯枉過正,但如果你在代碼中做了這麼多,你可能會發現它有用。

它實際上並沒有將這些數組連接到一個新的數組中 - 它實際上提供了一個Iterable,然後可以遍歷連接的數組。

public class JoinedArray<T> implements Iterable<T> { 
    final List<T[]> joined; 

    @SafeVarargs 
    public JoinedArray(T[]... arrays) { 
    joined = Arrays.<T[]>asList(arrays); 
    } 

    @Override 
    public Iterator<T> iterator() { 
    return new JoinedIterator<>(joined); 
    } 

    private class JoinedIterator<T> implements Iterator<T> { 
    // The iterator across the arrays. 
    Iterator<T[]> i; 
    // The array I am working on. 
    T[] a; 
    // Where we are in it. 
    int ai; 
    // The next T to return. 
    T next = null; 

    private JoinedIterator(List<T[]> joined) { 
     i = joined.iterator(); 
     a = i.hasNext() ? i.next() : null; 
     ai = 0; 
    } 

    @Override 
    public boolean hasNext() { 
     while (next == null && a != null) { 
     // a goes to null at the end of i. 
     if (a != null) { 
      // End of a? 
      if (ai >= a.length) { 
      // Yes! Next i. 
      if (i.hasNext()) { 
       a = i.next(); 
      } else { 
       // Finished. 
       a = null; 
      } 
      ai = 0; 
      } 
      if (a != null) { 
      if (ai < a.length) { 
       next = a[ai++]; 
      } 
      } 
     } 
     } 
     return next != null; 
    } 

    @Override 
    public T next() { 
     T n = null; 
     if (hasNext()) { 
     // Give it to them. 
     n = next; 
     next = null; 
     } else { 
     // Not there!! 
     throw new NoSuchElementException(); 
     } 
     return n; 
    } 

    @Override 
    public void remove() { 
     throw new UnsupportedOperationException("Not supported."); 
    } 

    } 

    public static void main(String[] args) { 
    JoinedArray<String> a = new JoinedArray<>(
      new String[]{ 
       "Zero", 
       "One" 
      }, 
      new String[]{}, 
      new String[]{ 
       "Two", 
       "Three", 
       "Four", 
       "Five" 
      }, 
      new String[]{ 
       "Six", 
       "Seven", 
       "Eight", 
       "Nine" 
      }); 
    for (String s : a) { 
     System.out.println(s); 
    } 

    } 

} 
0
// Create 2 Arrays 
String[] first = new String[]{"a","b","c"}; 
String[] second = new String[]{"d","e","f"}; 

// Create a List 
List<String> tempList = new ArrayList<String>(); 

// Then add both arrays in the List 
tempList.addAll(Arrays.asList(first)); 
tempList.addAll(Arrays.asList(second)); 

// Then convert the List into array 
String[] finalStr = tempList.toArray(new String[tempList.size()]); 

// Thats it 
相關問題