Java 从数组中删除第一个元素的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3663944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:17:32  来源:igfitidea点击:

What is the best way to remove the first element from an array?

javaarrays

提问by NullVoxPopuli

I have string array (String[]) and I need to remove the first item. How can I do that efficiently?

我有字符串数组 ( String[]),我需要删除第一项。我怎样才能有效地做到这一点?

采纳答案by jjnguy

The size of arrays in Java cannot be changed. So, technically you cannot remove any elements from the array.

Java 中数组的大小不能更改。因此,从技术上讲,您不能从数组中删除任何元素。

One way to simulate removing an element from the array is to create a new, smaller array, and then copy all of the elements from the original array into the new, smaller array.

模拟从数组中删除元素的一种方法是创建一个新的较小的数组,然后将原始数组中的所有元素复制到新的较小数组中。

String[] yourArray = Arrays.copyOfRange(oldArr, 1, oldArr.length);

However, I would not suggest the above method. You should really be using a List<String>. Lists allow you to add and remove items from any index. That would look similar to the following:

但是,我不会建议上述方法。你真的应该使用List<String>. 列表允许您在任何索引中添加和删除项目。这将类似于以下内容:

List<String> list = new ArrayList<String>(); // or LinkedList<String>();
list.add("Stuff");
// add lots of stuff
list.remove(0); // removes the first item

回答by mikera

Simplest way is probably as follows - you basically need to construct a new array that is one element smaller, then copy the elements you want to keep to the right positions.

最简单的方法可能如下——你基本上需要构造一个小一个元素的新数组,然后将你想要保留的元素复制到正确的位置。

int n=oldArray.length-1;
String[] newArray=new String[n];
System.arraycopy(oldArray,1,newArray,0,n);

Note that if you find yourself doing this kind of operation frequently, it could be a sign that you should actually be using a different kind of data structure, e.g. a linked list. Constructing a new array every time is an O(n) operation, which could get expensive if your array is large. A linked list would give you O(1) removal of the first element.

请注意,如果您发现自己经常执行此类操作,则可能表明您实际上应该使用不同类型的数据结构,例如链表。每次构造一个新数组是一个 O(n) 操作,如果您的数组很大,这可能会变得昂贵。链表会给你 O(1) 删除第一个元素。

An alternative idea is not to remove the first item at all, but just increment an integer that points to the first index that is in use. Users of the array will need to take this offset into account, but this can be an efficient approach. The Java String class actually uses this method internally when creating substrings.

另一种想法是根本不删除第一个项目,而只是增加一个指向正在使用的第一个索引的整数。阵列的用户需要考虑这个偏移量,但这可能是一种有效的方法。Java String 类在创建子字符串时实际上在内部使用此方法。

回答by Matthew Flaschen

You can't do it at all, let alone quickly. Arrays in Java are fixed size. Two things you could do are:

你根本做不到,更别提快了。Java 中的数组是固定大小的。你可以做的两件事是:

  1. Shift every element up one, then set the last element to null.
  2. Create a new array, then copy it.
  1. 将每个元素向上移动一个,然后将最后一个元素设置为 null。
  2. 创建一个新数组,然后复制它。

You can use System.arraycopyfor either of these. Both of these are O(n), since they copy all but 1 element.

您可以System.arraycopy用于其中任何一个。这两个都是 O(n),因为它们复制除了 1 个元素之外的所有元素。

If you will be removing the first element often, consider using LinkedListinstead. You can use LinkedList.remove, which is from the Queueinterface, for convenience. With LinkedList, removing the first element is O(1). In fact, removing any element is O(1) once you have a ListIteratorto that position. However, accessing an arbitrary element by index is O(n).

如果您将经常删除第一个元素,请考虑LinkedList改用。为方便起见LinkedList.remove,您可以使用来自Queue界面的 。使用LinkedList,删除第一个元素是 O(1)。事实上,一旦你有了ListIterator那个位置,删除任何元素都是 O(1) 。但是,通过索引访问任意元素是 O(n)。

回答by msw

Keep an index of the first "live" element of the array. Removing (pretending to remove) the first element then becomes an O(1)time complexity operation.

保留数组第一个“活动”元素的索引。移除(假装移除)第一个元素就变成了一个O(1)时间复杂度操作。

回答by Emil

An alternative ugly method:

另一种丑陋的方法:

   String[] a ={"BLAH00001","DIK-11","DIK-2","MAN5"};
   String[] k=Arrays.toString(a).split(", ",2)[1].split("]")[0].split(", ");

回答by mjad-org

To sum up, the quick linkedlist method:

总结一下,快速链表方法:

List<String> llist = new LinkedList<String>(Arrays.asList(oldArray));
llist.remove(0);