【问题标题】:Add to a String[] another String[]将另一个 String[] 添加到 String[]
【发布时间】:2014-03-07 10:27:57
【问题描述】:
    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[] 我该怎么做?

谢谢

【问题讨论】:

  • 到目前为止您尝试过什么?另请查看 Java 命名约定

标签: java string add


【解决方案1】:

如果您使用标准集合类型,您会发现执行此操作要容易得多。特别是,尝试使用@987654321@<String>,而不是String[]。然后你会发现将多个列表添加到另一个列表很简单,只需调用“addAll”方法即可,该方法旨在将元素从一个集合复制到另一个集合。

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

    • 谢谢,我认为您的第二个解决方案是最好的:p
    【解决方案3】:

    Arrays.copyOf 会为你工作。

    一个建议 - 如何做到这一点!

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

    【讨论】:

    • 我不能说:“不能在数组类型 String[] 上调用 copy(String[])”
    • 就这个:Info.copy(Livraison);
    • String[] 不提供此功能,Arrays 是提供copyOf 功能的实用程序类
    【解决方案4】:
    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);
    }
    

    【讨论】:

      【解决方案5】:

      除非您经常需要这样做(我愿意),否则您可能会发现将数组包装在 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));
      
        }
      }
      

      请注意,数组用于在内部支持列表,因此如果您更改数组,连接的版本也会更改。显然,如果数组被调整大小,那么这将中断连接。

      【讨论】:

        【解决方案6】:

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

        基于您的代码示例:(当您声明 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
            }
        

        【讨论】:

        • 不,我没有在做对象编程。所以我用我需要的所有字段创建了 1 个新类,它工作正常 :-)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-26
        相关资源
        最近更新 更多