如果我需要从 List 中删除一个对象,您更喜欢删除哪一个,假设字符串“abc”
linkedList 还是 ArrayList ? (我认为,两者是相同的)
如果我使用 Linkedlist 和 arraylist,时间和空间复杂度是多少
(我相信两者的时间复杂度相同,均为 O(n)
两者都有相同的时间复杂度 - O(n),但恕我直言,LinkedList
版本会更快,特别是在大型列表中,因为当您从数组中删除元素时(ArrayList
),右侧的所有元素都必须向左移动 - 为了填充空的数组元素,而LinkedList
只需要重新连接 4 个参考
以下是其他列表方法的时间复杂度:
For LinkedList<E>
get(int index) - O(n)
add(E element) - O(1)
add(int index, E element) - O(n)
remove(int index) - O(n)
Iterator.remove() is O(1)
ListIterator.add(E element) - O(1)
For ArrayList<E>
get(int index) is O(1)
add(E element) is O(1) amortized, but O(n) worst-case since the array must be resized and copied
add(int index, E element) is O(n - index) amortized, O(n) worst-case
remove(int index) - O(n - index) (removing last is O(1))
Iterator.remove() - O(n - index)
ListIterator.add(E element) - O(n - index)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)