Random class, array, and random class Array

Source: Internet
Author: User

Random class, array, and random class Array

Random Tool

Purpose: generate a random number.

Procedure:
1. Import package
Import package
Import java. util. Random;
2. Create a reference type variable
Data type variable name = new data type ();
Random rd = new Random ();
3. Call Functions
Variable name. Function
The value is a random integer in the range of [0, n). A Random integer that contains 0 but does not contain n is generated.
NextInt (n );
Rd. nextInt (n );

Random class code Demonstration:

1. Obtain the random number between 1 and 100 [1,100] ---> [] + 12 public static void main (String [] args) {3 // create reference type variable 4 Random rd = new Random (); 5 // call function 6 int num = rd. nextInt (100) + 1; 7 System. out. println ("num =" + num); 8}

Array:
Store and operate batch data
The value of the data stored in the array is changeable.
The Data Types of the stored batch data must be the same.

Array Definition Format:
Format 1: Data Type [] array name (Recommended Format)
Format 2: Data Type array name []; (recognizable)
Allocate memory space to the array and assign default values to the elements
Two Forms:
First: Dynamic initialization. The length of the array is defined while the array is defined (the maximum number of elements can be stored in the array). The default value of the array element is specified by the system.
Type 2: static initialization. The data to be stored is defined while the array is defined. The length of the array is specified by the system.
Dynamic initialization format:
Data Type [] array name = new data type [M];
Data Type: the data type of the element. The data types on both sides must be consistent.
[]: Array flag. One [] represents two [] Two-dimensional arrays.
Array name: the identifier can be used to obtain array elements.
M: The length of the array, that is, the maximum number of elements that can be stored.

Access array element format

Array name [index value] ----> array name [subscript] array name [Badge]
Index value starts from 0
The index value of the last element is n-1.

Static Initialization
Specify the value while defining the array. The length of the array is calculated by the system.
Syntax format:
Data Type [] array name = new data type [] {element value 1, element value 2 ,...};
Simplified format:
Data Type [] array name = {element value 1, element value 2 ,...};

Exception
Array out-of-bounds exception: java. lang. ArrayIndexOutOfBoundsException: 4
Null Pointer exception: java. lang. NullPointerException

Traverse the array:
Obtain array elements one by one
Automatically obtain the length of the array
Array name. length

Array traversal code Demonstration:
* Obtain the maximum number in the sequence 1, 3, 7, 4, 5, and 6.
* Define an array to store the preceding numerical sequence
* Int [] arr = {1, 3, 7, 4, 5, 6 };
* Idea:
* Define a temporary variable to store the value of element 0 first, and compare the value in the temporary variable with the value of the element in sequence. Once it is found that the value of an element is greater than the value in the temporary variable, the value in the element
* Assign a value to the temporary variable. from the beginning to the end, the Temporary Variable stores the maximum value.

1 public static void main (String [] args) {2 // 3 int [] arr = {,}; 4 5 int tmp = arr [0]; 6 7 for (int I = 1; I <arr. length; I ++) {8 // compare 9 if (tmp <arr [I]) {10 // assign 11 tmp = arr [I] once the conditions are met; // assign the element value to the temporary tmp12} 13} 14 15 System. out. println ("maximum value =" + tmp); 16}

Two-dimensional array:

Array element is an array of one-dimensional arrays
Definition Format:
Data Type [] [] array name; Recommended Format
Data Type [] array name [];
Data Type array name [] [];
Dynamic Initialization
Data Type [] [] array name = new data type [M] [N];
Data Type: the data type of the array element, consistent before and after
[]: Array flag, two [] represents two-dimensional array
Array name: the identifier can access array elements through array names.
M: the length of the two-dimensional array, that is, the number of one-dimensional arrays that constitute the two-dimensional array
N: number of elements in each one-dimensional array in a two-dimensional array
Static Initialization
Data Type [] [] array name = new data type [] [] {element value 1, element value 2 ,...}, {element value 1, element value 2 ,...}, {element value 1, element value 2 ,...},...};
Abbreviated format
Data Type [] [] array name ={{ element value 1, element value 2 ,...}, {element value 1, element value 2 ,...}, {element value 1, element value 2 ,...},...};
1 2 3
4 5 6
7 8 9
Int [] [] arr = {, 3}, {, 6}, {, 9 }}
Elements accessing a two-dimensional array
Array name [index value 1] [index value 2]
Length of a two-dimensional array
Array name. length
Obtains the length of an array in a two-dimensional array.
Array name [index value]. length

Two-dimensional array outer loop control row and inner loop control column

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.