BitSet Example in Java
From Java Example Source Code
Contents |
[edit] Overview - BitSet Example in Java
This Java example program shows a BitSet example in Java.
[edit] Java Source Code
- Package: example.bitset
- File: BitOHoney1.java
package example.bitset;
import java.util.BitSet;
public class BitOHoney1 {
public static void main(String args[]) {
String names[] =
{ "Hershey's Kisses", "Nestle's Crunch", "Snickers", "3 Musketeers", "Milky Way", "Twix", "Mr. Goodbar",
"Crunchie", "Godiva", "Charleston Chew", "Cadbury's", "Lindt", "Aero", "Hebert", "Toberlone", "Smarties",
"LifeSavers", "Riesen", "Goobers", "Raisenettes", "Nerds", "Tootsie Roll", "Sweet Tarts", "Cotton Candy" };
BitSet bits = new BitSet();
for (int i = 0, n = names.length; i < n; i++) {
if ((names[i].length() % 2) == 0) {
bits.set(i);
}}System.out.println(bits);
System.out.println("Size : " + bits.size());
System.out.println("Length: " + bits.length());
for (int i = 0, n = names.length; i < n; i++) {
if (!bits.get(i)) {
System.out.println(names[i] + " is odd");
}}BitSet bites = new BitSet();
bites.set(0);
bites.set(1);
bites.set(2);
bites.set(3);
bites.andNot(bits);
System.out.println(bites);
}}
[edit] What Result You Can Get
Run the program, you will get:
{0, 2, 3, 5, 7, 8, 12, 13, 15, 16, 17, 21, 23}
Size : 64
Length: 24
Nestle's Crunch is odd
Milky Way is odd
Mr. Goodbar is odd
Charleston Chew is odd
Cadbury's is odd
Lindt is odd
Toberlone is odd
Goobers is odd
Raisenettes is odd
Nerds is odd
Sweet Tarts is odd
{1}
[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.
