声明两个String数组,一个在Java中扩展另一个

时间:2015-02-23 13:22:43

标签: java arrays

我需要声明如下:

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

第一行声明一个包含三个(或更多)字符串值的字符串数组。 第二行应该声明一个包含BASE中所有字符串值的字符串数组,然后添加三个(或更多)字符串值。

这可能吗?如果是......怎么样?

6 个答案:

答案 0 :(得分: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 :(得分: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。

答案 2 :(得分:1)

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

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

答案 3 :(得分: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);

答案 4 :(得分: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);
    }

  }

}

答案 5 :(得分: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