Java 将字符串添加到字符串数组的开头

Java 将字符串添加到字符串数组的开头,java,Java,是否可以在不迭代整个数组的情况下将字符串添加到字符串数组的开头。您不能…您必须向前移动它后面的所有字符串以容纳新字符串。 如果直接将其添加到第0个索引,则会丢失前面的元素要执行此操作,应使用列表 如果您想特别使用内部数组,请使用ArrayList唯一的方法是维护一个环形缓冲区。i、 e.您有一个计数器,可以记住起始位置,您可以移动该计数器,而不是移动数组中的所有条目。这仅仅是因为您重新定义了“开始”的含义 请参见具有三个字段的源 86 /** 87 * Th

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

您不能…您必须向前移动它后面的所有字符串以容纳新字符串。
如果直接将其添加到第0个索引,则会丢失前面的元素

要执行此操作,应使用
列表


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

唯一的方法是维护一个环形缓冲区。i、 e.您有一个计数器,可以记住起始位置,您可以移动该计数器,而不是移动数组中的所有条目。这仅仅是因为您重新定义了“开始”的含义

请参见具有三个字段的源

   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;
因此,添加到start的工作方式如下

  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       }
注意:它移动头部,而不是移动阵列中的所有元素。

试试看

    String[] a = {"1", "2"};
    String[] a2 = new String[a.length + 1];
    a2[0] = "0";
    System.arraycopy(a, 0, a2, 1, a.length);
String[]myArray={“hi”,“hi2”};
List temp=newarraylist(Arrays.asList(prova));
温度加(0,“h3”);
myArray=temp.toArray(新字符串[temp.size()]);
我能做的最好的

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

你可以做下面这样的事情

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

这是@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()]);
String[]myArray={“hi”,“hi2”};
List List=newlinkedlist(Arrays.asList(myArray));
列表。添加(0,“h3”);
myArray=list.toArray(新字符串[list.size()]);

如果您已经在使用番石榴,您可以使用
ObjectArrays::concat
来执行此操作:

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

我很好奇。你能用不固定的环长来做吗?你有一些伪代码的例子吗?@Jerome从ArrayDeque中添加了一些代码,这是不固定的。如果需要,它会将数组的大小加倍。不能使用ArrayList执行getFirst(…)、getLast(…)、addFirst(…)、addLast(…)、removeFirst(…)或removeLast(…)。它没有像LinkedList那样提供额外的方法,以便于在列表末端进行操作does@Sibi我想你会发现你可以用add(int-index,E)。。。看这里@EvgeniyDorofeev,回答得不错。这当然会迭代数组中的所有元素。使用适合您需要的不同数据结构(例如
ArrayList
)会更好。使用ArrayUtils.add(T[]数组,int索引,T元素)()实际上您的解决方案不起作用。原因如下:这将起作用:String[]myArray={“hi”,“hi2”};List List=newlinkedlist(Arrays.asList(myArray));列表。添加(0,“h3”);myArray=list.toArray(新字符串[temp.size()]);
String[] args = ...;
ObjectArrays.concat("prepended", args);