The use of arrays and ideas-java basic learning

IDEA:

​ 1. Will use IDEA shortcut keys

  1. Will import/export a module

    1. Will create package classes and run a program

Array: (emphasis)

​ What is an array

​ When to use an array

​ How to use

​ How to create an array

​ How to get the contents of the array

​ Two commonly used operations on arrays:

​ 1. Traverse the array

  1. Find the best value/sum

IDEA installation

the goal

Install the idea tool

In-class code

​ Note: It is recommended to specify a custom installation path.

​ For detailed steps, refer to the "IDEA Installation Details.pdf" document in the notes.

IDEA crack

the goal

Crack the idea

In-class code

As IDEA is a paid software, it only provides users with a 30-day trial time. It needs to be used for a long time and needs to be cracked.

Use idea to write hello world case

the goal

What does the package do? Use diea to write a hello world case

In-class code

Requirements: Use the idea tool to create a project and run the HelloWorld case.

step:

​ 1. Create an empty project javaBase.Create New Project —> Empty Project

​ 2. Create a new module day04.File —> New —> Module

​ 3. Create the package com.itheima.demo1 under the src of the module. New —> Package

​ 4. Create a new class HelloWorld in the package. New —> Java Class

​ 5. Write the code and execute the program.

Illustration:

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-l0RVaztS-1616151997371)(assets/1560447600776.png)]

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-wAeYUeqj-1616151997374)(assets/1560448772319.png)]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-6HkcWkXR-1616151997377)(assets/1560449454596.png)]

summary

  1. What is a project? What is the relationship between project and module?

    A basic class is a project, and the content you learn every day is a module.

  2. All module codes are stored in the project.

  3. Write code in the src directory of the module to write code.

  1. What does the package do? What is the naming convention for packages?

    The package in java is essentially a folder, and its role is to manage code in different categories.

    Naming conventions: All lowercase, composition = reverse writing of company domain name + type of code.

    www.itheima.com -> com.itheima.shuzu

  2. Use diea to write a hello world case

public class Demo1 {
    
    

    //psvm
    public static void main(String[] args) {
    
    
        //输出语句 sout
        System.out.println("hello idea");
    }

}

Project structure in idea

the goal

Can create a project and create a module, the role of the project and the module?

In-class code

Project:

​ Project is the top structural unit of IDEA, representing a complete project or project. A project is generally divided into many modules by function, that is, a Project contains multiple Module modules.

Module:

​ Depends on the existence of the project, a module can complete independent and complete functions. There is a src directory under the module. When developing, you need to create a package in the src directory, create a class in the package, and complete the writing of java code in the class.

to sum up:

​ There can be multiple Module modules under a Project project, multiple Package packages under the src of the module, and multiple java class files under one package.

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-nQSo1VAj-1616151997381)(assets/1560446901601.png)]

summary

  1. Able to create project and module

  2. What is the role of project and module?

  • A project contains multiple modules

  • A module can write a package

Rules for creating packages:

​ www.itheima.com

Com.itheima

IDEA basic settings

the goal

Set the font of Idea

In-class code

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-ycXr9Eli-1616151997384)(assets/1560532023882.png)]

Background settings File->Settings->Appearance & Behavior->Appearance->Theme (Darcula is classic black)

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-yt4cDDXf-1616151997387)(assets/1560532480747.png)]

System Settings File -> Settings -> Appearance & Behavior -> System Settings

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-4ftBV5kD-1616151997393)(assets/1560533421844.png)]

summary

  1. Set the font of Idea

IDEA commonly used shortcut keys

the goal

Understand the shortcut keys from idea to related

In-class code

hot key Features
Ctrl + Alt + L Format code
Ctrl + / Add or cancel single-line comments
Ctrl + x Delete the line where the cursor is
Ctrl + D Copy the content of the line where the cursor is located, and insert it below the cursor position
Alt + Shift + up and down arrows Move the current line of code
Shift + Enter Fast line break
psvm Quickly generate main method
Ctrl + Shift + / Select code comment, multi-line comment, and press to cancel comment
salt Quickly generate print statements
Ctrl + Alt + Space Automatic code completion tips

summary

Understand the shortcut keys from idea to related

