Java getting started note 3_Datastructure

Source: Internet
Author: User
4. Data Structure
4.1 Array
4.1.1 Basic concepts
Arrays are objects in Java, so they need to be instantiated before being used. The elements in the array can be basic elements or objects, but the element types in the same array must be the same.
The object stored in the array is not the object itself, but the object reference.
4.1.2 array declaration and Instantiation
(1) String difWords []; Point hits [];
(2) String [] difWords []; Point [] hits; return type commonly used in methods;
(3) String [] names = new String [10]; int [] temps = new int [10];
(4) String [] names = {"jalapeno", "anaheim", "serrano "}
(5) int coords [] [] = new int [12] [12];
4.1.3 attributes and methods of arrays
An array is an object because it has attributes and methods, such as the length attribute.
4.2 Enumeration
Enumeration is an interface that provides some standard methods for accessing elements in it. These methods include:
(1) hasMoreElement (): determines whether other elements exist.
(2) nextElement (): return the element. If no element is returned, a NoSuchElementException exception is thrown;
4.3 BitSet
BitSet is more convenient when it represents a group of Boolean values. It can access bits without using the bitwise operator:
BitSet bits = new BitSet (4 );
Defines a bit set with a length of 4 bits. We can use at least the following methods to operate these bits:
(1) bits. set (index): set the second index bit to 1;
(2) bits. clear (index): Set the index bit to 0;
(3) bits. get (index): returns the value of the second index bit;
(4) bits. size (): returns the length of the bit set;
(5) xor (BitSet set): perform Xor operations with the specified bit set;
(6) for other operations, see the java. util package.
4.4 Vector
The concept of Vector is similar to that of Array, but the length of Array cannot automatically increase, but the length of Vector can automatically increase. When the length of Vector is insufficient, it will automatically increase, you can also specify the length of each automatic increase, for example:
Vector v = new Vector (20, 5 );
Indicates creating a Vector object. The initial length is 20, which can accommodate 20 elements. If the number of elements exceeds 20, the size of five elements is increased at a time, that is, the first increase is 25, and the second increase is 30.
For the methods in Vector, see java. util package.
4.5 Stack
Stack is a typical data structure that adopts the principle of advanced and backward release. It has several important methods:
(1) pop;
(2) push (Object );
(3) empty (): clear

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.