Java basic programming structure (5)

Source: Internet
Author: User
(9) large values
This stuff is generally not very commonly used, so let's simply put it here.
First, it must be used more than 32 bits. It is two useful classes included in the Java. Math package.
Biginteger and bigdecimal

Note that they cannot use-+/* = and need to use add (); subtract ();
Multiply ();
Divide ()
MoD ();
Compareto ();
Valueof ();

(10) array

Initialization
Int A [] = new int [20];
Int C []; // declare array variable
C = new int [12]; // create Array
For (INT I = 0; I <A. length; I ++)
A [I] = I;
For (int I:)
System. Out. println (I );

You can declare an array variable either
Int [] A; or
Int A [];

1. "for each" Loop

For (INT element:)
System. Out. println (element );
The above is equivalent to the following
For (INT I = 0; I <A. length; I ++)
System. Out. println (A [I]);

2. array initializes and anonymous Arrays
Int smallints [] = {1, 2, 3 };
String [] cardinals = {"one", "two", "three "};
String [] ordinals = {"first", "second", "third "};

Anonymous Array
New int [] {12, 23, 23, 23}
It seems useless.

3. Copy an array
System. arraycopy (from, fromindex, to, toindex, count );
Example:
Int [] smallprimes = {2, 3, 5, 7, 11, 13 };
Int [] luckynumbers = {1001,100 2, 1003,100 4, 1005,100 6, 1007 };
System. arraycopy (smallprimes, 2, luckynumbers, 3, 4 );
For (INT I = 0; I <luckynumbers. length; I ++)
System. Out. println (I + ":" + luckynumbers [I]);

Result:
0: 1001
1:1002
2: 1003
3:5
4: 7
5: 11
6:13

4. Command Line Parameters
Public static void main (string [] ARGs ){

For (INT I = 0; I <args. length; I ++)
System. Out. println (ARGs [I]);

}

5. array sorting
(Math. Random () * n; // generate a random number ranging from 0 to n-1.
Random () generates 0-1 (not including 1 floating point numbers)

Arrays. Sort (result); // sort

6. Multi-dimensional array

Balance = new double [nyears] [nrates];
// You cannot use the array until you new and initialize it

Int [] [] magicsquare =
{
{16, 3, 2, 13, 23 },
{5, 10, 11, 8, 3 },
{9, 6, 7, 12, 67 },
{4, 15, 14, 1, 44}
}; // Note: no "new"

7. Irregular Array
The length of each line is also different.
Example of initialization:
Int [] [] Odds = new int [NMA + 1] [];
For (INT I = 0; I <= NMA; n ++)
{
Odds [N] = new int [n + 1];
}
For (INT n = 0; n <odds. Length (); N ++)
{
For (int K = 0; k <odds [N]. Length (); k ++)
{
Odds [N] [k] = lotteryodds;
}
}

8. Vector
Similar to an array, but the size does not need to be declared
Can contain different object types

Initialization:
Vector VEC = new vector ();

Some useful methods:
Isempty ()
Indexof (Object Arg)
Contains (Object Arg)

Example:
Vector VEC = new vector ();
VEC. addelement ("one ");
VEC. addelement ("two ");
VEC. addelement ("three ");
Enumeration E = Vec. Elements ();
While (E. hasmoreelements ()){
System. Out. println ("element =" + E. nextelement ());
}
Result:
Element = one
Element = two
Element = three

9. Collection
Constructor ()
Creates a collection with no elements
Size ()
Number of elements in the collection
Boolean add ()
Add a new pointer/element at the end of the collection
Returns true is the collection is modified.
Iterator ()
Returns an iterator object
Hasnext ()-returns true if more elements
Next ()-returns the next element
Remove ()-removes element returned by previous next () call.

Example
It = strings. iterator ();
While (it. hasnext ()){
It. Next (); // get pointer to ELEM
It. Remove (); // remove the above ELEM
}

System. Out. println ("Size:" + strings. Size ());

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.