[multi easy education Java big data enrollment] preview 06

Posted by leoden on Tue, 04 Jan 2022 22:41:43 +0100

preface

Today is the sixth day of preview. Although I preview at home, there may be things that affect my study, but generally speaking, the progress is still a little slow. I will continue to work hard. Today, I mainly learned about user-defined types (user-defined classes) and collections (collections are mainly ArrayList collections). I will introduce today's preview in detail.

preview contents

Knowledge summary

1. Class we have also learned before. In the Java language, things in real life are abstracted into code. At this time, we can use a user-defined data type (class) to describe (map) things in real life. A user-defined class is a reference data type defined by ourselves.

Class definition

Format:

Create a java file with the same class name
public class class name{
Data type} attribute name 1;
Data type} attribute name 2;
                ...
        }

Use of classes
① guide bag
② create object
Data type variable name = new data type ();
③ access properties
Variable name Attribute name;

Example:

//Define a mobile phone class
public class Phone{
    int price;//Define phone price
    String brand;//Define mobile phone brand
    String color;//Define phone color
}

//Use of classes
Phone p = new Phone();
p.price = 2200;
p.brand = "Huawei";
p.color = "white";

//Printout
System.out.println("The price of mobile phone is" + p.price);
System.out.println("The mobile phone brand is" + p.brand);
System.out.println("Phone color is" + p.color);

2. In order to save an uncertain number of elements, the JDK provides a series of special classes. These classes can store elements of reference type with variable length, which are collectively referred to as collections. ArrayList collection is the most common collection in programs. It belongs to reference data type (class). Let's explain ArrayList collection in detail.

First, let's look at the characteristics of arrays and Collections:

Characteristics of array
① the length is fixed
② any type of data can be stored, but an array can only store one type of data
③ the array has an index, and the index starts from 0
Set characteristics
① variable length
② only reference type data can be stored. If you want to save a basic type, you need to use the packaging type corresponding to the basic type
③ the bottom layer of ArrayList set is array with index, and the index starts from 0

Packing type corresponding to the basic type: (except that int becomes Integer, the initial letters of other basic types are capitalized)

Basic typeBasic type packing class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Let's write a few examples of creating an ArrayList collection:

//The ArrayList collection that stores string type data. The contents in parentheses after the equal sign are consistent with those in parentheses before the equal sign and can be omitted
ArrayList<String> list = new ArrayList<String>();
//An ArrayList collection that stores integer type data
ArrayList<Integer> list = new ArrayList<Integer>(); 
//ArrayList collection that stores custom Phone type data
ArrayList<Phone> list = new ArrayList<Phone>();

ArrayList collection use
① guide bag
        import java.util.ArrayList
② create object
ArrayList < data type of storage element in Collection > variable name = new ArrayList < > ();
③ calling method
Add (E) add element
get(int index) gets the element according to the index
size() gets the length of the collection

import java.util.ArrayList;
public class ArrayList01 {
	public static void main(String[] args) {
		
        //Create an ArrayList collection
		ArrayList<String> list = new ArrayList<String>();
		
        //Add elements to the collection
		list.add("stu1");
		list.add("stu2");
		list.add("stu3");
		list.add("stu4");
		
        //Gets the number of elements in the collection
		System.out.println("Length of collection:" + list.size());
		
        //Take out and print the element at the specified position
		System.out.println("The first element is:" + list.get(0));//Display stu1
		System.out.println("The second element is:" + list.get(1));//Display stu2
		System.out.println("The third element is:" + list.get(2));//Display stu3
		System.out.println("The fourth element is:" + list.get(3));//Display stu4
	}
}

Traversal of ArrayList collection:

import java.util.ArrayList;
public class ArrayList02 {
	public static void main(String[] args) {
        //Create an ArrayList collection
        ArrayList<Integer> in = new ArrayList<>;
        
        //Add elements to the ArrayList collection
        in.add(10);
        in.add(20);
        in.add(30);
        in.add(40);
        in.add(50);

        //Traversing the ArrayList collection
        for(i = 0; i <= in.size(); i++){
            int a = in.get(i);
            System.out.println(a);
        }
    }
}

Common methods of ArrayList supplement:

public void add(int index,E element): inserts the specified element at the specified position in this collection.

public boolean remove(Object o): deletes the specified element and returns whether the deletion was successful

public E remove(int index): removes the element at the specified position in this collection. Returns the deleted element.

public void clear(): clear all elements in the collection

public E set(int index,E element): modifies the element at the specified index and returns the modified element.

Interested students can listen to the lecture video of Duoyi education teacher, with detailed method introduction and specific use methods

Doubtful points and difficulties

At the beginning of learning custom classes, I felt it was still very simple, but it was not easy to think about the follow-up of custom classes in combination with other contents. The bottom layer of the ArrayList set is the array. The contents of the array should be kept in mind in order to better apply the set. As for the use method of the set, you need to remember a lot.

Self feeling

It's not very difficult to learn, but when you really let yourself type the code, plus you need to use your brain to think, it takes a long time. I think you should learn some simple words and meanings. After all, you still feel too low if you don't understand some words when typing the code in computer language ~ I think I can continue to make progress! come on.

Special Thanks

For learning content, refer to the introduction course of zero basic big data of Duoyi education. Interested friends can search Duoyi education by themselves. I hope we can work together and make progress together!

Topics: Java