简体   繁体   English

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

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

I have string array ( String[] ) and I need to remove the first item. 我有字符串数组( String[] ),我需要删除第一项。 How can I do that efficiently? 我怎样才能有效地做到这一点?

The size of arrays in Java cannot be changed. Java中的数组大小无法更改。 So, technically you cannot remove any elements from the array. 因此,从技术上讲,您无法从阵列中删除任何元素。

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> . 你应该真的使用List<String> Lists allow you to add and remove items from any index. 列表允许您添加和删除任何索引中的项目。 That would look similar to the following: 这看起来类似于以下内容:

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

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, eg a linked list. 请注意,如果您发现自己经常进行此类操作,则可能表明您应该实际使用其他类型的数据结构,例如链接列表。 Constructing a new array every time is an O(n) operation, which could get expensive if your array is large. 每次构造一个新数组是一个O(n)操作,如果你的数组很大,这可能会很昂贵。 A linked list would give you O(1) removal of the first element. 链表将为您提供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类实际上在内部使用此方法。

You can't do it at all, let alone quickly. 你完全不能这样做,更不用说了。 Arrays in Java are fixed size. Java中的数组是固定大小的。 Two things you could do are: 你可以做的两件事是:

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

You can use System.arraycopy for either of these. 您可以将System.arraycopy用于其中任何一个。 Both of these are O(n), since they copy all but 1 element. 这两个都是O(n),因为它们复制了除1个元素以外的所有元素。

If you will be removing the first element often, consider using LinkedList instead. 如果您要经常删除第一个元素,请考虑使用LinkedList You can use LinkedList.remove , which is from the Queue interface, for convenience. 为方便起见,您可以使用来自Queue接口的LinkedList.remove With LinkedList , removing the first element is O(1). 使用LinkedList ,删除第一个元素是O(1)。 In fact, removing any element is O(1) once you have a ListIterator to that position. 事实上,一旦你有一个ListIterator到那个位置,删除任何元素是O(1)。 However, accessing an arbitrary element by index is O(n). 但是,通过索引访问任意元素是O(n)。

Keep an index of the first "live" element of the array. 保留数组的第一个“live”元素的索引。 Removing (pretending to remove) the first element then becomes an O(1) time complexity operation. 删除(假装删除)第一个元素然后变为O(1)时间复杂度操作。

To sum up, the quick linkedlist method: 总结一下,快速链表方法:

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

An alternative ugly method: 另一种丑陋的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM