Showing posts with label arrayList. Show all posts
Showing posts with label arrayList. Show all posts

Tuesday, 18 November 2014

Difference between Arrays.sort() and collections.sort() in java


Collections.sort()

Arrays.sort()
Collections.sort operates on a List
Arrays.sort operates on an array

Use Collections.sort() if you're dealing with something that implements the Collection interface - example: ArrayList

Use Arrays.sort() if you're dealing with an Array
Collections.sort() has a input as List so it does a translation of List to array and vice versa which is an additional step while sorting.
So this should be used when you are trying to sort a list.

Arrays.sort is for arrays so the sorting is done directly on the array.
So clearly it should be used when you have a array available with you and you want to sort it.


But internally both are same as collections.sort() uses arrays.sort() only to sort the elements.
The algorithm used for this sorting is mergesort algorithm.
Read More »

Thursday, 25 September 2014

How to sort and reverse an array list without using sort method

package test;

import java.util.ArrayList;
import java.util.List;

public class testing {

public static void main(String[] args) {
List<Integer> l= new ArrayList<Integer>();

l.add(1);
l.add(7);
l.add(90);
l.add(67);
int temp;
for (int i = 0; i < l.size(); i++) {
for (int j =0 ; j < l.size(); j++) {

if(l.get(i)>l.get(j)){
temp = l.get(i);
l.set(i,l.get(j));
l.set(j,temp) ;

}
}

}
for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));
}
}
}

 See :
How to reverse a string in java
Difference between Arrays.sort() and collections.sort() 
How to add instance of a class to list and then reterieve it
Constructor,Constructor Interview Questions
Overriding/method Overriding In java

Read More »

Monday, 22 September 2014

Internal Working of Array List

ArrayList works on the principle of creating a array and adding elements to it.

ArrayList class has a member variable elementData which is a Object array;


Object[] elementData;





When we do List l = new ArrayList(); the array elementData is initalized with a size of 10


if you use List l = new ArrayList(5) then an array elementData is initalized with a size of 5

add(E element)

Inside .add() method there is this check. Before adding element into the array it will check what is the current size of filled elements and what is the maximum size of the array. If size of filled elements is greater than maximum size of the array(or will be after adding current element) then size of the array must be increased .

 if it is completely filled that is all element 10 are filled a new array is created with a new capacity which is 1.5 of original array  using Arrays.copyOf. If the elementData array is not exhausted the new element is added in the array.
So adding a element in a array may take more time as a completely new array needs to be created with greater capacity and the data in the old array is transferred into the new array.

add(index i, E element)
On adding a element at a particular index in ArrayList, ArrayList checks if a element is already present at that index. If no than the element passed in add() is added at that index, otherwise a new array is created with the index kept vacant and the remaining element shifted to right.
For Eg:


List<Integer> l= new ArrayList<String>();
l.add(1);
l.add(2);
l.add(3);
for (int i: l)
system.out.println(l.get(i));

See also :
Difference between equals() and = =
Difference between Arrays.sort() and collections.sort() 
Static varibale,static class,Static method
How to add instance of a class to list and then reterieve it
Difference between Array and ArrayList
Custom Exception in Java -Tutorial
Read More »