The fifth day of Java big data learning -- while do while loop array method overload bubble sorting

review

structure

  1. Sequential structure
  2. Branch structure if
  3. Loop structure for
  4. Select structure switch

variable

  1. Member variable
  2. local variable

The Fifth Day

while Loop

  1. Structure:

    while (loop condition){

    Circulatory body;

    }

do-while Loop

  1. Structure:

    do{

    Circulatory body;

    }while (cycle condition);

Differences between the three cycles

  1. for: know the number of cycles
  2. while/do while: when the number of cycles is uncertain
  3. while: judge first. If the rules are not met, the code will not be executed
  4. do while: the code is executed at least once, and then judge whether it meets the rules and execute the code again
  5. Loops can replace each other, but it is generally best to choose an appropriate loop structure to complete the code~

array

  1. Concept:

    Array array, whose flag is [], is used to store multiple sets of data of the same type. If you want to obtain the element values in the array, you can obtain them through the footmark (subscript). The array subscript starts from 0, and the maximum value of the subscript is the length of the array minus 1

  2. Create array:

    • Static creation
      • Type [] array name = {constituent elements of array}
      • Type [] array name = new type {constituent elements of array}
    • Dynamic creation
      • Type [] array name = new type [array length]
  3. Array creation process

    • Example: the program creates an array int[] a = new int[5]; What happened at the time?
    1. Open up a continuous space in memory to store data. The length is 5
    2. Complete the initialization process for the array, give each element a default value, and the default value of int type is 0
    3. When the array is initialized, a unique address value is assigned
    4. Give the unique address value to the reference type variable a to save
  4. Array length: the length of the array is represented by the length attribute. Once the array is created, the length cannot be changed. The length of the array is allowed to be 0

    Array name. length when used

  5. Array properties:

    1. The length of the array is determined by * * array name. Length** To get
    2. Once the array is created, its length cannot be changed
    3. The subscript of the array starts from 0, and the maximum subscript is the array length - 1
    4. If a nonexistent subscript is accessed, the array subscript is out of bounds
  6. Array tool class Arrays

    • ToString (array name). For arrays of other types except char, you must use this method to view the specific elements of the array. If not, print the address value of the array
    • Sort (array name) to sort the array
    • Copyof (array to be copied, length of new array)
      If the new length is greater than the length of the original array - the expansion of the array
      If the new length is less than the length of the original array – the size of the array
      If the new length is equal to the length of the original array - normal copy
      Note: no matter what the length, it is not to modify the original array, but to create a new array
  7. Traversal of array

    • If you just want to see which elements are in the array, directly use system.out.println (arrays. ToString (array name)); Can
    • If you want to get the specific elements in the array or do further operations on the elements in the array, you need to traverse the array
    • Traversal: go through all the elements in the array one by one from beginning to end
    • Loop through the array, so the loop variable in the loop represents the subscript of the array

Bubble sorting

Sorting idea: To compare multiple rounds, compare two adjacent elements in each round, and exchange them if the position is wrong
Code idea:
This is done by nesting for loops

Outer cycle: it controls the number of rounds. If there are n rounds, the maximum is n-1 rounds
Inner loop: it controls the number of comparisons in each round. The number of comparisons in each round will decrease. The maximum value determined in the previous round should not participate in the comparison of the next round
Note: the loop variable of the outer loop represents the number of rounds 1 2 3 4
The loop variable of the inner loop represents the subscript [0,a.length-i) of the array

Method overload

/*We use the method name + parameter list to determine which method to call/

	/**Method overload
	 * Methods with the same method name but different parameter lists exist in the same class
	 * If the number of parameters of the method with the same name is different in the same class, it must constitute an overload
	 * If the number of parameters of the method with the same name is the same in the same class, it may constitute an overload
	 * (int a,String b)And (int b,String a) -- do not constitute an overload  
	 * You need to view the type of the parameter at the corresponding position, not the parameter name, which is independent of the parameter name
	 * (int a,String b)And (String a,int b) -- constitute an overload
	 * */

Today's case

Case 1
    package cn.yedu.review;
/**Test while*/
import java.util.Random;
import java.util.Scanner;

import javax.annotation.processing.SupportedSourceVersion;


