JAVA_SE_ notes finishing (BASIC)

Java foundation four

1 , the jump control statements

break interrupt

break usage scenarios:

In the selection of the switch structure;

Loop statement;

Leaving the scene of existence use does not make sense.

break role:

Out of single-cycle

The multilayer loop out: break with a label;

 

continue continue

scenes to be used:

In the loop statement;

Leaving the scene of existence use does not make sense.

effect:

Monolayer continue

 

Multilayer continue

 

return return

return key is not to jump out of the loop, a more common feature is the end of a method, a method that is quit. Jump to the upper method call. In using this method there will be explained in detail.

 

java  method

1. Why should Method:

There are many things in the code are the same, so there is no need to repeat the code to fill. These same program code can be defined as a method. So every shot simply call the method, no need to paste a lot of code.

2. Method Description:

In many languages ​​which have defined functions

Functions are called methods in Java

3, format:

Modifier return type method name (parameter type parameter name 1, parameter type parameter name 2 ...) {

Method body;

return Return value;

     }

Methods Format explanation:

Modifiers: The state modified method; currently only need public static

Return Value: returns the value used to define the type, byte short int long ·····

Method name: a name, the method call to the method.

parameter list:

Parameter Type: defining a method call, the incoming data type

Parameter name: is a variable that receives incoming calls when the method parameters.

The method body

Completion code functions

return: Returns the value of the end of the process and method specified

Returns: return the result of the program is brought back, returned to the caller.

 

4, Attention:

The method is not called, is not executed.

Is the same level and the relationship between the method of the method, not nested definitions.

The method defined time parameter intervals by commas.

Method is invoked without passing data types

If the method has a clear return values, there must be a return value back.

5, no return value of the method definition

Modifier void method name (parameter type parameter name 1, parameter type parameter name 2 ...) {

Method body;

// If write return, followed immediately ";" If you do not write, is also possible.

// return ;

    }

 

6, heavy

Overview: In the same class, there is a method to allow more than one of the same name, as long as they have different number of parameters or parameter types can.

Heavy-duty features:

Regardless of the return type, just look at the method name and parameter list

When called, the virtual machine to distinguish between different parameters by the same method on the list of

For example: sum

 

java  array

1 Overview

An array of a plurality of the same data type is a set of storage elements. It can also be seen as a container.

Arrays can either be stored basic data types, data types may be stored reference.

2, the array format definition

Format 1, data type [] array name;

2 format, the data type array name [];

Note: these two definitions done, the array is no element values.

3, initialize the array elements

Overview array initialization:

Java in the array must be initialized before use.

The so-called initialization: that allocates memory for the array elements in the array, and assigned to each array element.

Array initialization method:

Static initialization: Specifies initial value of each array element initialization, the system determined by the length of the array.

Dynamic Initialization: Only specified array length during initialization, an initial value is assigned by the system as an array.

Dynamic initialization

Only the specified array length during initialization, an initial value is assigned by the system as an array.

format:

Data Type [] = new Array name Data type [array length];

In fact, the length of the array is the number of elements in the array.

For example:

int[] array = new int[3];

Explanation: defines an int type array, this array can store three types of data int.

 

4, memory allocation:

Java program is running, you need to allocate space in memory. In order to improve operational efficiency, it has been divided into different spatial regions, because each region has a specific way of processing data and memory management.

Local variable stack storage

Memory heap new out of something

Method region (object-oriented portion talk)

  Register (for CPU usage)

 

Local variables: the definition of variables in the process or method is called local variable declarations

Every new thing has come out of the address value

Each variable has a default value

byte,short,int,long,0

float,double, 0.0

char ‘\u0000’

Boolean false

Reference type null

It becomes garbage after use, but did not recover immediately, the garbage collector will reclaim free time.

5, the initial static array: Specifies initial value of each array element initialization, the system determined by the length of the array.

Format: Data Type [] = new Array Name Data Type [] {element 1, element 2, element ...};

For example:

int[] arr = new int[]{1, 2, 3};

Explanation: defines an int type array that can be stored in the value 3 int 'type, and assigned to 1,2,3,

In fact, such an approach there is a simplified wording

int[] arr = {1, 2, 3};

 

6, an array of property

The length of the array length of an array, an array of objects belonging to

7, the array may encounter an exception

ArrayIndexOutOfBoundsException array subscript out of bounds

NullPointerException null pointer exception

Two-dimensional array

1, two-dimensional array Overview

In fact, two-dimensional array in fact, each array element is a one-dimensional array of arrays

2, a format defined

Data Type [] [] = new variable name Data type [m] [n]

m represents the number of the two-dimensional array of one-dimensional array

n represents a number of elements of each one-dimensional array.

 

For example:

int[][] array = new int[3][2];

It defines a two-dimensional array array

This two-dimensional array 3 has a three-dimensional array, name array [0], array [1], array [2]

2 are each a one-dimensional array has two elements, may be obtained by array [m] [n]. array [0] [1]

Retrieves m + 1-n + 1-th one-dimensional array of elements.

3, two Definition Format

Data Type [] [] = new variable name Data type [m] [];

m represents the number of the two-dimensional array of one-dimensional arrays

This time does not directly give the number of elements of the one-dimensional array. Dynamically given.

 

For example:

int[][] array = new int[3][];

array[0] = new int[2];

array[1] = new int[5];

array[2] = new int[4];

4, three Definition Format 

Static initialization

Data Type [] [] = new variable name Data type [] [] {

{Element 1, element 2, ....},

{element…},

{element..}

};

A simplified version of the format:

Data Type [] [] variable name = {

{Element 1, element 2, ....},

{element…},

{element..}

};

 

For example:

int[][] arr = {

{1, 2, 3},

{3, 6},

{123, 345, 678}

};

Sort by:

http://blog.csdn.net/hguisu/article/details/7776068 (eight kinds of ordering, self-learning)

Bubble Sort:

A basic overview: sequentially comparing adjacent two numbers, the numbers are placed ahead of large numbers on the back. I.e. the first pass: First, a first comparator and a second number, the decimal place before, after the release of large numbers. Then compare the second number and a third number, the decimal place before and after the release of large numbers, so continues until the last two numbers compare, before the decimal place, after the release of large numbers. Thus ended the first trip, the maximum number into the final. In the second pass: still starts from a first comparison of the number (probably due to the exchange as the number 2 and number 3, so that the number of the first number not less than 2), before the decimal place, large numbers after the release, has been compared to the penultimate number (the last first position is already the largest), the end of the second trip, get a new maximum number in the penultimate position (in fact, is the first in a whole series of numbers the second largest number). Under such circumstances, the process is repeated until the final completion of the sort.

for(int i = 0; i < array.length; i++) {

for(int j = 0; j < array.length - i - 1; j++) {

if(array[j] < array[j + 1]) {

int temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

}

}

}

Insertion sort:

Each step will be a sort of record, according to the size of the front insert key values ​​that have been sorted in place on the file, until all inserted last.

public static void insertsort(int arr[]){                

        for(int i = 1;i < arr.length; i++){

// Note [0, i-1] are ordered. If the element to be inserted arr [i-1] is no longer greater than compared to the previous element [i-1], and vice versa into the if statement

            if(arr[i-1] > arr[i]){

                int temp = arr[i];

                int j;

                for(j = i-1; j >= 0 && arr[j] > temp; j --){                

                        arr [j + 1] = arr [j]; // temp large than or equal to the backward movement of all the elements of a position            

                }

                arr [j + 1] = temp; // Sort the elements to be inserted into the vacated position temp (j + 1)

            }            

        }

        

    }

Insertion and bubble sort of difference:

For example: 45123422

After insertion sort, the sort will have performed a trip: 12453422

For After bubble sort, sort has performed a trip: 12452342   

 

Pascal's Triangle:

 

int triangle [] [] = new int [20] []; // create a two-dimensional array

// traverse the two-dimensional array of the first layer

