Java Array Base + Advanced

Array concept:
An array is a collection of elements that store the same data type.
Master the characteristics of arrays:
1. Once the length of the array is determined, it cannot be changed.
2. Each element in the array has a number. Starting at 0, the maximum number is the length of the array - 1;
Professional nomenclature for numbering: index
3. Arrays can only store elements of the same data type;
4. Arrays can store the same elements
Define Format
A: Data type [] array name;
B: Array name of data type [];
Recommendation is method A, method B will be forgotten.
But to understand
Initialization of arrays
A: Dynamic Initialization
Length only, default value given
Example: int[] arr = new int[3];
B: Static Initialization
Given the value, the system determines the length
Example: int[] arr = new int[]{1,2,3};
Simplified version: int[] arr = {1,2,3};
Common operations for arrays (must be mastered):
A:Traversal

		Mode 1:
			
			public static void printArray(int[] arr) {
				for(int x=0; x<arr.length; x++) {
					System.out.println(arr[x]);
				}
			}
			
		//Mode 2:
       
			public static void printArray(int[] arr) {
				System.out.print("[");
				for(int x=0; x<arr.length; x++) {
					if(x == arr.length-1) {
						System.out.println(arr[x]+"]");
					}else {
						System.out.println(arr[x]+", ");
					}
				}
			}

B:Maximum

		     //Maximum:
			public static int getMax(int[] arr) {
				int max = arr[0];
				
				for(int x=1; x<arr.length; x++) {
					if(arr[x] > max) {
						max = arr[x];
					}
				}
				
				return max;
			}
			
		    //Minimum value:
			public static int getMin(int[] arr) {
				int min = arr[0];
				
				for(int x=1; x<arr.length; x++) {
					if(arr[x] < min) {
						min = arr[x];
					}
				}
				
				return min;
			}

C:Reverse Order

	Mode 1:
		
			public static void reverse(int[] arr) {
				for(int x=0; x<arr.length/2; x++) {
					int temp = arr[x];
					arr[x] = arr[arr.length-1-x];
					arr[arr.length-1-x] = temp;
				}
			}
			
		//Mode 2:

			public static void reverse(int[] arr) {
				for(int start=0,end=arr.length-1; start<=end; start++,end--) {
					int temp = arr[start];
					arr[start] = arr[end];
					arr[end] = temp;
				}
			}

D:Lookup table

			public static String getString(String[] strArray,int index) {
				return strArray[index];
			}

E:Basic Search

		Mode 1:

			public static int getIndex(int[] arr,int value) {
				for(int x=0; x<arr.length; x++) {
					if(arr[x] == value) {
						return x;
					}
				}
				
				return -1;
			}
			
		//Mode 2:
		
			public static int getIndex(int[] arr,int value) {
				int index = -1;
			
				for(int x=0; x<arr.length; x++) {
					if(arr[x] == value) {
						index = x;
						break;
					}
				}
				
				return index;
			}		

Advanced Array Operations (Must Know):
Practice with dynamic arrays:
Example 1. Dynamic arrays perform the following functions:
Initialize an array of 10 lengths and do the following.
1. Insert elements at specified locations
2. Delete elements at specified locations
3. Append elements at specified locations
4. Modify elements at specified locations
Code implementation:

package shu.zu;

import java.util.Scanner;

public class Text_BianHuan {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] array = new int[10];
		for (int index = 0; index < array.length; index++) {
			System.out.print("Please enter #" + index + "Elements:");
			array[index] = sc.nextInt();
		}
			System.out.println("Array initialization succeeded!");
			System.out.print("array array={");
			for(int i=0;i<array.length;i++){
				System.out.print(array[i]);
				if(i!=array.length-1){
					System.out.print(",");
				}else{
					System.out.print("}");
					System.out.println();
				}
			}
		boolean flag = true;
		while (flag) {
			System.out.println("1,Insert Element    2. Delete element 3, modify element 4, query array 5, exit the program");
			int num = sc.nextInt();
			switch (num) {
			case 1:
				System.out.print("Please enter an index to insert the new element(0-" + (array.length - 1) + "):");
				int chaIndex = sc.nextInt();
				System.out.print("Enter a new element to add:");
				int value = sc.nextInt();
				int[] array2 = new int[array.length + 1];
				for (int i = 0; i < array2.length; i++) {
					if (chaIndex > i) {
						array2[i] = array[i];
					} else if (i == chaIndex) {
						array2[i] = value;
					} else {
						array2[i] = array[i - 1];
					}
				}
				array = array2;
				System.out.println("Insertion succeeded!");
				break;
			case 2:
				System.out.print("Enter the index of the element you want to delete:");
				int index = sc.nextInt();
				int[] array1 = new int[array.length - 1];
				for (int i = 0; i < array1.length; i++) {
					if (i < index) {
						array1[i] = array[i];
					} else {
						array1[i] = array[i + 1];
					}
				}
				array = array1;
				System.out.println("Delete successful!");
				break;
			case 3:
				System.out.print("Enter an index to delete the modified element:");
				int xiuIndex = sc.nextInt();
				System.out.print("Enter the value of the element you want to modify:");
				int value1 = sc.nextInt();
				array[xiuIndex] = value1;
				System.out.println("Successfully modified!");
				break;
			case 4:
				for(int i=0;i<array.length;i++){
					System.out.print(array[i]);
					if(i!=array.length-1){
						System.out.print(",");
					}else{
						System.out.print("}");
						System.out.println();
					}
				}
				break;
			case 5:
				System.out.println("Exit succeeded!");
				flag = false;
				break;
			default:
				System.out.println("The option you entered is incorrect, please start over!");
				continue;
			}
		}
	}
}

Example 2, removing duplicate elements from the array;
Array array = {4, 3, 35, 3, 2, 4, 6, 3}
Parse: use the boolean type array, change the Boolean array element value by sorting comparison method, then receive the array that removes duplicate elements through temporary array, then assign to the original array by assigning address values;
Code implementation:

package day.shuzu;

public class Text {
	public static void main(String[] args) {
		int[] array = { 4, 3, 35, 3, 2, 4, 6, 3 };
		boolean[] flag = new boolean[array.length];
		for (int index = 0; index < array.length; index++) {
			for (int i = index + 1; i < array.length; i++) {
				if (array[index] == array[i]) {
					flag[i] = true;
				}
			}
		}
		int sum = 0;
		for (int index = 0; index < flag.length; index++) {
			if (flag[index] == false) {
				sum += 1;
			}
		}
		int[] array1 = new int[sum];
		int a = 0;
		for (int index = 0; index < flag.length; index++) {
			if (flag[index] == false) {
				array1[a] = array[index];
				a++;
			}
		}
		for (int index = 0; index < array1.length; index++) {
			System.out.print(array1[index] + ",");
		}
	}
}
Five original articles were published. 0% praised. 38% visited
Private letter follow

Tags: Java

Posted on Thu, 06 Feb 2020 20:43:49 -0500 by sunsun