Sunteți pe pagina 1din 3

1. What are the major differences between Array, ArrayList, and LinkedList?

The difference is in the performance, because of how ArrayList and LinkedList work internally, some operations are more efficient on ArrayList, and some operations are more efficient on LinkedList. For example, inserting an element in the middle of the list is relatively slow on ArrayList, but fast on LinkedList. And looking up a random element in the list is fast on ArrayList, but slow on LinkedList. Which of the two you should choose depends on what you're going to use the list for Arrays: Simple Data Structure Store a row of values of the same type: Primitive types (int, double, etc) int[] grades = new grades[10]; //This stores 10 different grades// Object types (Students, books, cars, etc.) You access each value in an Array using the index[] int[] evenNums = new int[50]; //first 50 even numbers// evenNums[0] = 2; evenNums[3] = 8; Also classes remember: Array index start at 0.

ArrayList: In the ArrayList, the elements are stored internally just like in an Array. Elements in ArrayLists are stored as type Object

Thus in order to take an element out of an ArrayList , we will need to cast it into the type we want Since ArrayList is a class, it has its own methods For Example: set method change an entry size( ) method count number of elements

Arrays Stores any one type objects) Fixed length Has index Faster at retrieving its elements

ArrayLists Stores only objects (Can store different

Flexible length Does not have index slower at retrieving its elements

LinkedList In a linked list, each link stores an item, and is connected to the next link The list holds a reference to the first (and maybe the last) element A LinkedList stores its elements internally in a linked list data structure Example:. LinkedList anotherList = new LinkedList(); add method is fast it just appends a new link to the end of the list get method is slow has to walk down the list retrieve the element at the specified index

1. ArrayList: Advantage: Fast Random Access

For Example: Calling get(int) will just access the underlying array. Disadvantage: Adding values might be slow When you dont know the amount of values the array will contain when you create it, a lot of shifting is going to be done in the memory space when the ArrayList manipulates its internal array.

2. LinkedList Advantage Fast manipulation As youd expect, adding and removing new data anywhere in the list is instantaneous. Change two links, and you have a new value anywhere you want it. Disadvantage: No random access Even though the get(int) is still there, it now just iterates the list until it reaches the index you specified. It has some optimizations in order to do that, but thats basically it.

The Java Tutorials List Implementations, obtained 08/12/2013 http://docs.oracle.com/javase/tutorial/collections/implementations/list.html Shelly, Gary B., Cashman, Thomas J., Starks, Joy L., Mick, Michael L.. (2009-10-04). Java Programming: Comprehensive Concepts and Techniques, 3rd Edition. Delmar

S-ar putea să vă placă și