Study notes: array

Array

One-dimensional array

Dynamic initialization: the operation of array declaration and allocating space for array elements and assignment is carried out separately

int[] arr = new int[3];
arr[0] = 3;
arr[1] = 9;
arr[2] = 8;

Static initialization: When defining the array, allocate space and assign values ​​to the array elements.

int a[] = new int[]{
    
     3, 9, 8};
int[] a = {
    
    3,9,8};

One-dimensional array declaration method:
type var[] or type[] var;
for example:

   int a[];
   int[] a1;
   double  b[];
   Mydate[] c;  //对象数组

 After defining and using the operator new to allocate space, each element in the array can be referenced;
 The reference method of array elements: array name [array element subscript]
 The array element subscript can be an integer constant or an integer expression. Such as a[3], b[i], c[6*i];
 The subscript of array elements starts from 0; the legal subscript range of an array of length n: 0 —>n-1; such as int a[ ]=new int[3]; The array elements that can be referenced are a[0], a[1], a[2]
 Each array has an attribute length indicating its length, for example: a.length indicates the array a The length (number of elements)
 Once the array is initialized, its length is immutable

Default initialization of array elements

 Array is a reference type, and its elements are equivalent to member variables of the class. Therefore, once space is allocated for the array, each element in it is implicitly initialized in the same way as member variables. E.g:

public class Test {
    
    
public static void main(String argv[]){
    
    
int a[]= new int[5]; 
System.out.println(a[3]);	//a[3]的默认值为0
}
} 

Multidimensional Arrays

Format 1 (dynamic initialization): int[][] arr = new int[3][2];
defines a two-dimensional array named arr
. There are 3 one-dimensional arrays in the two-dimensional array, and
each one-dimensional array has 2
The names of the one- element one-dimensional arrays are arr[0], arr[1], arr[2]
. Assign a value of 78 to the first one-dimensional array 1 pin bit. The writing method is: arr[0][1] = 78;
Format 2 (dynamic initialization): int[][] arr = new int[3][];
There are 3 one-dimensional arrays in the two-dimensional array.
Each one-dimensional array is the default initialization value null (note: different from format 1)
The three one-dimensional arrays can be initialized separately
arr[0] = new int[3]; arr[1] = new int[1 ]; arr[2] = new int[2];
Note:
int[][]arr = new int[][3]; //Illegal
format 3 (static initialization): int[][] arr = new int[ ][]{ {3,8,2},{2,7},{9,0,1,6}};
define a two-dimensional array named arr,
each of which has three one-dimensional arrays The specific elements in
the one-dimensional array have also been initialized . The first one-dimensional array arr[0] = {3,8,2}; the
second one-dimensional array arr[1] = {2,7}; the
third one Dimensional array arr[2] = {9,0,1,6};
The length representation of the third one-dimensional array: arr[2].length;

Array sort

 Direct Insertion Sort, Half Insertion Sort, Shell Sort
 Exchange Sort
 Bubble Sort, Quick Sort (or Partition Exchange Sort)
 Select Sort
 Simple Select Sort, Heap Sort
 Merge Sort
 Cardinal Sort
Bubble Sort
Sort Idea:
Phase Compare two adjacent elements, exchange if necessary, and arrange the largest element at the end every time the loop is completed (such as sorting from small to large), and perform similar operations on other numbers in the next loop.

		int[] arr = new int[]{
    
    2,56,33,1,8,5,64,34};
		int temp = 0;
		for(int i = 0; i < arr.length - 1; i++){
    
    //外层循环是循环轮次,轮次循环的次数是数字长度-1
			for(int j = 0; j < arr.length - 1 - i; j++){
    
    //每一轮次的数字对比排序,每轮次的循环依次,轮次长度-1-i
				if(arr[j] > arr[j + 1]){
    
    //如果要是两个相邻的元素,前面的大于后面的,前后两个值交换
					temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}
			}
		}
		
		for(int i = 0; i < arr.length; i++){
    
    
			System.out.println(arr[i]);
		}

Traversal in java

Traversal method in java:

In Java, you will always encounter traversal, perhaps arrays, or collections, and arrays are of limited length, but what about collections? Ideally, as long as you have enough memory, you can access any length you want.

But when it comes to traversal, it can be divided into traversing an ordered collection or an unordered collection or array. Below we will look at the different traversal methods between the two.

Use the traditional for loop:

 我们通常把对有下标的简单的遍历,结构为:for(起始条件;终止条件;更改起始条件){      }形
//创建的一个数组
int[] arr= {
    
    13454};

 for(int i=0;i<=arr.length;i++){
    
    
     System.out.println(arr[i]);
 }
使用增强型for循环,结构为:for( 接收循环的数据类型  值:需要循环的对象){    }

For the above array traversal, enhanced loops can also be used;

 for(int a:arr) {
    
    
             System.out.println(a);
         }

Guess you like

Origin blog.csdn.net/qq_44909275/article/details/104821844