Friday, 4 April 2014

Iterate the List

ListIterator<E> listIterator()
    List Provides way to iterate the List both forward and backward way.

import java.util.*;

class listIteratorEx{
 public static void main(String args[]){
  List<Integer> myList = new LinkedList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  myList.add(50);
  
  /* traverse the list in Forward */
  ListIterator<Integer> iter = myList.listIterator();
  System.out.println("Forward Traversal Of the List is");
  while(iter.hasNext()){
   System.out.print(iter.next() + " ");
  }
  System.out.println();
  
  /* traverse the list in Backward */
  System.out.println("Backward Traversal Of the List is");
  while(iter.hasPrevious()){
   System.out.print(iter.previous() + " ");
  }
  System.out.println();
  
 }
}

Output

Forward Traversal Of the List is
10 20 30 40 50
Backward Traversal Of the List is
50 40 30 20 10

Prevoius                                                 Next                                                 Home

Get the Last Occurrence of the Element

int lastIndexOf(Object o)
   return the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element

import java.util.*;

class ListLastIndexOf{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  myList.add(10);
  myList.add(40);
  myList.add(10);
    
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.println("Last Index Of 10 is " + myList.lastIndexOf(10));
  System.out.println("Last Index Of 100 is " + myList.lastIndexOf(100));
 } 
}

Output
Elements in myList are
[10, 20, 30, 40, 10, 40, 10]
Last Index Of 10 is 6
Last Index Of 100 is -1

1. throws ClassCastException if the type of the specified element is incompatible with this list

2. throws NullPointerException if the specified element is null and this
list does not permit null elements



Prevoius                                                 Next                                                 Home

Get the First Occurrence of the Element

int indexOf(Object o)
   Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

import java.util.*;

class ListIndexOf{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
    
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.println("Index Of 30 is " + myList.indexOf(30));
  System.out.println("Index Of 100 is " + myList.indexOf(100));
 } 
}

Output

Elements in myList are
[10, 20, 30, 40]
Index Of 30 is 2
Index Of 100 is -1

1. throws ClassCastException if the type of the specified element is incompatible with this list

2. throws NullPointerException if the specified element is null and this
list does not permit null elements




Prevoius                                                 Next                                                 Home

Removes the element at the specified position

E remove(int index)
   Removes the element at the specified position in this list. Return the element previously at the specified position

import java.util.*;

class ListRemove{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  for(int i=3; i >= 0; i--){
   System.out.println("Removing the Element " + myList.remove(i) + " at index " + i);
   System.out.println(myList);
  }
 } 
}

Output
Elements in myList are
[10, 20, 30, 40]
Removing the Element 40 at index 3
[10, 20, 30]
Removing the Element 30 at index 2
[10, 20]
Removing the Element 20 at index 1
[10]
Removing the Element 10 at index 0
[]

1. throws UnsupportedOperationException if the remove operation is not supported by this list
import java.util.*;

class ListRemoveUnsupport{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  List<Integer> unmodifiable;
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  unmodifiable = Collections.unmodifiableList(myList);
  
  System.out.println("Elements in unmodifiable List are");
  System.out.println(unmodifiable);
  
  unmodifiable.remove(1);
 } 
}

Output

Elements in unmodifiable List are
[10, 20, 30, 40]
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableList.remove(Unknown Source)
        at ListRemoveUnsupport.main(ListRemoveUnsupport.java:19)

2. throws IndexOutOfBoundsException if the index is out of range

import java.util.*;

class ListRemoveIndexOut{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
    
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  myList.remove(-1);
 } 
}

Output
Elements in myList are
[10, 20, 30, 40]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
        at java.util.ArrayList.elementData(Unknown Source)
        at java.util.ArrayList.remove(Unknown Source)
        at ListRemoveIndexOut.main(ListRemoveIndexOut.java:16)


Prevoius                                                 Next                                                 Home

Insert Element At Particular position

void add(int index, E element)
   Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right.

import java.util.*;
class ListAddAtIndex{
 public static void main(String args[]){
  List<Integer> myList = new LinkedList<> ();
  
  myList.add(6);
  myList.add(7);
  myList.add(8);
  myList.add(9);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  for(int i=0; i < 6; i++){
   System.out.println("Adding " + i + " At " + i + "th index");
   myList.add(i, i);
   System.out.println(myList);
  }
 }
}

Output
Elements in myList are
[6, 7, 8, 9]
Adding 0 At 0th index
[0, 6, 7, 8, 9]
Adding 1 At 1th index
[0, 1, 6, 7, 8, 9]
Adding 2 At 2th index
[0, 1, 2, 6, 7, 8, 9]
Adding 3 At 3th index
[0, 1, 2, 3, 6, 7, 8, 9]
Adding 4 At 4th index
[0, 1, 2, 3, 4, 6, 7, 8, 9]
Adding 5 At 5th index
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

1. throws UnsupportedOperationException if the add operation is not supported by this list

import java.util.*;
class ListAddAtIndexUnsupport{
 public static void main(String args[]){
  List<Integer> myList = new LinkedList<> ();
  List<Integer> unmodifiable;
  
  myList.add(6);
  myList.add(7);
  myList.add(8);
  myList.add(9);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  unmodifiable = Collections.unmodifiableList(myList);
  System.out.println("Trying to add Elements to unmodifiable list");
  unmodifiable.add(0, 123); 
 }
}

Output

