This is the 27th day of my participation in the August More Text Challenge

An array of

  1. Definition: An array is an ordered collection of data of the same type;

    1. Several data of the same type are arranged and combined in a certain sequence;

    2. Where each data is called an array element;

    3. Each array element can be accessed by a subscript starting at 0, that is, subscripts in the range [0, length-1];

    4. An array is a variable that stores a group of data of the same data type;

      1. To declare a variable is to carve out an appropriate space in memory;
      2. To declare an array is to delimit a sequence of contiguous Spaces in memory.
  2. Array features:

    1. Its length is certain. Once an array is created, its size cannot be changed.
    2. The elements must be of the same type, with no mixed types allowed.
    3. The elements in an array can be any data type, including primitive and reference types.
    4. An array has only one name, an identifier;
  3. Array genus reference types:

    1. Length, elements of the array
  4. The array uses 4 steps:

    1. Declare arrays (declare arrays without specifying their length);
    2. Allocate space;
    3. The assignment;
    4. Processing data.
  5. Arrays can be created in several ways:

    1. Declare and claim space;

      1. Int []arr = new int[5];
    2. Declare arrays and assign values;

      1. Int []arr2 = new int []{1,2,3,4,5};
  6. Array assignment:

    1. Int []score = {89,79,76}; Int []score = new int[]{89,79,76};
    2. Dynamically enter information from the keyboard and assign values;
  7. Default initialization of array elements:

    1. An array is a reference type whose elements are equivalent to instance variables of the class, so once an array is allocated space, each element is implicitly initialized in the same way as instance variables. The default value for an array depends on the type of array defined;

      1. Int: 0;
      2. String: null;
      3. Boolean: false;
  8. Array error prone points:

    1. Int []score = new int[];
    2. Int []score = new int[2]; Score [2] = 65;

practice

  1. Requirements:

 

  1. Code:

    import java.util.Scanner; Public class PrintArray{public static void main(String[] args){// Print char[]az = new char[26]; for(int i = 0; i<26; i++){ az[i] = (char)(i+'a'); System.out.print(az[i]+"\t"); } System.out.println(); Int []arr2 = new int[]{1,2}; Int []arr3 = {4,5,6}; for(int i = 0; i<2; i++){ System.out.print(arr2[i]+"\t"); } for(int i = 0; i<2; i++){ System.out.print(arr2[i]+"\t"); } System.out.println(); Char []sr = new char[3]; Scanner sc = new Scanner(System.in); System.out.println(" please input 3 characters: ") for(int I = 0; i<3; i++){ sr[i] = sc.next().charAt(0); } for(int I = 0; i<3; i++){ System.out.print(sr[i]+"\t"); } System.out.println(); Int []arr4 = new int[6]; for(int i = 0; i<arr4.length; i++){ System.out.print(arr4[i]+"\t"); } system.out. println(" array length: "+arr4.length); Int []arr5 = new int[]{8,4,2,1,23,344,1}; System.out.println(" Please enter an arbitrary number: "); Scanner sc = new Scanner(System.in); int posArr = sc.nextInt(); for(int i = 0; i<arr5.length; I ++){if(arr5[I] == posArr){system.out.println (" target number found! ); break; }else{system.out.println (" number not in target array "); break; }}}}Copy the code

Sorting algorithm

  1. Bubble sort:

    Int []arr = new int[]{4,1,7,2,9,3,5,8,6}; // bubble sort for(int I = 0; i<arr.length; i++){ for(int j=0; j<arr.length-1; j++){ if(arr[j] > arr[j+1]){ int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } for(int i = 0; i<arr.length; i++){ System.out.print(arr[i]+"\t"); }Copy the code

  2. Selection sort

    // select sort for(int I = 0; i<arr.length; i++){ for(int j = i+1; j<arr.length; j++){ if(arr[i] > arr[j]){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } for(int i = 0; i<arr.length; i++){ System.out.print(arr[i]+"\t"); } System.out.println();Copy the code

2 d array

  1. A two-dimensional array can be called an array of arrays;

    1. When defining a two-dimensional array, it is important to specify the length of the array (the length of a one-dimensional array);

      Public class TwoDimensionalArray{public static void main(String[] args){int [] arr = new int[][3]; int [][] arr = new int [3][]; Arr [0] = new int[5]; arr[1] = new int[3]; arr[2] = new int[2]; // assign arr[0][0] = 1; arr[0][1] = 2; arr[0][2] = 3; arr[0][3] = 4; arr[0][4] = 5; arr[1][0] = 1; arr[1][1] = 2; arr[1][2] = 3; arr[2][0] = 1; arr[2][1] = 2; for(int i = 0; i<arr.length; i++){ for(int j = 0; j<arr[i].length; j++){ System.out.print(arr[i][j]+"\t"); } System.out.println(); }}}Copy the code