Docsity

Programming Java Part11-Java Programming-Lecture Slides, Slides of Java Programming

Toggle sidebar
bg1
Miscellaneous Example6
ListIterator import java.util.*;
public class ListIteratorSample
{
public static void main( String [] args ){
LinkedList ll = new LinkedList();
ll.add(new Long(7));
ll.add(new Long(5));
ll.add(new Long(3));
ListIterator li = ll.listIterator();
while ( li.hasNext() )
{
adrish.b@ardentcollaboratio
ns.com
http://www.ardentcollaborati
ons.com
351
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download Programming Java Part11-Java Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Miscellaneous Example

ListIterator ^ import java.util.*;

 public class ListIteratorSample

 public static void main( String [] args ){

 LinkedList ll = new LinkedList();

 ll.add(new Long(7));

 ll.add(new Long(5));

 ll.add(new Long(3));

 ListIterator li = ll.listIterator();

 while ( li.hasNext() )

adrish.b@ardentcollaboratio

351

Miscellaneous Example

ListIterator ^ System.out.print( li.next() );

 if ( li.hasNext() ) System.out.print(", ");

 System.out.println("");

 while ( li.hasPrevious() )

 System.out.print( li.previous() );

 if ( li.hasPrevious() ) System.out.print(", ");

adrish.b@ardentcollaboratio

352

Miscellaneous Example

HashMap ^ while ( i.hasNext() )  {

 Map.Entry me = (Map.Entry)i.next();

 System.out.print("KEY: "+me.getKey());

 System.out.println(",VAL: "+me.getValue());

 }

 }

 }

adrish.b@ardentcollaboratio

354

KEY: Key, VAL: 2 KEY: 5, VAL: Value

Miscellaneous Example

TreeMap ^ import java.util.*;  public class TreeMapSample

 {

 public static void main( String [] args )

 {

 TreeMap tm = new TreeMap();

 tm.put("CKEY", "Just A String");

 tm.put("BKEY", new Long(2));

 tm.put("AKEY", new Long(1));

 Set s = tm.entrySet();

adrish.b@ardentcollaboratio

355

Miscellaneous Example

Comparator ^ import java.util.*;  class MyComparator implements Comparator

 {

 public int compare( Object a, Object b )

 {

 long la = ((Long)a).longValue();

 long lb = ((Long)b).longValue();

 long ra = la % 2;

 long rb = lb % 2;

adrish.b@ardentcollaboratio

357

Miscellaneous Example

Comparator ^ if ( ( ra == 0 && rb == 1 ) || la < lb )  return -1;

 else if ( ( rb == 0 && ra == 1 ) || la > lb )

 return 1;

 return 1;

 }

 }

adrish.b@ardentcollaboratio

358

Miscellaneous Example

Comparator ^ ts.add( new Long(10) );  Iterator i = ts.iterator();

 while ( i.hasNext() )

 {

 System.out.print(i.next());

 if (i.hasNext()) System.out.print(", ");

 }

 }

 }

adrish.b@ardentcollaboratio

360

Miscellaneous Example

Vector import java.util.*;

class vector {

public static void main(String args[]) {

Vector vector = new Vector(5);

System.out.println("Capacity: " + vector.capacity());

vector.addElement(new Integer(0));

vector.addElement(new Integer(1));

vector.addElement(new Integer(2));

vector.addElement(new Integer(3));

vector.addElement(new Integer(4));

vector.addElement(new Integer(5));

adrish.b@ardentcollaboratio

361

(^363) java.util package(part-2)-Utility Classes

adrish.b@ardentcollaboratio

By Adrish Bhattacharyay Ardent Collaborations

StringTokenizer

 The processing of text often consists of parsing a formatted

input string. Parsing is the division of text into a set of

discrete parts, or tokens, which in a certain sequence can

convey a semantic meaning. The StringTokenizer class

provides the first step in this parsing process, often

called the lexer (lexical analyzer). StringTokenizer

implements the Enumeration interface. So you can

enumerate the individual tokens contained in it using

StringTokenizer.

adrish.b@ardentcollaboratio

364

StringTokenizer-methods

 Method Description  int countTokens( ) Using the current set of delimiters, the method determines the number of tokens left to be parsed and returns the result.  boolean hasMoreElements( ) Returns true if one or more tokens remain in the string and returns false if there are none.  boolean hasMoreTokens( ) Returns true if one or more tokens remain in the string and returns false if there are none.  Object nextElement( ) Returns the next token as an Object.  String nextToken( ) Returns the next token as a String.  String nextToken(String delimiters) Returns the next token as a String and sets the delimiters string to that specified by delimiters.

adrish.b@ardentcollaboratio

366

StringTokenizer

Program demostrating StringTokenizer methods

import java.util.StringTokenizer; class STDemo { static String in = "name=Adrish Bhattacharyay;" + "address=Chandannagar;" + "[email protected];" + "Qualification=B.Tech"; public static void main(String args[]) { StringTokenizer st = new StringTokenizer(in, "=;"); while(st.hasMoreTokens()) { String key = st.nextToken(); String val = st.nextToken(); System.out.println(key + "\t" + val); } }}

adrish.b@ardentcollaboratio

367

name Adrish Bhattacharyay address Chandannagar email [email protected] Qualification B.Tech

BitSet methods

 Method Description  void and(BitSet bitSet) ANDs the contents of the invoking BitSet object with those specified by bitSet. The result is placed into the invoking object.  void andNot(BitSet bitSet) For each 1 bit in bitSet, the corresponding bit in the invoking BitSet is cleared.  int cardinality( ) Returns the number of set bits in the invoking object.  void clear( ) Zeros all bits.  void clear(int index) Zeros the bit specified by index.  void clear(int startIndex, int endIndex) Zeros the bits from startIndex to (endIndex– 1).

adrish.b@ardentcollaboratio

369

BitSet methods

 Method Description  Object clone( ) Duplicates the invoking BitSet object.  boolean equals(Object bitSet) Returns true if the invoking bit set is equivalent to the one passed in bitSet. Otherwise, the method returns false.  boolean get(int index) Returns the current state of the bit at the specified index.  BitSet get(int startIndex, int endIndex) Returns a BitSet that consists of the bits from startIndex to endIndex–1. The invoking object is not changed.  boolean intersects(BitSet bitSet) Returns true if at least one pair of corresponding bits within the invoking object and bitSet are 1.

adrish.b@ardentcollaboratio

370