Vector_List_Compared1
Objectives:
  • Vector is considered as a legacy code and avoided by the developers.
  • Collection frameworks can compose everything a vector can do.
  • Vector is opted during thread-safe operation, ensuring that only one thread is tendered while accessing the collection.
  • Fore thread safe operation, synchronized ArrayList   is preferred.

Code :

//
import java.io.*;
import java.lang.reflect.*;
import java.util.*;

public class ClassTemplate1 {

public String str1; private String str2; public ClassTemplate1() {
// TODO Auto-generated constructor stub
str2="encapsulated strictly Private scope";
System.out.println(str2);
}

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String[] array1 = {"ABC" ,"ACD", "AEF", "AFG","ADE"};
String[] array2 = {"Peter" ,"Sam", "Adam", "Julia","Nat"};
try{
System.out.println(" Vector array objects---");
Vector<Object> vstr1 =new Vector<Object> (5);
Arrays.sort(array1);Arrays.sort(array2);
//loading vector
for (int i = 0; i < array2.length; i++) {
//vstr1.insertElementAt(i,0);
vstr1.add(array1[i]);
}
//adding more on the vector
for (int i = 0; i < array2.length; i++) {
//vstr1.insertElementAt(i,0);
vstr1.add(array2[i]);
}
//Process.display1(vstr1);// will have run time error
System.out.println(vstr1); //print as block
//
List<String>alist = new ArrayList<String>(Arrays.asList(array1));
System.out.println("Printing sorted List as a block");
System.out.print(alist);System.out.println();
//send to a loop
System.out.println("Iterate sorted List with For loop");
Process.display2(alist);//

}catch(NumberFormatException e){
//
}
}
}
//Reference super class
class Process{
Process() {} // constructor
// quick sand Vector<Object> str1
public static void display1(Vector<Object> str1 ){
ListIterator<Object> iter = str1.listIterator();
while(iter.hasNext())
{
System.out.println("\t"+ iter + " array of ");
}
}

public static void display2(List<String> str1 ){

for(String stx : str1)
{
System.out.print("\t"+ stx );
}
//
}}

Runtime Display