hot key Features
Ctrl + Shift + / Select code comment, multi-line comment, and press to cancel comment
Ctrl + Alt + L Format code
Ctrl + / Add or cancel single-line comments
Ctrl + Y Delete the line where the cursor is
Ctrl + D Copy the content of the line where the cursor is located, and insert it below the cursor position
Alt + Shift + up and down arrows Move the current line of code
Shift + Enter Fast line break
psvm Quickly generate main method
salt Quickly generate print statements
Ctrl + Alt + Space Automatic code completion tips

IDEA module operation

the goal

How to import modules? How to delete a module?

In-class code

Module import

File --> Porject Structure --> Modules --> 加号 --> Import Module

Select [module name] or the corresponding [module name.iml] file to complete the import.

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-0F5tAMrt-1616151997394)(assets/1559304620231.png)]

Module delete

Method 1: Mouse select module, right click Remove Module

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-21bmwVlh-1616151997398)(assets/1559305085702.png)]

Way 2:

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-fEPSorRe-1616151997399)(assets/1559304942378.png)]

Note: Module deletion only deletes the project directory in IDEA, not the module folder under the disk.

summary

  1. How to import modules

    • Create a new module by yourself, and copy all the content in the src of the module you want to import.
  2. How to delete a module

    • Select the module-----remove module-----Go to the location of the module to delete.

Array definition format

the goal

What are arrays used for? The characteristics of the array? What is the definition format of the array?

summary

  1. What is an array?

    An array is a container, it can hold a lot of data at once.

    Array is actually equivalent to helping us define a large number of variables at once.

  2. Characteristics of arrays

    Arrays can only store data of the same type

  3. Array format

    Data type [] Array name recommendation

    Data type array name[]

    int[] arr

    double [] arr

    char[] arr

Array dynamic initialization

the goal

What is initialization? What is dynamic initialization of an array? The format of the array dynamic initialization? Format meaning of dynamic initialization

summary

What is initialization

1. Allocate memory space 2. Assignment

What is dynamic initialization of an array?

1. When creating the array, only the length of the array is specified 2. The default assignment

The format of dynamic initialization?

Data type [] array name = new data type [length];

Format meaning

Array access

How do we access the elements of the array

Access the elements of the array by index

Array name [index]

Index: Number

The index of the array starts from 0, and the largest index is the length of the array-1

Memory allocation

the goal

How many memory areas are divided into jvm memory? What are the characteristics of data stored in stack memory and heap memory?

In-class code

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-ziEY45aN-1616151997403)(assets/1559370114032.png)]

The role of each memory area

Area name effect
Stack memory (Stack) The memory used when the method is running, storing the variables and parameters in the method. Disappear when the method is used up (pop the stack)
Heap memory (Heap) Storage objects or arrays, new data, are stored in the heap memory. Will not disappear immediately after use, but wait for the garbage collection mechanism to be cleared when it is free
Method area Store the loaded class file information.
register For CPU use, it has nothing to do with our development.
Native method stack JVM is used when using operating system functions and has nothing to do with our development.

Currently we only need to pay attention to stack memory and heap memory.

The default initialization value of the array

int : 0
double : 0.0
boolean : false
引用数据类型 : null    
数据类型: 基本数据类型  引用数据类型
    基本数据: byte short char int long float double boolean
    引用数据类型 : String , 类, 数组  , 接口

summary

  1. How many areas are jvm memory divided into?

    • Stack memory: store the memory when the method is running, including local variables
    • Heap memory: All new objects/entities are in the heap memory Scanner Random array
    • Method area. ,
    • register
    • Native method stack
  2. What are the characteristics of data stored in stack memory and heap memory?

Single array memory map

the goal

Understand the memory graph of a single array

In-class code

package cn.itheima.array;

public class ArrayDemo2 {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = new int[3];
        System.out.println(arr);
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
        //修改值
        arr[0] = 100;
        arr[2] = 200;
        System.out.println(arr);
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
    }
}

summary

  1. Understand the memory graph of a single array

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-RmkThGbg-1616151997404)(assets/1594626791799.png)]

Multiple array memory map

the goal

Understand the memory diagram of multiple arrays. If two variables point to different array objects, will the modification affect each other?

summary

Understand the memory diagram of multiple arrays. If two variables point to different array objects, will the modification affect each other?

