17

可能重复:
如何在 Java 中连接两个数组?

我有两个对象

HealthMessage[] healthMessages1;
HealthMessage[] healthMessages2;

HealthMessage[] healthMessagesAll;

healthMessages1 = x.getHealth( );   
healthMessages2 = y.getHealth( );

我应该如何加入这两个对象,所以我只能返回一个:

return healthMessagesAll;

推荐的方法是什么?

4

7 回答 7

37

使用 Apache Commons Collections API 是一个好方法:

healthMessagesAll = ArrayUtils.addAll(healthMessages1,healthMessages2);
于 2012-11-27T11:47:50.777 回答
21

我会分配一个总长度为healthMessages1and的数组,healthMessages2并使用System.arraycopyor 两个for循环来复制它们的内容。这是一个示例System.arraycopy

public class HelloWorld {

     public static void main(String []args) {

        int[] a = new int[] { 1, 2, 3};
        int[] b = new int[] { 3, 4, 5};
        int[] r = new int[a.length + b.length];
        System.arraycopy(a, 0, r, 0, a.length);
        System.arraycopy(b, 0, r, a.length, b.length);

        // prints 1, 2, 3, 4, 5 on sep. lines
        for(int x : r) {
            System.out.println(x);
        }            
     }         
}
于 2012-11-27T11:47:04.060 回答
13

这写起来更直观,您不必处理数组索引:

Collection<HealthMessage> collection = new ArrayList<HealthMessage>();
collection.addAll(Arrays.asList(healthMessages1));
collection.addAll(Arrays.asList(healthMessages2));

HealthMessage[] healthMessagesAll = collection.toArray(new HealthMessage[] {});

..但不要问我与..相比它的表现System.arraycopy

于 2012-11-27T11:53:52.603 回答
4

我会去System.arraycopy

private static HealthMessage[] join(HealthMessage[] healthMessages1, HealthMessage[] healthMessages2)
{
    HealthMessage[] healthMessagesAll = new HealthMessage[healthMessages1.length + healthMessages2.length];

    System.arraycopy(healthMessages1, 0, healthMessagesAll, 0, healthMessages1.length);
    System.arraycopy(healthMessages2, 0, healthMessagesAll, healthMessages1.length, healthMessages2.length);

    return healthMessagesAll;
}
于 2012-11-27T12:51:27.443 回答
0

数组是固定长度的,因此您有多种选择。这里有一对:

a)创建一个具有其他大小的新数组并手动复制所有元素。

healthMessagesAll = new HealthMessage[healthMessages1.length + healthMessages2.length];
int i = 0;
for (HealthMessage msg : healthMessases1)
{
   healthMessagesAll[i] = msg;
   i++;
}

for (HealthMessage msg : healthMessages2)
{
   healthMessagesAll[i] = msg;
   i++;
}

b) 使用Arrays类提供的方法。您可以将数组转换为列表,或批量复制元素。查看它提供的功能并选择适合您的功能。

更新

看到你关于重复的评论。你可能想把所有东西都放在一个Set保证唯一性的地方。如果两次添加相同的元素,则不会添加第二次。然后,如果您明确需要具有自己toArray()方法的数组,则可以将 Set 转换回数组。

正如其他受访者所建议的那样,System.arraycopy()也可以帮助您复制元素的内容,因此它是我上面的替代方案 (a) 的较短版本。

于 2012-11-27T11:51:04.567 回答
0

对于最复杂但最不占用内存的解决方案,您可以将它们包装在一个对象中。这个提供了一个Iterator<T>跨越所有项目的copyTo方法和一种复制到新数组的方法。它可以很容易地增强以提供 getter 和 setter。

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

  // Pass all arrays to be joined as constructor parameters.
  public JoinedArray(T[]... arrays) {
    joined = Arrays.asList(arrays);
  }

  // Iterate across all entries in all arrays (in sequence).
  public Iterator<T> iterator() {
    return new JoinedIterator<T>(joined);
  }

  private class JoinedIterator<T> implements Iterator<T> {
    // The iterator across the arrays.
    Iterator<T[]> i;
    // The array I am working on. Equivalent to i.next without the hassle.
    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 = nextArray();
    }

    private T[] nextArray () {
      ai = 0;
      return i.hasNext() ? i.next() : null;
    }

    public boolean hasNext() {
      if (next == null) {
        // a goes to null at the end of i.
        if (a != null) {
          // End of a?
          if (ai >= a.length) {
            // Yes! Next i.
            a = nextArray();
          }
          if (a != null) {
            next = a[ai++];
          }
        }
      }
      return next != null;
    }

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

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

  public int copyTo(T[] to, int offset, int length) {
    int copied = 0;
    // Walk each of my arrays.
    for (T[] a : joined) {
      // All done if nothing left to copy.
      if (length <= 0) {
        break;
      }
      if (offset < a.length) {
        // Copy up to the end or to the limit, whichever is the first.
        int n = Math.min(a.length - offset, length);
        System.arraycopy(a, offset, to, copied, n);
        offset = 0;
        copied += n;
        length -= n;
      } else {
        // Skip this array completely.
        offset -= a.length;
      }
    }
    return copied;
  }

  public int copyTo(T[] to, int offset) {
    return copyTo(to, offset, to.length);
  }

  public int copyTo(T[] to) {
    return copyTo(to, 0);
  }

  @Override
  public String toString() {
    StringBuilder s = new StringBuilder();
    Separator comma = new Separator(",");
    for (T[] a : joined) {
      s.append(comma.sep()).append(Arrays.toString(a));
    }
    return s.toString();
  }

  public static void main(String[] args) {
    JoinedArray<String> a = new JoinedArray<String>(
            new String[]{
              "One"
            },
            new String[]{
              "Two",
              "Three",
              "Four",
              "Five"
            },
            new String[]{
              "Six",
              "Seven",
              "Eight",
              "Nine"
            });
    for (String s : a) {
      System.out.println(s);
    }
    String[] four = new String[4];
    int copied = a.copyTo(four, 3, 4);
    System.out.println("Copied " + copied + " = " + Arrays.toString(four));

  }
}
于 2012-11-27T12:04:51.907 回答
-1

沿着这条路怎么办:

    List<String> l1 = Arrays.asList(healthMessages1);
    l1.addAll(Arrays.asList(healthMessages2));
    HealthMessage[] result = l1.toArray();

(需要一点泛化... :)

于 2012-11-27T11:52:35.663 回答