for (int i = 0; i < triangle.length; i++) {

    triangle [i] = new int [i + 1]; // initialize the size of the second set of layers

    // traverse the second set of layers

    for(int j=0;j<=i;j++){

        // array elements on both sides of the value of 1

        if(i==0||j==0||j==i){

            triangle[i][j]=1;

        } Else {// other values ​​calculated by the formula

            triangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1];

        }

        System.out.print (triangle [i] [j] + "\ t"); // array element output

    }

    System.out.println();               //换行

}

 

! Factorial: n == n * (n-1);!

public static int jc (int n) {

if(n ==1 ){

return 1;

}else{

return n * jc(n-1);

}

}

 

 

Fibonacci number: F [n] = F [n-1] + F [n-2] (n> = 2, F [1] = 1, F [2] = 1)

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368

 

public static int fibonacci(int n){

    if(n<=1)

return 1;

   return fibonacci(n-1)+fibonacci(n-2);

    }

 

Sort by:

http://blog.csdn.net/hguisu/article/details/7776068 (eight kinds of ordering, self-learning)

Bubble Sort:

A basic overview: sequentially comparing adjacent two numbers, the numbers are placed ahead of large numbers on the back. I.e. the first pass: First, a first comparator and a second number, the decimal place before, after the release of large numbers. Then compare the second number and a third number, the decimal place before and after the release of large numbers, so continues until the last two numbers compare, before the decimal place, after the release of large numbers. Thus ended the first trip, the maximum number into the final. In the second pass: still starts from a first comparison of the number (probably due to the exchange as the number 2 and number 3, so that the number of the first number not less than 2), before the decimal place, large numbers after the release, has been compared to the penultimate number (the last first position is already the largest), the end of the second trip, get a new maximum number in the penultimate position (in fact, is the first in a whole series of numbers the second largest number). Under such circumstances, the process is repeated until the final completion of the sort.

for(int i = 0; i < array.length; i++) {

for(int j = 0; j < array.length - i - 1; j++) {

if(array[j] < array[j + 1]) {

int temp = array[j];

array[j] = array[j + 1];

array[j + 1] = temp;

}

}

}

Insertion sort:

Each step will be a sort of record, according to the size of the front insert key values ​​that have been sorted in place on the file, until all inserted last.

public static void insertsort(int arr[]){                

        for(int i = 1;i < arr.length; i++){

// Note [0, i-1] are ordered. If the element to be inserted arr [i-1] is no longer greater than compared to the previous element [i-1], and vice versa into the if statement

            if(arr[i-1] > arr[i]){

                int temp = arr[i];

                int j;

                for(j = i-1; j >= 0 && arr[j] > temp; j --){                

                        arr [j + 1] = arr [j]; // temp large than or equal to the backward movement of all the elements of a position            

                }

                arr [j + 1] = temp; // Sort the elements to be inserted into the vacated position temp (j + 1)

            }            

        }

        

    }

Insertion and bubble sort of difference:

For example: 45123422

After insertion sort, the sort will have performed a trip: 12453422

For After bubble sort, sort has performed a trip: 12452342   

 

Pascal's Triangle:

 

int triangle [] [] = new int [20] []; // create a two-dimensional array

// traverse the two-dimensional array of the first layer

for (int i = 0; i < triangle.length; i++) {

    triangle [i] = new int [i + 1]; // initialize the size of the second set of layers

    // traverse the second set of layers

    for(int j=0;j<=i;j++){

        // array elements on both sides of the value of 1

        if(i==0||j==0||j==i){

            triangle[i][j]=1;

        } Else {// other values ​​calculated by the formula

            triangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1];

        }

        System.out.print (triangle [i] [j] + "\ t"); // array element output

    }

    System.out.println();               //换行

}

 

! Factorial: n == n * (n-1);!

public static int jc (int n) {

if(n ==1 ){

return 1;

}else{

return n * jc(n-1);

}

}

 

 

Fibonacci number: F [n] = F [n-1] + F [n-2] (n> = 2, F [1] = 1, F [2] = 1)

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368

 

public static int fibonacci(int n){

    if(n<=1)

return 1;

   return fibonacci(n-1)+fibonacci(n-2);

    }

 

Guess you like

Origin www.cnblogs.com/songliuzhan/p/12624128.html