Java Review Notes Starter Stage 05: Arrays

1. Overview of arrays

What is a 1.1 array?

Arrays are memory areas (containers) used to store a batch of the same type of data

1.2 Example

        int[] arr={21,58,698,88};

        String[] names={"Zhang San","lisi","king wu"};

1.3 Key Points

  • Define an array to store data;

  • Operational array elements;

  • Solve the problem;

  • Array Memory Principle

  • Attention Points for Array Use

2. Array definition

2.1 Static Initialization Array

Assign values directly when defining arrays:

data type[] Array name = new Array data type[]{Element 1, element 2}
int[] ages = new int[]{15,24,28};

//Simplified Format
 data type[] Array name ={Element 1, element 2}
int[] ages = {15,24,28};

         * Statically Initialized Array
        double[] money = new double[]{15.6,65.3,99.9};
        double[] money2 ={15.6,65.3,99.9};

        int[] ages = new int[]{5,6,5,6};
        int[] ages2 = {5,6,5,6};

        String[] names = new String[]{"Zhang San","lisi","king wu"};
        String[] names2 = {"Zhang San","lisi","king wu"};

2.2 Fundamentals of Arrays

  • An array variable region ages is opened in memory.

  • The new outgoing object stores information in a continuous area, with the address given to the variable name.

  • [ I@4c873330 [for array; I for int; @ for address, followed by hexadecimal first address;

  • The address stored in the reference type; Store data in basic types

2.3 Array Access

  • Array Access

    • Array Name [Index]

    • Array length property: length

    • Array maximum index: length-1; // There is at least one element

  • Matters needing attention

    • The data type [] array name can also be the data type array name []

    • Array storage data type fixed

    • Once an array is defined, the length and type are fixed during program execution

2.4 Dynamic Initialization Array

  • When defining an array, only the element type and length are determined, and then the values are stored

    • Format: int[] sums= new int[9];

  • Principle:

    • Similar to static initialization, variable names are stored in the first address, length is determined, and each address is stored in the default value, such as: int default memory 0; Reference type null

  • Static uses the scene difference:

    • Static, know the specific value;

    • Dynamic, know the amount of data, don't know the specific value

3. Array traversal

3.1 Sum to Maximum

 int[] arr={21,58,698,88};
        for (int i = 0; i < arr.length; i++) {
        int sum=0;
        for (int i = 0; i < arr.length; i++) {

        //The first element is recommended as a reference
        int Max=arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (Max<arr[i]){
                Max = arr[i];

3.2 Word Guess Game

public class ArrayDemo2 {
    public static void main(String[] args) {
         * Character guessing game:
         *      Randomly generate 5 numbers between 1 and 20 (whether duplicated or not)
         *      Guess words: the output first appears in the tree location, all output ends

        Random random = new Random();
        int[] Numbers = new int[5];
        int count = 0;
        for (int i = 0; i < 5; i++) {
            Numbers[i] = random.nextInt(20)+1;
//        System.out.println(Arrays.toString(Numbers));
        Scanner scanner = new Scanner(;
        while (count<5){
            boolean a = false;
            System.out.println("Please enter 1-20 Number");
            int test = scanner.nextInt();
            if (test<0 || test>20){
                System.out.println("Number range input error");
                continue ;
            for (int i = 0; i < 5; i++) {
                if (Numbers[i] == test){
                    System.out.println("Good luck, guessed right, number"+Numbers[i]+"First appearing"+i+"position");
                    System.out.println("Bad"+(5-count)+"Yes, you guessed it!");
                    //break OUT; // End the cycle, end the game
            if (!a){
                System.out.println("No guess, try again");
        System.out.println("Congratulations, guess all the numbers and end the game.");

3.3 Random Rank

         * Random ranking:
         *      Enter five people and sort them randomly

        Scanner scanner = new Scanner(;
        System.out.println("Please enter how many people you want to sort");
        int length=scanner.nextInt();
        int[] users = new int[length];
        int count = 0;
        for (int i = 0; i < users.length; i++) {
            System.out.println("Please enter #"+i+"Individual ID");
        Random random = new Random();
        for (int i = 0; i < users.length; i++) {
            int temp=random.nextInt(users.length);
            int tempUser = users[0];

4. Array sorting

  • Array sorting

    • Sort elements in an array from smallest to largest or from largest to smallest

  • Sorting Technology

    • Bubble sort

    • Select Sort

    • fast

    • insert

  • Array search related technology

    • Binary search

    • Block Search

    • Hash Table Lookup

4.1 Bubble sort:

  1. Ideas:

    1. Start from scratch, sort in pairs, size change

    2. Save the largest element of the current array at the end of each round

  2. Steps to achieve:

    1. Outer Loop Round (Array Length-1)

    2. Internal loops, each comparing several positions backward (array length-i-1)

    3. The former and the latter, exchanging

         * Bubble sort
        int[] arr={6,8,7,3};
        //Define control cycle comparison rounds
        for (int i = 0; i < arr.length-1; i++) {
            //0 rounds, 3 comparisons, j=0 1 2
            //1 round, 2 comparisons, j=0 1
            //2 rounds, compare once, j=0
            for (int j = 0; j < arr.length-i-1 ; j++) {
               if (arr[j]>arr[j+1]){
                   int temp = arr[j+1];
        for (int i = 0; i < arr.length; i++) {

5. Memory diagram of arrays

5.1 Java Memory Allocation

  • Stack (data method runs area briefly)

    • Stack memory: The memory into which the method runs; The variable is also here

  • Heap (area where data stays for a long time)

    • new comes out, opens up space in this memory and generates an address

    • Big space, save a lot of new data in

  • Method Area

    • Memory into which byte code files are loaded

  • Native Method Stack

  • register

5.2 Java memory allocation process:

  1. Run after compilation to main method and place in method area;

  2. Method, open stack memory space, put basic data type in value, and reference type in variable name;

  3. Newopens up space in stack memory, generates addresses, and stores values;

  4. Assign the address generated by new to a variable based on the principle of running from right to left;

  5. Assignment based on address and spatial index;

5.3 Two variables point to the same array

The principle is the same; Assign the address of arr to arr2;

         * Two variables point to the same array
        int[] arr={6,8,7,3};
        int[] arr2=arr;


6. Frequently asked questions about array use

6.1 Access element position exceeds maximum index

ArrayIndexOutOfBoundsException (array index out of bounds exception)

6.2 No address, null, access in array variable

NullPointerException (null pointer exception)

Tags: Java

Posted by sw0o0sh on Wed, 10 Aug 2022 22:22:26 +0530