public class TestWhile {
	public static void main(String[] args) {
		//The demand generates a random number and compares it with the number guessed by the user until the user guesses it correctly
		//1. Create and call the method of generating random numbers to return values according to business analysis
		int r = creatNum();
		System.out.println("Generated random number:"+r);
		//3. Create and call the method of guessing numbers
		guessNum (r);
        
        
Case 2
package cn.yedu.review;

import java.util.Random;

/**Test do while*/
public class TestDoWhile {
	public static void main(String[] args) {
		int n ;
		do {
			System.out.println("I am a circulatory body");
			n = new Random().nextInt(300);//Random number generated by random package guide 0-300, excluding 300
			System.out.println(n);
		}while(n>100);
		/**do-while  It will be executed at least once, and then make judgment. If it meets the conditions, continue the cycle. It is suitable for the cycle that will be executed at least once*/
		
	}
}
Case 3
    package cn.tedu.array;

import java.util.Arrays;

/**This class is used to practice the creation of arrays*/
public class TestArray {
	public static void main(String[] args) {
		//1. Create an array
		//1.1 static array creation -- the specific value of each element has been determined when creating the array
		char[] c1= {'h','e','l','l','o'};//Create an array of char types. Char [] is a reference type that represents an array of char types
		char[] c2= new char[] {'h','e','l','l','o'};
		
		//1.2 dynamically create an array -- only know the length of the array, and then dynamically assign values to each element of the array
		char[] c3=new char[5];//Open up a continuous space in memory to store 5 characters -- the length of the array is 5 and has a default value
		/**We manipulate each element in the array through the index of the array, which starts from 0
		 * Assign value to each element: numeric name [subscript] = assign value*/
		c3[0]='h';
		c3[1]='e';
		c3[2]='l';
		c3[3]='l';
		c3[4]='o';
		//Print view the array you just created
		System.out.println(c1);
		System.out.println(c2);
		System.out.println(c3);
		
		//3. Create a String type array to store a, B, C
		//3.1 static
		String[] s1= {"a","b","c"};
		String[] s2= new String[]{"a","b","c"};
		String[] s3= new String[3];
		s3[0] = "a";
		s3[1] = "b";
		s3[2] = "c";
		/**char Type array is processed in the underlying code. You can directly print the array name and view the specific elements in the array
		 * For Arrays other than char type, if you want to view the specific elements in the array, you need to use the tool class of the array: Arrays 
		 * Specific usage: Arrays.tostring (array name). Note that you need to import packages when using Arrays*/
		System.out.println(Arrays.toString(s1));
		System.out.println(Arrays.toString(s2));
		System.out.println(Arrays.toString(s3));
	}
}
Case 4
package cn.tedu.array;

import java.util.Arrays;

/**Review how arrays are created*/
public class TestArray2 {
	public static void main(String[] args) {
		//1.
		int[] a= {1,2,3,4,5,6};
		int[] b=new int[] {1,2,3,4,5,6};
		int[] c= new int[6];
		c[0] = 1;
		c[1] = 2;
		c[2] = 3;
		c[3] = 4;
		c[4] = 5;
		c[5] = 6;
		System.out.println(Arrays.toString(a));
		System.out.println(Arrays.toString(b));
		System.out.println(Arrays.toString(c));
		/**int Is the basic type. Int [] represents an int array and is a reference type
		 * Variables of array type, such as a, store the address value of the array rather than multiple specific elements in the array*/
		System.out.println(a);
		/**Note: once the array is created, the length of the array cannot be changed 
		 *If you want to add or delete elements in the array, you can only recreate a new length array and copy the original data*/
		System.out.println(a.length);//6 length of array a  
		
	}
}
Case 5
package cn.tedu.array;

import java.util.Arrays;
import java.util.Random;

/**This class is used for array exercises*/
public class TestArrayExec {
	public static void main(String[] args) {		
		//1. Create a method to obtain the days of each month of 12 months in a year, and print x months with X days
		dayOfMonth();
		//2. Create a method to store data, such as int array, into 1-10
		addNumToArrays();
		//3. Create random array: create a random integer array with a length of 6 
		creatArrays();
	
	}
//3.
	private static void creatArrays() {
		int[] a=new int[6];//Create a dynamic array of length 6
		for(int i = 0;i<6;i++) {//The array traverses and stores data in turn
			a[i]=new Random().nextInt(100);//Random 0-100 data
		}
		//Loop end print array
		System.out.println(Arrays.toString(a));
	}
//2.
	private static void addNumToArrays() {
		//Create an int array with a length of 10
		//Traverse the array and store data in turn at each position in the array
		int[] a=new int[10];
		for(int i=0;i<10;i++) {
			a[i]=i+1;			
		}
		//After the for loop ends
		//Print and view the specific elements in the array after storing the data circularly
		System.out.println(Arrays.toString(a));
	}
//1.
	private static void dayOfMonth() {
		// Create an int type array to store 12 months of days
		int[] a= {31,28,31,30,31,30,31,31,30,31,30,31};
		//Traversal of array--
		//From there: the first element of the array: a[0]
		//To the end: the twelfth element of the array: a [11] - a [a.length-1]
		//How to change: subscript of array++
		for(int i=0;i<=a.length-1;i++) {
			System.out.println(a[i]);
			System.out.println((i+1)+"Yue you"+a[i]+"day");
		}
		
	}
}
Case 6
 package cn.tedu.array;

import java.util.Arrays;

/**This class is used to practice expanding and shrinking arrays*/
public class TestArraysCopyOf {
	public static void main(String[] args) {
		//1. Create an array
		int[] from= {1,2,3,4,5};//Once the array is created, its length cannot be changed
		//1.1 common copy of array
		/**
		 * Arrays.copyOf()Used to complete the assignment of the array, two parameters
		 * Parameter 1: which array do you want to copy
		 * Parameter 2: length of new array
		 * */
		int[] to=Arrays.copyOf(from,5);//There is a return value, so use int [] to accept the copied new array	
		System.out.println(Arrays.toString(to));//[1,2,3,4,5]
		
		//1.2 expansion of array
		/**
		 * Capacity expansion: expand the capacity of the array. The capacity of the new array is greater than that of the original array
		 * Capacity expansion idea: first create a new array corresponding to the new length, and initialize the default value of 0 for each position
		 * Then copy the data from the original array to the new array. The position that has not been overwritten is still 0
		 * */
		int[] to2=Arrays.copyOf(from,10);	
		System.out.println(Arrays.toString(to2));//[1,2,3,4,5,0,0,0,0,0]
		
		//1.3 volume reduction of array
		/**
		 * Shrink: reduce the capacity of the array. The capacity of the new array is less than that of the original array
		 * Shrink volume idea: first create a new array of corresponding length, and the default value of initialization for each position is 0
		 * Then assign a specified number of data from the original array to the new array, similar to the "intercept" part
		 * */
		int[] to3=Arrays.copyOf(from,3);
		System.out.println(Arrays.toString(to3));//[1,2,3]
		
		//1.4 specify to intercept the elements in the original array from beginning to end
		/** copyOfRange()Used to complete the interception of the array
		 * Parameter 1: which array to intercept [original array]
		 * Parameter 2: which subscript to start from [including this element]
		 * Parameter 3: end of subscript [element without this subscript]*/
		int[] to4=Arrays.copyOfRange(from,2,4);
		System.out.println(Arrays.toString(to4));//[3,4]
		
		int[] to5=Arrays.copyOfRange(from,1,7);//Intercept and expand capacity
		System.out.println(Arrays.toString(to5));//[2,3,4,5,0,0]  
		
		
		
	}
	
		
}   
Case 7
package cn.tedu.basic;

import java.util.Arrays;

/**Bubble sorting*/
public class BubbleSort {
	public static void main(String[] args) {
		//Create an unordered array
		int[] a= {27,96,73,25,21};
		//Call your own sorting method to sort
		//Method 1
		method(a);
		//Method 2  
		/**We use sort() of the array tool class Arrays to sort the array elements*/
		Arrays.sort(a);
		System.out.println(Arrays.toString(a));
	}

	private static void method(int[] a) {
		//The number of rounds of outer loop control comparison is assumed to be n, and the maximum number of rounds is n-1
		//Cyclic variables represent the number of rounds. For example, five numbers can compare up to four rounds, and the value of i [1,2,3,4] represents four rounds
		for(int i=1;i<=a.length-1;i++) {
			System.out.println("The first"+i+"round");
			//The inner loop controls the comparison of two adjacent elements in the array. If the position is wrong, the position will be exchanged
			//Circular variables represent the subscript of the array
			for(int j=0;j<a.length-i;j++) {
				//For adjacent comparison, a[j] represents the previous number of the array, and a[j+1] represents the next number
				if(a[j]>a[j+1]) {
					//change of position
					int t;
					t=a[j];
					a[j]=a[j+1];
					a[j+1]=t;
					
				}
			}
			System.out.println("The first"+i+"Results after round comparison:"+Arrays.toString(a));
		}
		System.out.println(Arrays.toString(a));
	}
}    
Case 8
package cn.tedu.basic;
/**This class is used for overloading test methods*/
public class TestMethod {
	public static void main(String[] args) {
		/**We use the method name + parameter list to determine which method to call*/
		
		/**Method overload
		 * Methods with the same method name but different parameter lists exist in the same class
		 * If the number of parameters of the method with the same name is different in the same class, it must constitute an overload
		 * If the number of parameters of the method with the same name is the same in the same class, it may constitute an overload
		 * (int a,String b)And (int b,String a) -- do not constitute an overload  
		 * You need to view the type of the parameter at the corresponding position, not the parameter name, which is independent of the parameter name
		 * (int a,String b)And (String a,int b) -- constitute an overload
		 * */
		//1
		method();
		//2
		method(666);
		//3
		method(999,"Ganmaoling");
		//4
		method("Ganmaoling",999);
	}
	
	//1
	private static void method() {
		System.out.println("Hahaha, I have no parameters~~");
	}
	//2
	private static void method(int a) {
		System.out.println("Hahaha, my parameter is:"+a);
	}
	//3
	private static void method(int a,String b) {
		System.out.println("Hahaha, I have no parameters~~");
	}
	//4
	private static void method(String a,int b) {
		System.out.println("Hahaha, I have no parameters~~");
	}
}
        
        
        
        
        
        

Tags: Java Big Data Algorithm

Posted on Mon, 06 Dec 2021 20:04:37 -0500 by hesketh