java


Which is faster - inserting an element at the end of an array or a linked list


Which is faster - inserting an element at the end of an array or a linkedlist ?
I assume you meant ArrayList when you said "array", since in Java you can't "add" to a full array.
Firstly if you're "inserting at the end" you're actually appending to the end not, "inserting". This distinction is important because inserting into an ArrayList at an arbitrary position is an O(n) operation, because all elements to the right must be "shifted" along by one position to make room for the element being inserted.
Adding to (the tail of) a LinkedList is always a O(1) (constant time) operation.
Adding to an ArrayList is usually a O(1) operation, but may be a O(n) operation if the backing array is full, because a new array must be allocated and every element copied across. In the general case of the array not being full, the performance of ArrayList is (slightly) faster than LinkedList, but the difference is very small.
The amortised cost of both is the same, but if constant time is required every time, only a LinkedList can do it.
Appending an element to the end of Array would be faster then Adding to a LinkedList as adding to a LinkedList always means creation of a cell at the end of the list and then adding a value to it. In Array you just add a value to existing cell. However, in Array if it is already full and doesn't have a space you will get IndexOutOfBoundsException while in the LinkedList you will never have this problem as the List will always grow according to your needs. Also there is distinct difference between LinkedList and ArrayList with ArrayList there is a random access which means the access to any element is always O(1) while in LinkedListit is always sequential access which means that access time will differ depending on the index and in worst case scenario is O(n). However, Appending to LinkedList is Always O(1) but not so in ArrayList With ArrayList if it is not full it will be O(1) but if it is full it will first grow by more then one cell and only ten it will add the value, so the worst case scenario is O(n)

Related Links

JComboBox from ArrayList<String> - Not working - Java Swing
Issue with MySql Unique key
Parallel sort of generic linked list
Format a string containing no spaces using java [closed]
Beginner Programmer: Java String Manipulation
Implementing findOne in Spring Data (MongoDB)
My Asyntask is not working when I put it inside a separate java file
Create a variable having name stored in a string variable
WAR directory structure for Spring MVC JSP app?
Clicking back button causes force close / NullPointerException
How to add C++ “int” to HashSet<Integer>?
Time since first boot up
HeadlessException in my amazon ec2 instance
Netbeans 8.0 Unsupported major.minor version 52.0 error
SQLException Parameter index out of range
Trying to multithread a Sieve of Eratosthenes, with each thread taking a range of numbers to sieve

Categories

HOME
sendgrid
urbancode
activiti
plot
iterator
dot
read-eval-print-loop
sqlite-net-extensions
spring-jdbc
malloc
pivotal-cloud-foundry
in-app-purchase
paradox
autotools
cross-validation
nano-server
resize
netflix
highlight.js
evopdf
openedx
errorlevel
web-sql
wpfdatagrid
phpfox
ping
ejabberd-module
bpel
preg-match
galsim
semantic-versioning
copying
vsts-build-task
http-digest
streamsets
impersonation
hue
graphenedb
dynamic-reports
elmah
node-gyp
turbogears
acoustics
s
segment
skeleton-css-boilerplate
long-polling
jsch
flickr-api
network-flow
python-c-api
celery-task
integrity
np-complete
python-cryptography
color-profile
django-scheduler
redundancy
specrun
angular-cache
grails-tomcat-plugin
thrust
ctest
plottable.js
connect-by
actionbardrawertoggle
natvis
prerequisites
lemon
cannon.js
cyclomatic-complexity
microbenchmark
php-ci
createprocessasuser
tablelayout
openexr
article
xceed-datagrid
bundles
intentservice
c18
tidy
mylyn
http-unit
typoscript2
pvrtc
bluepill
viewswitcher
objective-c-2.0
enter
nsdatecomponents
actionview
sortable-tables
web-architecture
webkit.net
exchange-server-2003
javap
anti-piracy
defensive-programming
.net-1.0
windows-live-messenger

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App