Common java array usage

1. Assignment

Two assignment methods

Static initialization: define the contents of the array directly from the beginning

Dynamic initialization: open up the space first, and then assign values later. You can not assign all values (at this time, the initial values are not assigned, such as 0 and null)

   		// statement
		int[] ids;
		//1.1 static initialization; The initialization of array and the amplitude operation of array elements are carried out at the same time.
		ids = new int[]{100,100,1002,1003};//Reference data type, so new is required
		//1.2 dynamic initialization
		//Array initialization and array element amplitude operation separate. conduct
		String[] names = new String[6]; //names do not need to be declared like ids above?
		// Assign value later
		names[0] = "asd";
		names[1] = "miracleli";
		/*  Try assigning some values. Not all of them are assigned.
		names[2] = "Assad ";
		names[3] = "213e";
		names[4] = "qwe";

2. Traversal

The for loop keeps running out. println();

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

Search, assignment, flipping and sorting of arrays

1. Assignment of array elements (Yang Hui triangle, loop number, etc.)

Yang Hui triangle:

Output the first ten lines



public class YangHuiSanJiao {

	public static void main(String[] args) {
		int[][] arr = new int[10][];
		arr[0] = new int[]{1};
		arr[1] = new int[]{1,1};
		for(int i = 2;i<arr.length;i++)
			arr[i] = new int[i+1];
			arr[i][0] = 1;
			for(int j = 1;j<arr[i].length-1;j++)
				arr[i][j] = arr[i-1][j-1]+arr[i-1][j];	
		for(int i = 0;i<arr.length;i++)
			for(int j = 0;j<arr[i].length;j++)

2. Find the maximum, minimum, average, sum, etc. of the elements in the numerical array

package com.atguigu.exer;

 * Define a one-dimensional array of int type, containing 10 elements, and assign some random integers respectively,
Then find the maximum, minimum, sum and average values of all elements and output them.
Requirement: all random numbers are two digits. [10,99]
Math.random()The value range of is [0,1)

public class ArrayExer1 {
	public static void main(String[] args) {
		int[] arr = new int[10];
		int all=0;

		double avg = 0.0;
		for(int i=0; i<arr.length;i++)	
			arr[i] = (int)(Math.random()*(99-10+1)+10);   //Casts should be enclosed in parentheses (int)
			all += arr[i];
		//average value
		avg = (double)all/arr.length;
		int max = arr[0];
		int min = arr[0];
		for (int i = 1; i < arr.length; i++) 
				min = arr[i];
			else if(arr[i]>max)
				max = arr[i];
		System.out.println("And the value is:"+all);
		System.out.println("The maximum value is:"+max);
		System.out.println("The minimum value is:"+min);
		System.out.println("The average value is:"+avg);

3. Array copy, inversion and search (linear search, dichotomy search)

  • Copy (different from the assignment of array variables)
// Copy of array
for(int i = 0;i<array2.length;i++)
    array2[i] = array1[i];

If array2 = array1;

At this time, if array2 print s, it is the same as array1. However, if array2 is modified, the value in array1 will also change. The reason is that array2 = array1; This will cause array2 to store the same memory address as array1. These two variables point to the same memory space. As long as you change one of them, the other will also change. Usually, when doing this for assignment, it is easy to modify array2 subsequently, resulting in the change of array1. At this time, there is no error, so it is difficult to shoot the problem. Therefore, it is generally assigned through the for loop.

  • reversal
    Inversion is to swap the front and back elements of the array, and in turn, get a new layout
    String[] arr = new String[]{"AA","BB","MM","GG","DD","xx"};
    //Use temporary variables	
    for(int i =0;i<arr.length/2;i++)  //It's divided by two
    	String temp = arr[i];   //This is not a String array, so new is not required.
    	arr[i] = arr[(arr.length-1)-i];  //Note here that minus 1, because the first one is 0 and the last one is arr.length-1
    	arr[(arr.length-1)-i] = temp; 

    The reverse result is as follows: correct

  • Find / search (linear, dichotomy)
    • Linear search (carpet search)
// Linear search
String[] arr = new String[]{"AA","BB","MM","GG","DD","xx"};
String dest = "BB";

boolean isfalg = false;
for(int i = 0;i<arr.length;i++)
		System.out.println("The specified element was found at:"+i);
		isfalg = true;
	System.out.println("Can't find");
  • Binary search
    Must be an ordered array to find by dichotomy. What is an ordered array? [1,2,3,4,5,6] and [7,6,4,3,2,1] are ordered arrays. [1,3,1,4,5,2] is not an ordered array. That is to say, if the element values in the array are arranged from small to large or from large to small, you can use binary search.

    Specific algorithm:

	//Binary search
	// The array to be searched must be ordered (from small to large or from large to small)
	int[] arr2 = new int[]{-123,-43,-22,-3,33,52,91,232,};
	int dest1 = 33;
	int head = 0; //Initial first index
	int end = arr2.length-1;//Initial last index
	boolean isFlag = false;
		int middle = (head+end)/2;
		if(arr2[middle] == dest1)
			System.out.println("The specified element was found at:"+middle);
			isFlag = true;
		else if(arr2[middle]<dest1)
			head = middle+1;
			end = middle-1;
		System.out.println("The specified element was not found");

4. Sorting algorithm of array elements
Sometimes, in order to find faster, you need to use dichotomy, which can only be used for ordered data, so you have to sort first.

The title of the sorting algorithm will be updated later

Top ten internal sorting algorithms

The implementation idea must know that java is encapsulated, but the most basic thing is to be able to write by hand


The idea of quick sorting should be understood. One element can be arranged to its final position at a time, and the length of the array is divided into two segments for continuous classification

If you don't know the amount, take an example and find the law.

It doesn't mean which is the best. There are applicable cases.

During real development, you don't need to write any, but directly call——

Look at my other article—— Use of class Arrays

Keywords: Java Eclipse JavaEE Algorithm architecture

Added by dmort on Wed, 29 Dec 2021 09:38:56 +0200