Multiple array variables point to the same array object

the goal

Multiple variables point to the same array object, so will modifying the elements affect each other?

summary

  1. When multiple objects point to the same memory address, will modifying elements affect each other?

Modifications between each other will have an impact

Array static initialization

the goal

The characteristics of static initialization? The format of static initialization? When to use static initialization and dynamic initialization?

summary

  1. The characteristics of static initialization?

    When creating an array, you only need to specify the elements of the array, not the length. The length is determined by the number of elements

  2. The format of static initialization?

    Data type [] Array name = new data type [] {element 1, element 2, element 3...};

    Data type [] Array name = {element 1, element 2, element 3...};

  3. When to use static initialization and dynamic initialization?

    If we know the elements of the array, we use static

    If we only know the length of the array, we use dynamic

expand

【练习】下面创建数组错误的是( )
	A、 int[] arr = new int[5];
	B、 int arr[] = new int[5];
	C、 int[] arr = new int[3]{
    
    1,2,3}; 
	D、 int[] arr = {
    
    1,2,3};

【练习】下面为数组初始化正确的写法是(  )
	A: double c[]=new double[]{
    
    1,2}	 
	B: double c[]=new double1,2
	C: double c[]=new double[](1,2)	
	D: double c[]=new double{
    
    1,2}	
	E: double c[]=new double[2]{
    
    1,2} 

Two common small problems of array operations

the goal

What are the two common problems when using arrays? What caused it?

In-class code

package com.itheima_04;

/*
    索引越界:访问了数组中不存在的索引对应的元素,造成索引越界问题
        ArrayIndexOutOfBoundsException

    空指针异常:访问的数组已经不再指向堆内存的数据,造成空指针异常
        NullPointerException

    null:空值,引用数据类型的默认值,表示不指向任何有效对象
 */
public class ArrayDemo {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = new int[3];

        //System.out.println(arr[3]);
        //System.out.println(arr[2]);

        //把null赋值给数组
        arr = null;
        System.out.println(arr[0]);
    }
}

summary

  1. What are the two common problems when using arrays?

  2. What caused it?

Traverse

the goal

How to write the elements of an array using loops? How to access the length of the array?

In-class code

package com.itheima_05;
/*
    遍历
        获取数组中的每一个元素输出在控制台

    获取数组元素
        数组名.length
 */
public class ArrayTest01 {
    
    
    public static void main(String[] args) {
    
    
        //定义数组
        int[] arr = {
    
    11, 22, 33, 44, 55};

        //使用通用的遍历格式
        for(int x=0; x<arr.length; x++) {
    
    
            System.out.println(arr[x]);
        }
    }
}

summary

  1. How to write the elements of an array using loops?
  2. How to access the length of the array?

Max

the goal

Find the maximum value of the array {66, 88, 11, 22, 33}

Requirements: find the maximum value of the array

step:

1、定义临时变量max,存储数组最大值,并假设第一个元素为最大值。

2、遍历数组,从第二个元素开始,逐个与max比较。

3、如果该元素比max变量还大,将该元素赋值给max变量。

4、循环结束后,max中存储的就是数组的最大值。

In-class code


package com.itheima_05;
/*
    获取最值
        获取数组中的最大值
        最小值同学们自己完成
 */
public class ArrayTest02 {
    
    
    public static void main(String[] args) {
    
    
        //定义数组
        int[] arr = {
    
    12, 45, 98, 73, 60};

        //定义一个变量,用于保存最大值
        //取数组中第一个数据作为变量的初始值
        int max = arr[0];

        //与数组中剩余的数据逐个比对,每次比对将最大值保存到变量中
        for(int x=1; x<arr.length; x++) {
    
    
            if(arr[x] > max) {
    
    
                max = arr[x];
            }
        }

        //循环结束后打印变量的值
        System.out.println("max:" + max);

    }
}

summary

  1. Idea steps to find the maximum
    • Create an array using static initialization
    • Define a variable to store the maximum value. The default value is to get the first element of the array.
    • Iterate over the elements of the array starting from the index value 1, and then compare each element with the variable of the maximum value. If an element is greater than the maximum value, the variable of the maximum value is changed to the current element.
    • Maximum output

Guess you like

Origin blog.csdn.net/weixin_42137874/article/details/115006844