Introduction of today's content
U Random
U array
Chapter 1th Random
1.1 Generating integer random numbers
1.1.1 Random Steps to use
What should we do if we want to generate random numbers of 1~100 (including 1 and 100)? We don't need to write the algorithm ourselves, because Java has provided us with classes that produce random numbers---random:
Role:
– Used to generate a random number
? Use steps (similar to scanner)
– Guide Pack
? Import Java.util.Random;
– Create Objects
? Random r = new Random ();
– Get random numbers
? int number = R.nextint (10);
? The resulting data is between 0 and 10, including 0, not including 10.
? The 10 inside the parentheses is changeable, and if it's 100, that's 0-100 data.
1.1.2 Case Code One:
Package Com.itheima;
Import Java.util.Random;
/*
* Random: Used to generate random numbers
*
* Steps to use:
* A: Guide Package
* Import Java.util.Random
* B: Create object
* Random r = new Random ();
* C: Get random numbers
* int number = R.nextint (10);
* Get a random number between 0-10, including 0, not including 10
*
* Requirements: How to get to a random number between 1-100?
*/
public class Randomdemo {
public static void Main (string[] args) {
Creating objects
Random r = new Random ();
for (int x = 0; x < n × x + +) {
Get random number
int number = R.nextint (10);
Output random number
System.out.println ("Number:" + number);
}
System.out.println ("--------------------");
How do I get to a random number between 1-100?
int i = r.nextint (100) + 1;
System.out.println ("I:" + i);
}
}
1.2 Guess the number of small game case:
1.2.1 System generates a random number between 1-100, please guess how much this data is.
1.2.2 Case Code Two:
Package Com.itheima;
Import Java.util.Random;
Import Java.util.Scanner;
/*
* Guess the number of small game cases
* The system generates a random number between 1-100, please guess how much this data is.
Analysis
* A: The system generates a random number between 1-100.
* int number = R.nextint (100) + 1;
* B: Keyboard input The data we want to guess
* Implemented with scanner
* C: Compare the two data (with an if statement)
* Big: Give a hint.
* Small: Give a hint.
* Guessed: Give a hint, congratulations, guess.
* D: Guess the data multiple times, and we don't know how many times to guess, how to do?
* while (true) {The contents of the loop}
*/
public class Randomtest {
public static void Main (string[] args) {
The system produces a random number between 1-100.
Random r = new Random ();
int number = R.nextint (100) + 1;
while (true) {
Keyboard input The data we want to guess
Scanner sc = new Scanner (system.in);
System.out.println ("Please enter the number you want to guess (1-100):");
int guessnumber = Sc.nextint ();
Compare the two data (with an if statement)
if (Guessnumber > number) {
System.out.println ("You guessed the data" + Guessnumber + "big up");
} else if (Guessnumber < number) {
System.out.println ("You guessed the data" + Guessnumber + "small");
} else {
System.out.println ("Congratulations, guessed it");
Break
}
}
}
}
2nd Chapter Array
2.1 Overview of arrays
Requirement: Now we need to count the salary of a company's employees, such as calculating average wages, finding the highest wage, etc. Assuming that the company has 80 employees, using the knowledge previously learned, the program first needs to declare 80 variables to remember each employee's salary, and then in the operation, this will be very troublesome. To solve this problem, Java provides an array for us to use.
So what exactly is an array? What are the characteristics? Through the above analysis: we can get the following two words:
An array is something (a container) that stores multiple variables (elements)
The data types of the multiple variables must be consistent
2.2 Defining formats for arrays
2.2.1 Array Concept
An array is a container that stores multiple elements of the same data type.
Arrays can store either the base data type or the reference data type.
2.2.2 The definition format of an array
Format 1: Data type [] array name;
Format 2: Data type array name [];
Note: These two definitions are done, and there are no element values in the array.
2.3 Initialization of the array
Overview of 2.3.1 Array initialization:
Arrays in Java must be initialized before they can be used.
Initialize: Allocates the memory space for array elements in arrays and assigns values to each array element.
2.3.2 How arrays are initialized
2.3.2.1 Dynamic Initialization: Specifies only the array length when initializing, and the system assigns the initial value
Format: data type [] Array name = new data type [array length];
The length of the array is actually the number of elements in the array.
Example:
int[] arr = new INT[3];
Explanation: An array of type int is defined, which can hold a value of 3 int type.
2.3.2.2 Case Code Three:
Package com.itheima_01;
/*
* Array: A container that stores multiple elements of the same data type.
*
* Definition Format:
* A: Data type [] array name;
* B: Data type array name [];
For example
* a:int[] A; Defines an array of type int, the array name is a
* B:int a[]; Defines a variable of type int, the variable name is an array of a
*
* Array Initialization:
* A: The so-called initialization is an array that opens up memory space and assigns the initial value to each element in the array
* B: We have two ways of initializing an array
* A: Dynamic initialization only specifies the length, the system gives the initialization value
* B: Static initialization gives the initialization value, the system determines the length of the
*
* Dynamic initialization:
* data type [] Array name = new data type [array length];
*/
public class Arraydemo {
public static void Main (string[] args) {
data type [] Array name = new data type [array length];
int[] arr = new INT[3];
/*
Left
* Int: Indicates that the data type of an element in an array is of type int
* []: Description This is an array
* Arr: is the name of the array
Right
* NEW: Allocate memory space for array
* Int: Indicates that the data type of an element in an array is of type int
* []: Description This is an array
* 3: The length of the array is actually the number of elements in the array
*/
}
}
2.3.2.3 Static Initialization: Specifies the initial value of each array element when initialized, and the system determines the array length
2.3.2.4 Case Code Four:
Package com.itheima_01;
/*
* Static initialization of the format:
* data type [] Array name = new data type []{element 1, element 2,...};
*
* Simplified Format:
* data type [] Array name = {element 1, element 2,...};
*
For example
* int[] arr = new int[]{1,2,3};
*
* After simplification:
* int[] arr = {A-i};
*/
public class ArrayDemo2 {
public static void Main (string[] args) {
Defining arrays
Int[] arr = {A-i};
Output array names and elements
System.out.println (arr);
System.out.println (Arr[0]);
System.out.println (arr[1]);
System.out.println (arr[2]);
}
}
2.4 Memory allocations for arrays
2.4.1 JVM Memory Partitioning
Java programs need to allocate space in memory at run time. In order to improve the efficiency of computing, space is divided into different regions, because each region has a specific way of processing data and memory management.
Stack store local Variables
Heap storage new out of things
Method area (object-oriented advanced speaking)
Local method area (and system-dependent)
Register (for CPU use)
2.4. Memory graph of 21 arrays
Defines an array, outputting array names and elements. Then assign values to the elements in the array, and output the array names and elements again
2.4.2.1 Case Code Five:
Package com.itheima_01;
/*
* Requirements: Define an array, output array names and elements. It then assigns values to the elements in the array, outputting the array names and elements again.
*/
public class Arraytest {
public static void Main (string[] args) {
Define an array
int[] arr = new INT[3];
Output array names and elements
System.out.println (arr);
System.out.println (Arr[0]);
System.out.println (arr[1]);
System.out.println (arr[2]);
Assigning a value to an element in an array
ARR[0] = 100;
ARR[2] = 200;
Output array name and element again
System.out.println (arr);
System.out.println (Arr[0]);
System.out.println (arr[1]);
System.out.println (arr[2]);
}
}
2.4.2.2 Code Memory Plots:
2.4. Memory graph of 32 arrays
Defines two arrays, outputting array names and elements, respectively. Then assign values to the elements in the array, and then output the array names and elements separately.
2.4.3.1 Case Code VI:
Package com.itheima_01;
/*
* Requirements: Define two arrays, outputting array names and elements, respectively. Then assign values to the elements in the array, and then output the array names and elements separately.
*/
public class ArrayTest2 {
public static void Main (string[] args) {
Define two arrays
int[] arr = new int[2];
int[] arr2 = new Int[3];
Output array names and elements, respectively
System.out.println (arr);
System.out.println (Arr[0]);
System.out.println (arr[1]);
System.out.println (ARR2);
System.out.println (Arr2[0]);
System.out.println (arr2[1]);
System.out.println (arr2[2]);
Then assign values to the elements in the array, respectively.
ARR[1] = 100;
ARR2[0] = 200;
ARR2[2] = 300;
Output array name and element again
System.out.println (arr);
System.out.println (Arr[0]);
System.out.println (arr[1]);
System.out.println (ARR2);
System.out.println (Arr2[0]);
System.out.println (arr2[1]);
System.out.println (arr2[2]);
}
}
2.4.3.2 Code Memory Plots:
2.4.42 Arrays of memory graphs pointing to the same address
Define two arrays, define an array, assign values, and output. The second array is then defined to assign the address of the first array to the second array. Then assign a value to the second array and output the names and elements of the two arrays again
2.4.4.1 Case Code VII:
/*
* Requirements: Define two arrays, first define an array, assign, output. The second array is then defined to assign the address of the first array to the second array.
* Then assign a value to the second array and output the names and elements of the two arrays again.
*/
public class ArrayTest3 {
public static void Main (string[] args) {
First, define an array, assign the value, output
int[] arr = new INT[3];
ARR[0] = 100;
ARR[1] = 200;
ARR[2] = 300;
System.out.println (arr);
System.out.println (Arr[0]);
System.out.println (arr[1]);
System.out.println (arr[2]);
The second array is then defined to assign the address of the first array to the second array
int[] arr2 = arr;
Then assign a value to the second array
Arr2[0] = 111;
ARR2[1] = 222;
ARR2[2] = 333;
Output the names and elements of two arrays again
System.out.println (arr);
System.out.println (Arr[0]);
System.out.println (arr[1]);
System.out.println (arr[2]);
System.out.println (ARR2);
System.out.println (Arr2[0]);
System.out.println (arr2[1]);
System.out.println (arr2[2]);
}
}
2.4.4.2 Code Memory Plots:
2.5 Use of arrays
2.5.1 element access to an array
2.5.1.1 Case Code Eight:
Package com.itheima_01;
/*
* Array: A container that stores multiple elements of the same data type.
*
* Definition Format:
* A: Data type [] array name;
* B: Data type array name [];
For example
* a:int[] A; Defines an array of type int, the array name is a
* B:int a[]; Defines a variable of type int, the variable name is an array of a
*
* Array Initialization:
* A: The so-called initialization is an array that opens up memory space and assigns the initial value to each element in the array
* B: We have two ways of initializing an array
* A: Dynamic initialization only specifies the length, the system gives the initialization value
* B: Static initialization gives the initialization value, the system determines the length of the
*
* Dynamic initialization:
* data type [] Array name = new data type [array length];
*/
public class Arraydemo {
public static void Main (string[] args) {
data type [] Array name = new data type [array length];
int[] arr = new INT[3];
/*
Left
* Int: Indicates that the data type of an element in an array is of type int
* []: Description This is an array
* Arr: is the name of the array
Right
* NEW: Allocate memory space for array
* Int: Indicates that the data type of an element in an array is of type int
* []: Description This is an array
* 3: The length of the array is actually the number of elements in the array
*/`
System.out.println (arr); [[Email protected], address value
We get the address value is meaningless, I want the data value in the array, what should I do?
Don't worry, Java has helped you think about it.
In fact, each element in the array is numbered, the number is starting from 0, the largest number is the length of the array-1
With the array name and number, we can get the element of the specified number in the array.
The professional term for this number: index
Format: array name [number]--array name [index]
System.out.println (Arr[0]);
System.out.println (arr[1]);
System.out.println (arr[2]);
}
}
Two small problems with the 2.5.2 array
2.5.2.1 Case Code IX:
Package com.itheima_02;
/*
* Two common minor issues:
* A:java.lang.arrayindexoutofboundsexception
* Array out of bounds exception
* Cause: You are accessing an index element that does not exist.
* B:java.lang.nullpointerexception
* NULL pointer exception
* Cause: The array does not point to the heap memory data, you also use the array name to access the element.
* Why should we remember such a small problem?
* Programming not only writes out the code, but also solves the problem quickly when the problem arises.
*/
public class Arraydemo {
public static void Main (string[] args) {
Defining arrays
Int[] arr = {1, 2, 3};
System.out.println (Arr[3]);
Reference type: Class, interface, array
Constant: empty constant null, which is a value that can be assigned to a reference type
arr = null;
System.out.println (arr[1]);
}
}
2.6 One-dimensional array exercises
2.6.11-dimensional Array traversal
2.6.1.1 Case Code Ten:
Package com.itheima_03;
/*
* Requirements: Array traversal (sequentially outputting each element in the array)
* Gets the number of elements in the array: array name. length
*/
public class Arraytest {
public static void Main (string[] args) {
Defining arrays
Int[] arr = {11, 22, 33, 44, 55};
Original procedure
System.out.println (Arr[0]);
System.out.println (arr[1]);
System.out.println (arr[2]);
System.out.println (Arr[3]);
System.out.println (Arr[4]);
System.out.println ("--------------------");
Improved with For loop
for (int x = 0; x < 5; × x + +) {
System.out.println (Arr[x]);
}
System.out.println ("--------------------");
To solve the problem of counting the number of elements in an array, the array provides a property: length
Used to get the length of an array
Format: array name. length
SYSTEM.OUT.PRINTLN ("Array of total:" +arr.length+ ");
System.out.println ("--------------------");
for (int x=0; x<arr.length; x + +) {
System.out.println (Arr[x]);
}
}
}
2.6.2 array operation gets the most value:
2.6.2.1 Case Code 11:
Package com.itheima_03;
/*
* Requirements: Array gets the maximum value (gets the minimum value in the array)
*/
public class ArrayTest2 {
public static void Main (string[] args) {
Defining arrays
Int[] arr = {12,98,45,73,60};
Defining a referential
int max = arr[0];
Iterate through the array, get all the elements except 0, and compare
for (int x=1; x<arr.length; x + +) {
if (Arr[x] > max) {
max = arr[x];
}
}
System.out.println ("The maximum value in the array is:" +max);
}
}
2.7 Two-dimensional arrays
2.7.12-dimensional Array overview
Our Black Horse Programmer's Java Basic class has a lot of students in each class, so we can use arrays to store, and we have a lot of Java basic classes. This should also be stored in an array. How to represent such data? Java provides a two-dimensional array for our use.
This shows that the two-dimensional array is actually an array of one-dimensional arrays of elements.
2.7.22-D array format
Defining formats
data type [] array name;
Data type array name []; Not recommended
data type [] array name []; Not recommended
Initialization mode
data type [] Variable name = new data type [m][n];
data type [] Variable name = new data type [][]{{element ...},{element ...},{element ...}};
Simplified version format: data type [] Variable name = {{element ...},{element ...},{element ...}};
2.7.2.1 Case Code 12:
Package com.itheima_04;
/*
* Two-dimensional array: An array of elements that are one-dimensional arrays.
*
* Definition Format:
* A: Data type [] [] array name;
* B: Data type array name []; Not recommended
* C: Data type [] array name []; Not recommended
*
* How do I initialize it?
* A: Dynamic initialization
* data type [] Array name = new data type [m][n];
* M indicates how many one-dimensional arrays are in this two-dimensional array
* n indicates how many elements of each one-dimensional array
* B: Static initialization
* data type [] Array name = new data type [][]{{element ...},{element ...},{element ...},...};
* Simplified Format:
* data type [] Array name = {{element ...},{element ...}},{element ...},...};
*/
public class Arrayarraydemo {
public static void Main (string[] args) {
data type [] Array name = {{element ...},{element ...}},{element ...},...};
Int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println (arr); [[Email protected]
System.out.println (arr.length); The number of one-dimensional arrays in a two-dimensional array
System.out.println (arr[0]);//[Email protected]
System.out.println (arr[0].length);
System.out.println (arr[1]);//[Email protected]
System.out.println (arr[2]);//[Email protected]
How do I get to an element of a two-dimensional array?
System.out.println (Arr[0][0]);
System.out.println (arr[1][1]);
System.out.println (Arr[2][0]);
}
}
2.7. Traversal of 32-dimensional arrays
Traversal thought: First use loops to iterate through each one-dimensional array stored in a two-dimensional array, and then iterate through the elements in that one-dimensional array using loops for each of the traversed one-dimensional arrays
2.7.3.1 Case Code 13:
Package com.itheima_04;
/*
* Two-dimensional array traversal
*
* SYSTEM.OUT.PRINTLN (): Output content and wrap
* System.out.print (): Output content
* SYSTEM.OUT.PRINTLN (): Line break
*/
public class Arrayarraytest {
public static void Main (string[] args) {
Defining a two-dimensional array
Int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
One-dimensional array names in two-dimensional arrays: two-dimensional array names [index]
Arr[0] is actually the name of the first one-dimensional array in a two-dimensional array
ARR[1] is actually the name of the second one-dimensional array in a two-dimensional array
ARR[2] is actually the name of the third one-dimensional array in a two-dimensional array
for (int x = 0; x < arr[0].length; × x + +) {
System.out.println (Arr[0][x]);
// }
System.out.println ("Hello");
SYSTEM.OUT.PRINTLN ("World");
System.out.print ("Hello");
System.out.print ("World");
/*
Elements of the first one-dimensional array
for (int x = 0; x < arr[0].length; × x + +) {
System.out.print (Arr[0][x] + "");
}
System.out.println ();
Elements of the second one-dimensional array
for (int x = 0; x < arr[1].length; × x + +) {
System.out.print (Arr[1][x] + "");
}
System.out.println ();
Elements of the third one-dimensional array
for (int x = 0; x < arr[2].length; × x + +) {
System.out.print (Arr[2][x] + "");
}
System.out.println ();
*/
for (int y=0; y<3; y++) {
for (int x = 0; x < arr[y].length; × x + +) {
System.out.print (Arr[y][x] + "");
// }
System.out.println ();
// }
for (int y=0; y<arr.length; y++) {
for (int x = 0; x < arr[y].length; × x + +) {
System.out.print (Arr[y][x] + "");
}
System.out.println ();
}
}
}
Java Learning-the fourth day of basic knowledge--Random, array