简体   繁体   English

ArrayList 与 LinkedList 的字节

[英]ArrayList vs LinkedList for bytes

I want to create a list of bytes.我想创建一个字节列表。 (In Java) The size is known. (在 Java 中)大小是已知的。 After every iteration, I'd like to remove the first element.每次迭代后,我想删除第一个元素。 And at any point in time, I will only be accessing the first element.在任何时候,我都只会访问第一个元素。

I thought about using a queue but that will not work in my use case as I need to keep the item in the list for multiple iterations.我考虑过使用队列,但这在我的用例中不起作用,因为我需要将项目保留在列表中进行多次迭代。

What will be best suited keeping the performance in mind.什么最适合记住性能。

Array list of LinkedList? LinkedList 的数组列表? (Order is important) Since size is not changing, how about using array and accessing i-th element? (顺序很重要)既然大小没有变化,那么使用数组和访问第 i 个元素怎么样? Without removing the first everytime?每次都不删除第一个?

For removal of first element an arrayList will be slower as it uses arrays to store data while a linkedList would be much faster O(1)为了删除第一个元素,arrayList 会变慢,因为它使用数组来存储数据,而 linksList 会快得多O(1)

Refer blogs like;参考博客; https://www.baeldung.com/java-remove-first-element-from-list#:~:text=ArrayList's%20remove()%20method,elements%20need%20to%20be%20shifted . https://www.baeldung.com/java-remove-first-element-from-list#:~:text=ArrayList's%20remove()%20method,elements%20need%20to%20be%20shifted

For removal of 1st element order does not matter anyway.无论如何,删除第一个元素的顺序无关紧要。 So keeping performance in mind, LinkedList is the way to go for you.因此,请牢记性能,LinkedList 是您的最佳选择。

For accessing a random i'th element however, an ArrayList is much faster O(1) than LinkedList O(n)然而,为了访问随机第 i 个元素,ArrayList 比 LinkedList O(n)快得多O(1) O(n)

Refer the answer to question for more details to this or any further questions regarding LinkedList vs ArrayList: When to use LinkedList over ArrayList in Java?有关此问题的更多详细信息或有关 LinkedList 与 ArrayList 的任何其他问题,请参阅问题的答案: 何时在 Java 中使用 LinkedList over ArrayList?

I am not sure if you want to implement this by yourself.我不确定你是否想自己实现这个。 If that's not the case, check Java Byte Class which is a byte primitive wrapper.如果不是这种情况,请检查Java Byte Class ,它是一个字节原始包装器。

Regarding accessing the first element, there are multiples approaches, some of them are:关于访问第一个元素,有多种方法,其中一些是:

  1. Just simply iterating over the list with an index and then moving it to the last element and starting over again when the last element is reached.只需简单地使用索引遍历列表,然后将其移动到最后一个元素,并在到达最后一个元素时重新开始。

  2. Poping the elements and adding them into another queue.弹出元素并将它们添加到另一个队列中。 When the last element is deleted you just simply change the queues and start over.当最后一个元素被删除时,您只需更改队列并重新开始。

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

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