The 3rd lesson of the Java Learning Series--java Advanced Tutorials

Source: Internet
Author: User
Tags bit set bitset

Address of this article

Can read: Learn Java from scratch

Share an outline:

1.java Data structure

1. Java Data structure

  1) "Overview"

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

    • Enumeration (enumeration)
    • Bit set (BitSet)
    • Vectors (vector)
    • Stacks (Stack)
    • Dictionary (Dictionary)
    • Hash table (Hashtable)
    • Attributes (properties)

These classes are traditionally left behind, and a new framework-set framework (Collection) has been introduced in JAVA2, which we will discuss later.

  2) enumeration (enumeration)

A) The "definition" enumeration (enumeration) interface, although it does not belong to the data structure itself, is widely used in other data structure categories. The enumeration (the enumeration) interface defines a way to retrieve contiguous elements from a data structure

b) "code example"

ImportJava.util.Vector;Importjava.util.Enumeration; Public classEnumerationtester { Public Static voidMain (String args[]) {enumeration days; Vector DayNames=NewVector (); 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 ()); }   }}
Enumerationtester.java

Operation Result:

Sundaymondaytuesdaywednesdaythursdayfridaysaturday
Run Results

 3) Bit set (BitSet)

a) "Definition"

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 Boolean values by assigning a "bit" to each value and then setting or clearing the bits appropriately.

b) "code example"

ImportJava.util.BitSet; Public classBitsetdemo { Public Static voidMain (String args[]) {BitSet bits1=NewBitSet (16); BitSet bits2=NewBitSet (16); //set some bits      for(inti=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 bitsBits2.and (BITS1); System.out.println ("\nbits2 and Bits1:");     System.out.println (BITS2); //OR bitsbits2.or (BITS1); System.out.println ("\nbits2 OR bits1:");     System.out.println (BITS2); //XOR bitsBits2.xor (BITS1); System.out.println ("\nbits2 XOR bits1:");  System.out.println (BITS2); }}
Bitsetdemo.java

Operation Result:

Initial pattern in bits1:{0, 2, 4, 6, 8, ten,,}initial pattern in bits2:{1, 2, 3, 4, 6, 7, 8, 9, 11  ,}bits2 and bits1:{2, 4, 6, 8,}bits2 or bits1:{0, 2, 4, 6, 8, ten, +}bits2 XOR bits1:{}
Run Results

 4) vectors (vector)

a) "Definition"

Vector classes are very similar to traditional arrays, but the size of vectors can change dynamically as needed.

As well as arrays, the elements of a vector object can also be accessed through an index.

The main advantage of using the vector class is that you don't have to specify the size of the object when you create it, and its size changes dynamically as needed.

b) "code example"

ImportJava.util.*; Public classVectordemo { Public Static voidMain (String args[]) {//Initial size is 3, increment is 2Vector v =NewVector (3, 2); System.out.println ("Initial Size:" +v.size ()); System.out.println ("Initial Capacity:" +v.capacity ()); V.addelement (NewInteger (1)); V.addelement (NewInteger (2)); V.addelement (NewInteger (3)); V.addelement (NewInteger (4)); System.out.println ("Capacity after four additions:" +v.capacity ()); V.addelement (NewDouble (5.45)); System.out.println ("Current Capacity:" +v.capacity ()); V.addelement (NewDouble (6.08)); V.addelement (NewInteger (7)); System.out.println ("Current Capacity:" +v.capacity ()); V.addelement (NewFloat (9.4)); V.addelement (NewInteger (10)); System.out.println ("Current Capacity:" +v.capacity ()); V.addelement (NewInteger (11)); V.addelement (NewInteger (12)); System.out.println ("First element:" +(Integer) v.firstelement ()); System.out.println ("Last element:" +(Integer) v.lastelement ()); if(V.contains (NewInteger (3)) ) System.out.println ("Vector contains 3."); //enumerate the elements in the vector.Enumeration Venum =v.elements (); System.out.println ("\nelements in Vector:");  while(Venum.hasmoreelements ()) System.out.print (Venum.nextelement ()+ " ");   System.out.println (); }}
Vectordemo.java

Operation Result:

Initial size:03557913. Elements in Vector:1 2 3 4 5.45 6.08 7 9.4 10 11 12
Run Results

5) stack (stack)

a) "Definition"

Stack implements a last-in-first-out (LIFO) data structure.

You can understand the stack as a vertically distributed stack of objects, and when you add a new element, place the new element at the top of the other element.

When you take an element from the stack, you take an element from the top of the stack. In other words, the last element of the stack is first taken out.

b) "code example"

ImportJava.util.*; Public classStackdemo {Static voidShowpush (Stack St,inta) {St.push (NewInteger (a)); System.out.println ("Push (" + A + ")"); System.out.println ("Stack:" +St); }   Static voidShowpop (Stack St) {System.out.print ("Pop"); Integer a=(Integer) St.pop ();      System.out.println (a); System.out.println ("Stack:" +St); }    Public Static voidMain (String args[]) {Stack St=NewStack (); System.out.println ("Stack:" +St); Showpush (St,42); Showpush (St,66); Showpush (St,99);      Showpop (ST);      Showpop (ST);      Showpop (ST); Try{Showpop (ST); } Catch(emptystackexception e) {System.out.println ("Empty Stack"); }   }}
Stackdemo.java

Operation Result:

stack: []push] Stack: []push] Stack: []push] Stack: [A, A, a, a. Stack:[©:] c11>-> Empty Stack
Run Results

The 3rd lesson of the Java Learning Series--java Advanced Tutorials

Related Article

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.