Leak Check (Java)

Source: Internet
Author: User

First, an anonymous array:

Java provides a simplified form of writing that creates an array object and gives the initial value at the same time
Int[] A = {1,2,5,4};
Reinitialize an array without creating a new variable

int b[] = {1,2,3,4};
A = b;
Simplified: a = new int[]{1,2,3,4};

See the code:

public class Main{public static void Main (string[] args) {///anonymous array defined by new int[]{1,5,1,9} int a[] = {1,5,1,9};int b[];b = A; System.out.println (b[1]); b = New int[]{1,5,1,9};//This is a simplified System.out.println (b[1]) of the above three lines of code;}}

Second, array copy:

int ary[] = {1,2,3,4,5,6,7,8,9,10};

Copies all elements of an array ary to array b

int b[] = arrays.copyof (ary,ary.length);

Changing the size of an 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,3,4,5,6,7,8,9,10};//SYSTEM.OUT.PRINTLN ( ary.length); int b[] = arrays.copyof (ary,ary.length);//Copy the elements of Ary to BSystem.out.println (b[1]); System.out.println (b.length);//b the same number of elements and ary array ary = arrays.copyof (ary, 2*ary.length);// The size of the ARY array is larger by twice Times System.out.println (Ary.length);}}

three, array sorting:

Quickly sort the values of an element in a array

Arrays.sort (a);

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<ary.length;i++) {System.out.println (Ary[i]),}↓for (int i:ary) System.out.println (i);//both equivalent


Four, the Java random number generation way:


Apply the random method in the math class

(int) (Math.random () *n) method randomly generates a random floating-point number between 0 and 1 (containing 0, not 1), which is multiplied by the float to get the random numbers between 0 and n-1.

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); int ran = (int) (Math.random () *ary.length); System.out.println ("Generated random number is" +ran); System.out.println ("Printed array element" +ary[ran]);}}

v. Relationships between classes and classes:

1. Dependency (use a)
2. Aggregation (has a)
3. Inheritance (is a)
1. Dependency: If a class's methods manipulate objects of another class, then we say that one class depends on another class

PS: You should minimize the number of interdependent classes as much as possible, and if Class A does not know the existence of Class B, it will not care about any changes in class B (b changes will not cause any bugs in class A)
Terminology: Minimizing the degree of coupling between classes


2. Aggregation: Class A object contains object of Class B


3. Inheritance


Date class, whose object describes a point in time

System.out.println (New Date ());//Anonymous Object

You can also apply a method to the object you just created, and the date class has a ToString method that returns a string description of the date

String s = new Date (). toString ();
In both cases, the constructed object is used only once, if you want to use date Bir = new Date () multiple times;

Six, there is an important difference between objects and objects:

Example: Date d;//defines an object variable D

String s = d.tostring ();

D is not an object, actually there is no reference object, it is just an object variable, a name, a code, you cannot apply the date method to this variable

Therefore, you must first initialize the variable d, here are two options:
1. Class with the new constructed object.

D = new Date ();

2. Let this variable refer to an existing object

Date dir = new Date ();

D = bir;//Now two variables share an object


Leak Check (Java)

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.