将String添加到String数组的开头

时间:2013-01-10 10:43:25

标签: java

是否可以在不迭代整个数组的情况下将字符串添加到String数组的开头。

9 个答案:

答案 0 :(得分:21)

执行此操作的唯一方法是维护环形缓冲区。即你有一个计数器,它记住了开始的位置,你移动它而不是移动数组中的所有条目。这只能起作用,因为你重新定义了“开始”的含义。

查看ArrayDeque的来源,其中包含三个字段

   86       /**
   87        * The array in which the elements of the deque are stored.
   88        * The capacity of the deque is the length of this array, which is
   89        * always a power of two. The array is never allowed to become
   90        * full, except transiently within an addX method where it is
   91        * resized (see doubleCapacity) immediately upon becoming full,
   92        * thus avoiding head and tail wrapping around to equal each
   93        * other.  We also guarantee that all array cells not holding
   94        * deque elements are always null.
   95        */
   96       private transient E[] elements;
   97   
   98       /**
   99        * The index of the element at the head of the deque (which is the
  100        * element that would be removed by remove() or pop()); or an
  101        * arbitrary number equal to tail if the deque is empty.
  102        */
  103       private transient int head;
  104   
  105       /**
  106        * The index at which the next element would be added to the tail
  107        * of the deque (via addLast(E), add(E), or push(E)).
  108        */
  109       private transient int tail;

所以添加到开头就像这样

  224       public void addFirst(E e) {
  225           if (e == null)
  226               throw new NullPointerException();
  227           elements[head = (head - 1) & (elements.length - 1)] = e;
  228           if (head == tail)
  229               doubleCapacity();
  230       }


  312       /**
  313        * @throws NoSuchElementException {@inheritDoc}
  314        */
  315       public E getFirst() {
  316           E x = elements[head];
  317           if (x == null)
  318               throw new NoSuchElementException();
  319           return x;
  320       }

注意:它移动头部而不是将所有元素向下移动。

答案 1 :(得分:12)

    String[] a = {"1", "2"};
    String[] a2 = new String[a.length + 1];
    a2[0] = "0";
    System.arraycopy(a, 0, a2, 1, a.length);

答案 2 :(得分:10)

如果您已经在使用Guava,可以使用ObjectArrays::concat执行此操作:

String[] args = ...;
ObjectArrays.concat("prepended", args);

答案 3 :(得分:5)

这是@matteosilv提出的解决方案的更正版本:

String[] myArray= {"hi","hi2"};
List<String> list = new LinkedList<String>(Arrays.asList(myArray));
list.add(0, "h3");
myArray = list.toArray(new String[list.size()]);

答案 4 :(得分:4)

你不能......你必须移动它前面的所有字符串才能容纳新的字符串。 如果您直接将其添加到第0个索引,那么您将丢失之前的元素

答案 5 :(得分:3)

String[] myArray= {"hi","hi2"};
List<String> temp = new ArrayList<String>(Arrays.asList(prova));
temp.add(0, "h3");
myArray = temp.toArray(new String[temp.size()]);

答案 6 :(得分:1)

为此,您应该使用List

如果您想特别使用内部数组,请转到ArrayList

答案 7 :(得分:1)

我能做的最好......

public static void main(String[] args) {
        String[] s = new String[] { "a", "b", "c" };
        System.out.println(Arrays.toString(prepend(s,"d")));
}

public static String[] prepend(String[] a, String el) {
        String[] c = new String[a.length+1];
        c[0] = el;
        System.arraycopy(a, 0, c, 1, a.length);
        return c;
}

答案 8 :(得分:1)

您可以执行以下操作

public class Test {

public static String[] addFirst(String s[], String e) {
    String[] temp = new String[s.length + 1];
    temp[0] = e;
    System.arraycopy(s, 0, temp, 1, s.length);
    return temp;
}

public static void main(String[] args) {
    String[] s = { "b", "c" };
    s = addFirst(s, "a");
    System.out.println(Arrays.toString(s));
}
}