DAY06-DAY07 Personal Summary

1, one-dimensional array 1, the definition of an array:

A collection of data, or an array that is used to represent a set of data, is called an array.

To define an array:

Int[] INTs;

Double[] Doubles

String[] Names

Each array must have a consistent data type, access to each of the data in the array needs to be accessed through the index of the array, that is, the array subscript, the array subscript range is: 0 to (n-1), and once the array is created, the length is immutable, the array belongs to the reference type of data

2, the Declaration of the array:

Data type variable name = variable Value

data type [] array variable name; (recommended)

Data type array variable name [];

3. Creating an array

Creation of one-dimensional arrays, using new to request memory space

Use the keyword New in Java to create an array object, specify the size of the array, and allocate space for the storage of the array elements

Format: Array variable name = new array element type [number of array elements];

Array = new INT[5];

4. Assigning values to arrays

Static initialization is the initialization of arrays at the same time that an array object is created.

There are two ways of static initialization:

1, int[] A = new Int[]{1, 2, 3}; The array is defined and created, and the elements of the initialized array are 1, 2, 3, and the length of the array is also limited to 3.

2, int[] A = {1, 2, 3}; Effect ditto

Note: Do not write int[] a = new int[3]{1,2,3};

Both of these methods do not need to define the length of the array, and the length of the array is determined by the number of elements at initialization time.

Dynamic initialization is the separation of the creation of arrays and the initialization of groups.

For example:

Int[] A = new int[3];

A[0] = 3; Assigning values to an array element by an element index (subscript)

a[1]=4;

a[2]=5;

5. Iterating through an array

The first way to iterate through an array:

The first way of traversing

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

System.out.print (array[i]+ "");

}

System.out.println ();

The second way of traversal

for (int num:array) {

System.out.print (num+ "");

}

6. Sorting and finding

1, Sequential search:

Array elements in the absence of order, in order to find the way to look for, is to find

The code is as follows:

Sequential Lookup

public static int Ordersort (int[] array,int key) {

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

if (array[i] = = key) {

return i;

}

}

return-1;

}

}

2, two-way search:

The array elements can be found in the order of the binary search method.

The code is as follows:

public static int Twosort (int[] nums,int num) {

int min = 0;//used to define the smallest subscript of an array

int max = nums.length-1;//to define the maximum subscript for an array

int mid = (Min+max)/2;//used to define subscripts in the middle of an array

do{

Always take the value of the middle of the array to compare with num if equal returns the subscript value

if (nums[mid] = = num) {

return mid;

}

if (Nums[mid] > num) {

max = mid-1;

}else{

Min = mid + 1;

}

Mid = (max+min)/2;

}while (min <= max);

return-1;

}

3. Find the maximum and minimum values of the array

The code is as follows:

Find the maximum and minimum number

public static void Mostsort (int[] nums) {

int max = nums[0];

int min = nums[0];

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

if (Max<nums[i]) {

max = Nums[i];

}

}

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

if (Min>nums[i]) {

min = nums[i];

}

}

System.out.println ("The largest number is:" +max);

System.out.println ("The smallest number is:" +min);

}

4. Bubble Sort:

5,8,1,7,6, 3

？？？？？ 8

？？？？ 7,8

1,3,5,6,7,8

Bubble sort, that is, each comparison, the large elements of the back, compared to the first time after the largest element to the array of the last

The second comparison, or from the beginning of the first element and the subsequent comparison, the remaining largest element is taken to the second-to-last array, the first round of comparison of the largest element does not participate in the comparison

In turn.

The code is as follows:

Bubble sort from small to large arrangement

public static void Bubblesort (int[] nums) {

int temp = 0;

Used to control the number of wheels that require a sort comparison

for (int i=0;i<=nums.length-2;i++) {

22 comparison

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

If the latter is less than the former will be exchanged

if (nums[j-1]> Nums[j]) {

temp = Nums[j];

NUMS[J] = nums[j-1];

NUMS[J-1] = temp;

}

}

}

}

5. Select sort

The array is 22 compared to the size once, sorted by the code as follows:

Select sorting from large to small

public static void Ordersort (int[] nums) {

int k = 0;

for (int i=0;i<=nums.length-2;i++) {

K = i+1;

The maximum number in the array is calculated by this for loop

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

and assigns the subscript of the largest number to K.

if (Nums[j]>nums[k]) {

K = J;

}

}

Judge K if not equal to I always will

if (k! = i) {

int temp = Nums[i];

Nums[i] = nums[k];

NUMS[K] = temp;

}

}

7. Variable parameters and two-dimensional arrays

1, what is the variable parameter,

The nature of a mutable parameter is still an array, and in the case of a method overload, you can use a variable parameter in cases where you need to pass different parameters, just define a method, and the code is as follows:

public static int getsum (int ... INTs) {

int sum = 0;

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

Sum + = Ints[i];

}

return sum;

}

2, the main method of understanding

In the main method, the following:

public static void Main (string[] args)

When the main method is executed, the external arguments passed to the main method

3. Array expert is the function and tool of array

Function:

Sort (); Sorts the incoming array in ascending order

Arrays.tostring (); The incoming array is output as a string

Use the following methods:

Int[] A = {8,9,10,11,1,2,3,4,5,6,7};

Arrays.sort (a);//Sort the array

System.out.println (Arrays.tostring (a));//print array in string form

Learn Java sixth to Seventh day summary