Java Getting Started---data structure enumeration (enumeration) & Bit Collection (bitset) __ Data structure

Source: Internet
Author: User
Tags bit set bitset int size

The Java Toolkit provides a powerful data structure. The data structure in Java consists mainly of the following interfaces and classes:

Enumeration (enumeration) Bit collection (bitset) vector (vector) stack (stack) dictionary (Dictionary) hash Table (Hashtable) property (properties)

These classes are traditionally legacy, introducing a new framework-set framework (Collection) in Java2, which we'll have in the following articles. However, in this article we mainly look at the enumeration (enumeration) & Bit Collection (Bitset) of the two data structures. The first is an enumeration (enumeration).

The enumeration (enumeration) interface, although it is not part of the data structure itself, is widely used in the context of other data structures. The enumeration (the enumeration) interface defines a way to retrieve contiguous elements from a data structure. For example, an enumeration defines a method called Nextelement, which is used to get the next element of a data structure that contains multiple elements.

The enumeration interface defines methods that allow you to enumerate (get one at a time) the elements in a collection of objects. This traditional interface has been replaced by iterators, and although enumeration has not yet been abandoned, it is rarely used in modern code. Nevertheless, it is used in methods defined by traditional classes such as vector and properties, and in addition to some API classes, and is also widely used in applications. The following table summarizes some of the enumeration declaration methods:

Serial Number Method Description
1 Boolean hasmoreelements ()
Tests whether this enumeration contains more elements.
2 Object nextelement ()
If there is at least one available element for this enumeration object, the next element of this enumeration is returned.

The following examples demonstrate the use of enumeration:

Import Java.util.Vector;
Import java.util.Enumeration;

public class Enumerationtester {

public static void Main (String args[]) {
Enumeration<string> days;
vector<string> daynames = new vector<string> ();
Daynames.add ("Sunday");
Daynames.add ("Monday");
Daynames.add ("Tuesday");
Daynames.add ("Wednesday");
Daynames.add ("Thursday");
Daynames.add ("Friday");
Daynames.add ("Saturday");
Days = Daynames.elements ();
while (Days.hasmoreelements ()) {
System.out.println (Days.nextelement ());
}
}
}

the results of the operation are:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday

then look at the lower set (Bitset). A bit collection class implements a set of bits or flags that can be set and cleared individually. This class is useful when dealing with a set of Boolean values, and you can manipulate a Boolean value by assigning a "bit" to each value and then setting or clearing the bits appropriately.

a Bitset class creates a special type of array to hold the bit value. The size of the array in Bitset will increase as needed. This is similar to the bit vector (vector of bits). This is a traditional class, but it is completely redesigned in Java 2. Bitset defines two construction methods, and the first construct creates a default object:

Bitset ()

The second method allows the user to specify the initial size. All bits are initialized to 0:

Bitset (int size)

The methods defined in the Cloneable interface are implemented in Bitset as listed in the following table:

Serial Number Method Description
1 void and (Bitset set)
Performs logic and operations on this target bit set and parameter bit set.
2 void AndNot (Bitset set)
Clears all bits in this bitset, and their corresponding bits are set in the specified bitset.
3 int cardinality ()
Returns the number of digits set to true in this bitset.
4 void Clear ()
Set all the bits in this bitset to false.
5 void Clear (int index)
Sets the bit at the index specified to False.
6 void Clear (int startIndex, int endindex)
Sets the specified Fromindex (including) to a bit in the specified Toindex (not included) range to false.
7 Object Clone ()
Copy this bitset to generate a new bitset equal to it.
8 Boolean equals (Object bitset)
Compares this object to the specified object.
9 void Flip (int index)
Sets the bit at the specified index to the complement of its current value.
10 void Flip (int startIndex, int endindex)
Sets the specified Fromindex (including) to the complement of its current value for each bit within the specified Toindex (excluding) range.
11 Boolean get (int index)
Returns the bit value at the specified index.
12 Bitset get (int startIndex, int endindex)
Returns a new Bitset, which is composed of bits from the Fromindex (including) to the Toindex (excluding) range from the Bitset.
13 int Hashcode ()
Returns the hash code value for this bit set.
14 Boolean intersects (Bitset Bitset)
Returns ture if there is a bit in the specified bitset that is set to true and is set to true in this bitset.
15 Boolean IsEmpty ()
If this bitset does not contain any bits set to True, return ture.
16 int Length ()
Returns the logical size of this bitset: The index of the highest set bit in Bitset plus 1.
17 int nextclearbit (int startIndex)
Returns the index of the first bit that is set to false, which occurs on the specified starting index or after the index.
18 int nextsetbit (int startIndex)
Returns the index of the first bit that is set to true, which occurs at the specified starting index or after the index.
19 void or (Bitset bitset)
Performs logic or operations on this bit set and bit set parameters.
20 void set (int index)
Sets the bit at the specified index to true.
21st void set (int index, Boolean v)
Sets the bit at the specified index to the specified value.
22 void set (int startIndex, int endindex)
Sets the specified Fromindex (including) to a bit in the specified Toindex (not included) range to true.
23 void set (int startIndex, int endindex, Boolean v)
Sets the specified Fromindex (including) to the specified value in the specified Toindex (excluding) bit.
24 int size ()
Returns the number of digits of the actual space used when this bitset represents a bit value.
25 String toString ()
Returns the string representation of this bit set.
26 void xor (Bitset bitset)
Performs a logical XOR or operation on this bit set and bit set parameters.

The following procedure illustrates several methods supported by this data structure:

Import Java.util.BitSet;

public class Bitsetdemo {

public static void Main (String args[]) {
Bitset bits1 = new Bitset (16);
Bitset bits2 = new Bitset (16);

Set some bits
for (int i=0; i<16; i++) {
if ((i%2) = = 0) bits1.set (i);
if ((i%5)!= 0) Bits2.set (i);
}
System.out.println ("Initial pattern in Bits1:");
System.out.println (BITS1);
System.out.println ("\ninitial pattern in bits2:");
System.out.println (BITS2);

and Bits
Bits2.and (BITS1);
System.out.println ("\nbits2 and Bits1:");
System.out.println (BITS2);

OR bits
Bits2.or (BITS1);
System.out.println ("\nbits2 OR bits1:");
System.out.println (BITS2);

XOR bits
Bits2.xor (BITS1);
System.out.println ("\nbits2 XOR bits1:");
System.out.println (BITS2);
}
}

the results of the operation are:

Initial pattern in Bits1:
{0, 2, 4, 6, 8, ten, {} Initial in

bits2: {1, 2, 3, 4, 6, 7, 8,
9, 11, 1  2,

bits2 and Bits1:
{2, 4, 6, 8,}

bits2 OR bits1:
{0, 2, 4, 6, 8, Ten,}

bits2 XOR Bits1:
{}
Well, that's it for now, and subsequent data structures will be recorded in later articles. If the feeling is good, please give a lot of praise support oh ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.