35

配列全体を反復せずに文字列配列の先頭に文字列を追加することは可能ですか?

4

10 に答える 10

21

これを行う唯一の方法は、リング バッファーを維持することです。つまり、開始位置を記憶するカウンターがあり、配列内のすべてのエントリを移動する代わりにそれを移動します。これは、「開始」の意味を再定義するためにのみ機能します。

3 つのフィールドを持つ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       }

注: 配列のすべての要素を下にシフトするのではなく、ヘッドを移動します。

于 2013-01-10T10:45:45.187 に答える
16

試す

    String[] a = {"1", "2"};
    String[] a2 = new String[a.length + 1];
    a2[0] = "0";
    System.arraycopy(a, 0, a2, 1, a.length);
于 2013-01-10T10:49:07.593 に答える
6

これは、@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()]);
于 2014-01-09T20:07:13.930 に答える
4

できません...新しい文字列に対応するために、その後に続くすべての文字列を前方に移動する必要があります。0 番目のインデックスに直接追加すると、前の要素が失われます。

于 2013-01-10T10:44:13.250 に答える
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()]);
于 2013-01-10T11:02:22.443 に答える
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;
}
于 2013-01-10T11:10:18.343 に答える
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));
}
}
于 2013-01-10T11:21:20.997 に答える
1

そのためには、 を使用する必要がありますList

特に配列を内部で使用したい場合は、ArrayList

于 2013-01-10T10:45:15.137 に答える