Array:
1): We can't increase or decrease size at runtime.(Array are fixed in size)
example:
class Array
{static public void main(String[] s)
{int []a=new int[10];
a={1};
System.out.println(a[0]);
}
}
output:
1
2) In the form of memory point of view arrays concept is not recommended to use (i.e. arrays size are fixed if we use memory for elements less than arrays size so memory will be wasted).
3) In the form of performance point of view arrays concept is recommended to use (i.e. we know the size of arrays in advance or at compile time so no overhead at runtime that why it takes less time).
4) Arrays can hold homogeneous data elements (i.e. Array elements are of the same type).
5) Arrays don’t provide readymade method support that’s why we can call as arrays are not underlying data structure.
6) Arrays are capable to hold both primitives (byte, short, int, long etc.) and Objects (Wrapper Classes, String, StringBuffer or Any user-defined classes).
example: object type
class Array
{
public static void main(String[] args){
Integer[] ar = new Integer[10];
System.out.println(ar[0]);
}
}
Example 2: For Primitives Type
class ArrayClass{ public static void main(String[] args){ int[] a = new int[10]; System.out.println(a[0]); }}
Output
0
ArrayList:The ArrayList class extends AbstractList and implements the List interface.ArrayList support dynamic that can grow as needed.
Array lists are created with an initial size.When this size is exceeded, the collection is automatically enlarged.When objects are removed, the arry may be shrunk
ArratList hold both primitive and object data type.
We can increase or decrease size of ArrayList at run time.
import java.util.*;
class List{
public static void main(String[] args){
ArrayList a = new ArrayList();
a.add(11);
a.add(22);
a.add(33);
System.out.println(a);
al.add(44);
System.out.println("Basic arraylist are :"+a);
}
}
output:
[11,22,33]
Basic ArrayList are :[11,22,33,44]
- In case of memory point of view ArrayList is recommended to use (i.e. ArrayList size are not fixed memory will be allocated according to ArrayList elements size).
- In case of performance point of view, ArrayList is not recommended to use.
- ArrayList can hold both homogeneous and heterogeneous data elements (i.e. ArrayList elements may be of a different type).
Example:
import java.util.*;
class ArrayListClass{
public static void main(String[] args){
ArrayList al = new ArrayList(10);
al.add("A");
al.add("true");
System.out.println(al);
}
}
- ArrayList provide readymade method support that’s why we can call as ArrayList is underlying data structure.
Example:
import java.util.*;
class ArrayListClass{
public static void main(String[] args){
ArrayList al = new ArrayList(10);
al.add("A");
al.add("true");
al.remove(1);
System.out.println(al.size());
}
}
Output
1
Redymade method:
add(), remove(), size() etc.
please like and subscribe the post thank you
No comments:
Post a Comment