Tutorialspoint

Java How list can be sorted

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

public class Demo {
   public static void main(String args[]) {
      List aList = new ArrayList();
      aList.add("James");
      aList.add("Harry");
      aList.add("Susan");
      aList.add("Emma");
      aList.add("Peter");
      System.out.println("The unsorted ArrayList is: " + aList);
      Collections.sort(aList);
      System.out.println("The sorted ArrayList is: " + aList);
   }
}

Java Binary Search using Collections.binarySearch

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

public class Demo {
   public static void main(String args[]) {
      List aList = new ArrayList();
      aList.add("James");
      aList.add("George");
      aList.add("Bruce");
      aList.add("Susan");
      aList.add("Martha");
      Collections.sort(aList);
      System.out.println("The sorted ArrayList is: " + aList);
      int index = Collections.binarySearch(aList, "Martha");
      System.out.println("Element Martha is at index: " + index);
      index = Collections.binarySearch(aList, "Amy");
      System.out.println("Element Amy is at index: " + index);
   }
}

Java How a Vector can be cloned

import java.util.Vector;

public class Demo {
   public static void main(String args[]) {
      Vector vec1 = new Vector();
      vec1.add(7);
      vec1.add(3);
      vec1.add(5);
      vec1.add(9);
      vec1.add(2);
      Vector vec2 = (Vector) vec1.clone();
      System.out.println("The Vector vec1 elements are: " + vec1);
      System.out.println("The Vector vec2 elements are: " + vec2);
   }
}

Java Convert a Vector to an array

import java.util.Vector;

public class Demo {
   public static void main(String args[]) {
      Vector vec = new Vector();
      vec.add(7);
      vec.add(3);
      vec.add(5);
      vec.add(2);
      vec.add(8);
      Object[] arr = vec.toArray();
      System.out.println("The Array elements are: ");
      for (int i = 0; i < arr.length; i++) {
         System.out.println(arr[i]);
      }
   }
}

Java Find the minimum element of a Vector

import java.util.Collections;
import java.util.Vector;

public class Demo {
   public static void main(String args[]) {
      Vector vec = new Vector();
      vec.add(7);
      vec.add(3);
      vec.add(9);
      vec.add(5);
      vec.add(8);
      System.out.println("The Vector elements are: " + vec);
      System.out.println("The minimum element of the Vector is: " + Collections.min(vec));
   }
}

Java Find the maximum element of a Vector

import java.util.Collections;
import java.util.Vector;

public class Demo {
   public static void main(String args[]) {
      Vector vec = new Vector();
      vec.add(7);
      vec.add(3);
      vec.add(9);
      vec.add(5);
      vec.add(8);
      System.out.println("The Vector elements are: " + vec);
      System.out.println("The maximum element of the Vector is: " + Collections.max(vec));
   }
}

Java Loop through the Vector elements using an Iterator

import java.util.Iterator;
import java.util.Vector;

public class Demo
{
  public static void main(String args[]) 
  {
    Vector vec = new Vector();
    
    vec.add(4);
    vec.add(1);
    vec.add(3);
    vec.add(9);
    vec.add(6);
    vec.add(8);

    System.out.println("The Vector elements are: ");
    
    Iterator i = vec.iterator();
    
    while (i.hasNext()) 
    {
      System.out.println(i.next());
    }

  }
}

Java Loop through the Vector elements using an Iterator

import java.util.Iterator;
import java.util.Vector;

public class Demo {
   public static void main(String args[]) {
      Vector vec = new Vector();
      vec.add(4);
      vec.add(1);
      vec.add(3);
      vec.add(9);
      vec.add(6);
      vec.add(8);
      System.out.println("The Vector elements are: ");
      Iterator i = vec.iterator();
      while (i.hasNext()) {
         System.out.println(i.next());
      }
   }
}

Java Enumerate through the Vector elements

import java.util.Enumeration;
import java.util.Vector;

public class Demo {
   public static void main(String args[]) {
      Vector vec = new Vector();
      vec.add(7);
      vec.add(3);
      vec.add(5);
      vec.add(9);
      vec.add(2);
      vec.add(8);
      System.out.println("The Vector elements are: ");
      Enumeration e = vec.elements();
      while (e.hasMoreElements()) {
         System.out.println(e.nextElement());
      }
   }
}

Java Get first and last elements from Vector

import java.util.Vector;

public class Demo {
   public static void main(String args[]) {
      Vector vec = new Vector();
      vec.add(4);
      vec.add(7);
      vec.add(2);
      vec.add(8);
      vec.add(6);
      System.out.println("The Vector elements are: " + vec);
      System.out.println("The first element of the Vector is : " + vec.firstElement());
      System.out.println("The last element of the Vector is : " + vec.lastElement());
   }
}

1 2 3 4 5 6 7 ... 2070 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.