Check for missing vacancies (java)
1. Anonymous array:
Java provides a simplified writing mode for creating array objects and granting them with initial values.
Int [] a = {1, 2, 5, 4 };
Reinitialize an array without creating new variables.
Int B [] = {1, 2, 4 };
A = B;
Simplified: a = new int [] {1, 2, 3, 4 };
For details, see the code:
Public class Main {public static void main (String [] args) {// anonymous array definition method new int [] {,} int a [] =, 1, 9}; int B []; B = a; System. out. println (B [1]); B = new int [] {,}; // This is a simplified System of the above three lines of code. out. println (B [1]) ;}}
Ii. Copy an array:
Int ary [] = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
Copy all elements of array ary to array B
Int B [] = Arrays. copyOf (ary, ary. length );
Change the size of array ary
Ary = Arrays. copyOf (ary, 2 * ary. length );
Import java. lang. reflect. array; import java. lang. reflect. constructor; import java. SQL. date; import java. util. arrays; public class Main {public static void main (String [] args) {int ary [] = {1, 2, 4, 5, 6, 7, 8, 9, 10}; // System. out. println (ary. length); int B [] = Arrays. copyOf (ary, ary. length); // copy the elements in ary to bSystem. out. println (B [1]); System. out. println (B. length); // The number of elements in B is the same as that in the ary array. ary = Arrays. copyOf (ary, 2 * ary. length); // increase the size of the ary array by 2 times. out. println (ary. length );}}
Iii. array sorting:
Quickly sort the values of elements in array
Arrays. sort ();
Code:
import java.util.Arrays;public class Main{public static void main(String[] args){int ary[] = {8,4,56,6,7,21,1};Arrays.sort(ary);//for(int i = 0;i< ary.length;i++)for(int i : ary){//System.out.println("ary["+i+"] = "+ary[i]);System.out.println("ary["+i+"] = "+i);}}}
For (int I = 0; I
4. java random number generation method:
Apply the random method in the Math class
(Int) (Math. the random () * n) method randomly generates a random floating point number between 0 and 1 (including 0, excluding 1). Multiply n by this floating point number to obtain a random number between 0 and n-1.
Import java. util. arrays; public class Main {public static void main (String [] args) {int ary [] = {, 1}; Arrays. sort (ary); int ran = (int) (Math. random () * ary. length); System. out. println ("the random number generated is" + ran); System. out. println ("printed array elements" + ary [ran]);}
V. Relationship between classes:
1. use)
2. Aggregation (has)
3. Inheritance (is)
1. Dependency: If the method of a class operates on the object of another class, we will say that one class depends on another class.
PS: Minimize the number of dependent classes. If Class A does not know the existence of Class B, it will not care about any changes in Class B (changes in Class B will not cause any bugs in Class)
Terminology: Minimum Coupling between classes
2. Aggregation: Class A objects include objects of Class B.
3. Inheritance
Date class, whose object describes a time point
System. out. println (new Date (); // anonymous object
You can also apply a method to the newly created object. The Date class has a toString method that describes the string of the returned Date.
String s = new Date (). toString ();
In the preceding two examples, the construction object is only used once. If you want to use Date bir = new Date () multiple times ();
6. There is an important difference between objects:
For example, Date d; // defines an object variable d.
String s = d. toString ();
D is not an object. In fact, it does not reference an object. It is just an object variable, a name, and a code. The Date method cannot be applied to this variable.
Therefore, the variable d must be initialized first. There are two options:
1. initialize with a new constructed object
D = new Date ();
2. Let this variable reference an existing object
Date dir = new Date ();
D = bir; // now the two variables share an object.