Elements in myList are
[6, 7, 8, 9]
Trying to add Elements to unmodifiable list
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableList.add(Unknown Source)
        at ListAddAtIndexUnsupport.main(ListAddAtIndexUnsupport.java:17)

2. throws ClassCastException if the class of the specified element prevents it from being added to this list

3. throws NullPointerException if the specified element is null and this list does not permit null elements

4. throws IllegalArgumentException if some property of the specified
element prevents it from being added to this list

5. throws IndexOutOfBoundsException if the index is out of range





Prevoius                                                 Next                                                 Home

set : Replace element at particular index

E set(int index, E element)
   Replaces the element at the specified position in this list with the specified element. Return the element previously at the specified position

import java.util.*;
class ListSet{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in the List are");
  System.out.println(myList);
  
  /* Replace the Elements */
  for(int i=0; i < myList.size(); i++){
   System.out.println("Current Element At " + i +"th Position is " + myList.set(i, i));
   System.out.println("After Replacing " + i + "th element ");
   System.out.println(myList);
  }
 }
}


Output
Elements in the List are
[10, 20, 30, 40]
Current Element At 0th Position is 10
After Replacing 0th element
[0, 20, 30, 40]
Current Element At 1th Position is 20
After Replacing 1th element
[0, 1, 30, 40]
Current Element At 2th Position is 30
After Replacing 2th element
[0, 1, 2, 40]
Current Element At 3th Position is 40
After Replacing 3th element
[0, 1, 2, 3]

1. throws UnsupportedOperationException if the set operation is not supported by this list

import java.util.*;
class ListSetUnsupport{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  List<Integer> unmodifiable;
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in the List are");
  System.out.println(myList);
  
  unmodifiable = Collections.unmodifiableList(myList);
  System.out.println("Trying to Replace the Element in unmodifiable List");
  unmodifiable.set(0, 100);  
 }
}
 

Output
Elements in the List are
[10, 20, 30, 40]
Trying to Replace the Element in unmodifiable List
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableList.set(Unknown Source)
        at ListSetUnsupport.main(ListSetUnsupport.java:18)

2. throws ClassCastException if the class of the specified element prevents it from being added to this list

3. throws NullPointerException if the specified element is null and
this list does not permit null elements

4. throws IllegalArgumentException if some property of the specified
element prevents it from being added to this list

5. throws IndexOutOfBoundsException if the index is out of range
import java.util.*;
class ListSetIndexOut{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in the List are");
  System.out.println(myList);
  
  System.out.println("Trying to Replace the Element which is out of Bounds");
  myList.set(5, 100);  
 }
}

Output
Elements in the List are
[10, 20, 30, 40]
Trying to Replace the Element which is out of Bounds
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size:4
        at java.util.ArrayList.rangeCheck(Unknown Source)
        at java.util.ArrayList.set(Unknown Source)
        at ListSetIndexOut.main(ListSetIndexOut.java:16)




Prevoius                                                 Next                                                 Home

Wednesday, 2 April 2014

MouseListener

The listener interface for receiving mouse events like click, presses, Released, Entered and Exited.


public interface MouseListener extends EventListener {
    public void mouseClicked(MouseEvent e);
    public void mousePressed(MouseEvent e);
    public void mouseReleased(MouseEvent e);
    public void mouseEntered(MouseEvent e);
    public void mouseExited(MouseEvent e);
}

void mouseClicked(MouseEvent e)
Invoked when the mouse button has been clicked (pressed and released) on a component.

void mousePressed(MouseEvent e)
Invoked when a mouse button has been pressed on a component.

void mouseReleased(MouseEvent e)
Invoked when a mouse button has been released on a component.

void mouseEntered(MouseEvent e)
Invoked when the mouse enters a component.

void mouseExited(MouseEvent e)
Invoked when the mouse exits a component.


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;

public class MouseListenerEx{
 JTextArea displayArea;
 JTextField typingArea;
 JFrame frame1;
  
 MouseListenerEx(){
  
  /* Initialize typingArea */
  typingArea = new JTextField(20);
  
  /* Initialize Display Area */
  displayArea = new JTextArea();
  displayArea.setEditable(false);
  JScrollPane scrollPane = new JScrollPane(displayArea);
  scrollPane.setPreferredSize(new Dimension(375, 125));
   
  /* Initialize Frame */
  frame1 =new JFrame("Sample Frame");
  frame1.pack();
  frame1.setVisible(true);
  frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame1.getContentPane().add(typingArea, BorderLayout.PAGE_START);
  frame1.getContentPane().add(scrollPane, BorderLayout.CENTER);
  frame1.setSize(300, 500);
    
  /* Add Key Listener */
  typingArea.addMouseListener(new MouseListener(){
   public void mouseClicked(MouseEvent e){
    displayArea.append("Mouse Clicked\n" + e +"\n");
   }
    
   public void mousePressed(MouseEvent e){
    displayArea.append("Mouse Pressed\n " + e +"\n");
   }
    
   public void mouseReleased(MouseEvent e){
    displayArea.append("Mouse Released\n" + e +"\n");
   }
   
   public void mouseEntered(MouseEvent e){
    displayArea.append("Mouse Entered\n" + e +"\n");
   }
   
   public void mouseExited(MouseEvent e){
    displayArea.append("Mouse Exited\n" + e +"\n");
   }
  });   
 }
 
 public static void main(String args[]){
  new MouseListenerEx();
 }
}

Output






Prevoius                                                 Next                                                 Home