Arrays:
Arrays are strongly typed. Hence only same type of elements can stored. (Ex. if “A” is an array, here we can store only integer or string or bool etc..).
Since strongly typed, boxing and unboxing never happens.
These are fixed in size and will not grow dynamically runtime.
To store and to retrieve we should use integral index.
Array don’t support Add() and Remove() functions.
ArrayList:
ArrayList are not strongly typed(Loosely typed).
Since Loosely typed, boxing and unboxing happens which results in performance problem.
They can grow dynamically while runtime.
ArrayList provide sufficient methods to work with like Add().
package com.listsample;
import java.util.*;
public class ListExample {
public static void main(String args[]) {
// Creating an empty array list
List list = new ArrayList();
Thread test3 = new Thread();
// Adding items to arrayList
list.add(2);
list.add(3);
list.add(6); // it will add Item3 to the third position of
// array list
list.add("DDD");
// Display the contents of the array list
System.out.println("The arraylist contains the following elements: "
+ list);
/*for (int i = 0; i < list.size(); i++) {
Integer iVal = Integer.valueOf(list.get(i).toString());
System.out.println("Index: " + i + " - Item: " + iVal);
}
// hasNext(): returns true if there are more elements
// next(): returns the next element
System.out.println("Retrieving items using iterator");
Iterator itr = list.iterator();
while (itr.hasNext()) {
String str = itr.next().toString();
System.out.println(str);
if (str.equals("AAA"))
itr.remove();
}
*/ // Generics - to restrict the collection object to specific data type
ArrayList<String> list1 = new ArrayList<String>();
list1.add("aaaa");
list1.add("2");
System.out.println("Availabiliy of the element " +list1.contains("bbb"));
ArrayList<String> list2 = new ArrayList<String>();
System.out.println("Before adding theelemets : "+list2.isEmpty());
list2.add("bbbb");
list2.add("aaaa");
list2.add("cccc");
list2.add("2");
list2.add("cccc");
ArrayList<String> list3 = new ArrayList<String>();
list3.addAll(list1);
list3.addAll(list2);
Collections.synchronizedList(list3);
System.out.println("The contents of list3 are : "+list3);
System.out.println("String list "+list2.toString());
System.out.println("Position of the element "+list2.indexOf("cccc")+" the position of the duplicate "+list2.lastIndexOf("cccc"));
System.out.println("After adding the elements: "+list2.isEmpty());
System.out.println("Comparing the lists: "+list1.containsAll(list2));
Iterator<String> itr1 = list1.iterator();
while (itr1.hasNext()) {
String s = itr1.next();
System.out.println(s);
}
String[] arr = new String[3];
arr[0] = "3";
arr[1]="3";
arr[2] = "4";
String[] arr1 = new String[3];
arr1[0] = "3";
arr1[1]="3";
arr1[2] = "4";
String s="AAA";
String s1= "AAA";
if(s.equals(s1))
{
System.out.println("I m here");
}
System.out.println("Array elements: "+Arrays.toString(arr));
}
}
Arrays are strongly typed. Hence only same type of elements can stored. (Ex. if “A” is an array, here we can store only integer or string or bool etc..).
Since strongly typed, boxing and unboxing never happens.
These are fixed in size and will not grow dynamically runtime.
To store and to retrieve we should use integral index.
Array don’t support Add() and Remove() functions.
ArrayList:
ArrayList are not strongly typed(Loosely typed).
Since Loosely typed, boxing and unboxing happens which results in performance problem.
They can grow dynamically while runtime.
ArrayList provide sufficient methods to work with like Add().
package com.listsample;
import java.util.*;
public class ListExample {
public static void main(String args[]) {
// Creating an empty array list
List list = new ArrayList();
Thread test3 = new Thread();
// Adding items to arrayList
list.add(2);
list.add(3);
list.add(6); // it will add Item3 to the third position of
// array list
list.add("DDD");
// Display the contents of the array list
System.out.println("The arraylist contains the following elements: "
+ list);
/*for (int i = 0; i < list.size(); i++) {
Integer iVal = Integer.valueOf(list.get(i).toString());
System.out.println("Index: " + i + " - Item: " + iVal);
}
// hasNext(): returns true if there are more elements
// next(): returns the next element
System.out.println("Retrieving items using iterator");
Iterator itr = list.iterator();
while (itr.hasNext()) {
String str = itr.next().toString();
System.out.println(str);
if (str.equals("AAA"))
itr.remove();
}
*/ // Generics - to restrict the collection object to specific data type
ArrayList<String> list1 = new ArrayList<String>();
list1.add("aaaa");
list1.add("2");
System.out.println("Availabiliy of the element " +list1.contains("bbb"));
ArrayList<String> list2 = new ArrayList<String>();
System.out.println("Before adding theelemets : "+list2.isEmpty());
list2.add("bbbb");
list2.add("aaaa");
list2.add("cccc");
list2.add("2");
list2.add("cccc");
ArrayList<String> list3 = new ArrayList<String>();
list3.addAll(list1);
list3.addAll(list2);
Collections.synchronizedList(list3);
System.out.println("The contents of list3 are : "+list3);
System.out.println("String list "+list2.toString());
System.out.println("Position of the element "+list2.indexOf("cccc")+" the position of the duplicate "+list2.lastIndexOf("cccc"));
System.out.println("After adding the elements: "+list2.isEmpty());
System.out.println("Comparing the lists: "+list1.containsAll(list2));
Iterator<String> itr1 = list1.iterator();
while (itr1.hasNext()) {
String s = itr1.next();
System.out.println(s);
}
String[] arr = new String[3];
arr[0] = "3";
arr[1]="3";
arr[2] = "4";
String[] arr1 = new String[3];
arr1[0] = "3";
arr1[1]="3";
arr1[2] = "4";
String s="AAA";
String s1= "AAA";
if(s.equals(s1))
{
System.out.println("I m here");
}
System.out.println("Array elements: "+Arrays.toString(arr));
}
}
No comments:
Post a Comment