添加到String []另一个String []

时间:2014-03-07 10:27:57

标签: java string add

    Info = new String[15];
    Livraison = new String[5];
    Facturation = new String[5];
    Autres = new String[3];

    Livraison = AddressForm(JP_Add_Livraison,"Livraison");
    Facturation = AddressForm(JP_Add_Facturation,"Facturation");

    Autres[0] = JT_Tel.getText();
    Autres[1] = JT_Contact.getText();
    Autres[2] = JT_Date.getText();
    Autres[3] = JT_Note.getText();

            Info.add(Livraison);
            Info.add(Facturation);
            Info.add(Autres);

我想要3 String [] - > Livraison + Facturation + Autres in Info [] 我怎么能这样做?

由于

6 个答案:

答案 0 :(得分:2)

如果您使用标准集合类型,您会发现这样做要容易得多。特别是,请尝试使用List<String>,而不是String[]。然后你会发现将多个列表添加到另一个列表是一个简单的问题,即调用“addAll”方法,该方法旨在将元素从一个集合复制到另一个集合。

答案 1 :(得分:1)

Arrays.copyOf会对你有用。

建议 - 如何做到这一点!

int len1 = newarray.length;
int len2 = arraytobecopied.length;
String[] result = Arrays.copyOf(newarray, len1 + len2);
System.arraycopy(arraytobecopied, 0, result, len1, len2);

答案 2 :(得分:1)

您可以像这样创建数组和数组:

String[][] arrays = { array1, array2, array3, array4, array5 };

但是,或者,您可以创建一个具有这些属性的类,不知道这是否是您想要做的..

public class Something{
    String[] Livraison;
    String[] Facturation;
    String[] Autres;
}

答案 3 :(得分:0)

public static void main(String[] args) throws Exception {
    String[] all = new String[15];
    String[] some = new String[] { "one", "two", "three" };
    String[] more = new String[] { "four", "five" };

    System.arraycopy(some, 0, all, 0, some.length);
    System.arraycopy(more, 0, all, some.length, more.length);

    for (String value : all) System.out.println(value);
}

答案 4 :(得分:0)

完全超过顶部,除非你需要做很多事情(我这样做),你可能会发现将数组包装在一个有用的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 acrioss 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() {
      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.
            if (i.hasNext()) {
              a = i.next();
            } else {
              // Finished.
              a = null;
            }
            ai = 0;
          }
          if (a != null) {
            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 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<>(
            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));

  }
}

请注意,数组用于在内部备份列表,因此如果更改数组,则连接的版本也会更改。显然,如果数组被调整大小,则会破坏连接。

答案 5 :(得分:0)

问自己一个问题:我真的需要数组吗?

基于您的代码示例:(当您声明Autres 3的长度并添加4个元素时,它实际上可以正常工作)

Autres[0] = JT_Tel.getText(); 
Autres[1] = JT_Contact.getText();
Autres[2] = JT_Date.getText();
Autres[3] = JT_Note.getText();

我建议您使用对象Autres

   class Autres{
    private String tel,contact,date,note;
    //getters and setters ommited
    }