Question

    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);

I want the 3 String[] -> Livraison + Facturation + Autres in Info[] How can I do that ?

Thanks

Was it helpful?

Solution 2

You can create and array of arrays like this:

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

But, alternatively, you could create a class that has those attributes, don't know if that's what you want to do..

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

OTHER TIPS

You'll find it much easier to do this if you work with the standard collections types. In particular, try using List<String>, instead of String[]. Then you'll find that adding mutiple lists to another list is a simple matter of calling the "addAll" method which is designed to copy the elements from one collection to another.

Arrays.copyOf will work for you.

A suggestion - how to do this!

int len1 = newarray.length;
int len2 = arraytobecopied.length;
String[] result = Arrays.copyOf(newarray, len1 + len2);
System.arraycopy(arraytobecopied, 0, result, len1, len2);
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);
}

Totally over the top unless you need to do this a lot (I do) you may find wrapping the arrays in an Iterable useful.

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));

  }
}

Note that the arrays are used to back the lists internally so if you change the arrays the joined versions also change. Obviously if the arrays get resized then that will break the connection.

ask yourself question : do i really need arrays?

based on your code sample:(which actually shoulnd work as you declare length of Autres 3 and add 4 elements)

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

i recommend you to go with object Autres

   class Autres{
    private String tel,contact,date,note;
    //getters and setters ommited
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top