An Example of BitSet by NumSeries

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - An Example of BitSet by NumSeries

This Java example program shows how to use BitSet.

[edit] Java Source Code

  • Package: com.darwinsys
  • File: NumSeries.java
  1. package com.darwinsys;
  2.  
  3. import java.util.BitSet;
  4.  
  5. public class NumSeries {
  6.     public static void main(String[] args) {
  7. 	for (int i = 1; i <= months.length; i++)
  8. 	    System.out.println("Month # " + i);
  9.  
  10. 	for (int i = 0; i < months.length; i++)
  11. 	    System.out.println("Month " + months[i]);
  12.  
  13. 	BitSet b = new BitSet();
  14. 	b.set(0); // January
  15. 	b.set(3); // April
  16.  
  17. 	for (int i = 0; i < months.length; i++) {
  18. 	    if (b.get(i))
  19. 		System.out.println("Month " + months[i] + " requested");
  20. 	}
  21.     }
  22.  
  23.     /** The names of the months. See Dates/Times chapter for a better way */
  24.     protected static String months[] =
  25. 	    { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",
  26. 		    "December" };
  27. }

[edit] What Result You Can Get

Run the program, you will get:

Month # 1
Month # 2
Month # 3
Month # 4
Month # 5
Month # 6
Month # 7
Month # 8
Month # 9
Month # 10
Month # 11
Month # 12
Month January
Month February
Month March
Month April
Month May
Month June
Month July
Month August
Month September
Month October
Month November
Month December
Month January requested
Month April requested

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools