Friday, June 30, 2006

[java] Shows BitSet application

Shows BitSet application

import java.lang.*;

import java.util.*;

public class BitSetApp

{

public static void main(String args[])

{

int size= 50;

boolean a = true;

//creating bitset b1 as the size of "size"

BitSet b1 = new BitSet(size);

//setting b1

for (int i=0; i<size;++i) b1.set(i);

//cloning b1 to b2

BitSet b2 = (BitSet) b1.clone();

//clearing b2

for (int i=0; i<size; i=i+2)

b2.clear(i);

//outing b1

System.out.println("b1: ");

//getting b1 elements

for (int i=0; i<size; i=i+2)

System.out.print(b1.get(i) +" ");

//getting b2

System.out.println("\nb2: ");

for (int i=0; i<size; i=i+2)

System.out.print(b2.get(i) +" ");

//outing b1 and b2

System.out.println();

System.out.println("b1: "+b1);

System.out.println("b2: " +b2);

//outing bitset b1 to a string

String b = b1.toString();

//outing the string

System.out.println("b1 as a String: " +b);

//getting b1 hashcode

int d = b1.hashCode();

System.out.println("Hash code of bit set b1: " +d);

//getting b1's logical length

d = b1.length();

System.out.println("Length of bit set b1: " +d);

//if b1 and b2 intersect

a = b2.intersects(b1);

System.out.println("b2 intersects b1: " +a);

//if b1 or not b2

b1.xor(b2);

System.out.println("b1 xor b2" +b1);

//elements common b1 and b2

b1.and(b2);

System.out.println("b1 and b2" +b1);

//if b1 and b2 have elements common

b1.or(b2);

System.out.println("b1 or b2" +b1);

//if b1 and not b2

b1.andNot(b2);

System.out.println("b1 andNot b2" +b1);

System.out.println("b2: " +b2);

//checking if b1 is empty

a = b1.isEmpty();

System.out.println("is b1 empty: " +a);

//getting b2 elements 35 to 50 in b1

b1 = b2.get(35, 50);

//flipping b1 elements

for (int i=0; i<=15; i++) b1.flip(i);

System.out.println("b1 after getting: " +b1);

//clearing half of the b2 elements

for (int i=0; i<size/2; ++i)

b2.clear(i);

System.out.println("b2 is: " +b2);

//clearing b2 from 25 to 50

b2.clear(25, 50);

System.out.println("b2 after clearing: " +b2);

//number of elements false in b1

d= b1.cardinality();

System.out.println("number of elements set false in b1 : " +d);

//setting b1 to false

b1.set(1, 16, false);

System.out.println("b1 after setting to false: "+b1);

}

}

Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India

No comments: