This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Continue today ~

The enumeration

  • Commonly used to represent a set of constants, such as four seasons in a year
  • Use keywordsenumdefine
enum Game{
    CSGO,LOL
}
Copy the code

Enumerations are used in inner classes

public class Test{
    enum Game{
        CSGO,LOL
    }
    public static void main(String[] args){
        Game g1 = Game.CSGO;
        System.out.println(g1);/ / output CSGO}}Copy the code

Ps: Enumerations can be interpreted as lists of identical key-value pairs

Iterated enumeration element

enum Game{
    CSGO,LOL
}
public class Test{
    public static void main(String[] args){
        for(Game games : Game.values()){ System.out.println(games); }}}Copy the code

Ps: values() can fetch all the values, using Java to enforce the for loop to loop the values and print them out

Use enumerated classes in Switch

enum Game{
    CSGO,LOL
}
public class Test{
    public static void main(String[] args){
        Game g1 = Game.CSGO;
        switch(g1){
            case CSGO:
                System.out.println(counter-Strike);
                break;
            case LOL:
                System.out.println(League of Legends);
                break; }}}Copy the code

Ps: Enumeration instantiates the obtained values, the individual values themselves, which can be used directly in the switch for matching

values().ordinal()valueOf()methods

values()– Returns all values in the enumeration

ordinal()– Returns all indexes in the enumeration

valueOf()– Returns an enumeration constant for the specified string

enum Game{
    CSGO,LOL
}
public class Test{
    public static void main(String[] args){
        Game[] arr = Game.values();
        
        for(Game gow : arr){
            System.out.println(gow);/ / value
            System.out.println(gow.ordinal());/ / index
        } 
        System.out.println(Game.valueOf("LOL")); }}Copy the code

Enumeration class

  • Enumerations can have their own variables, methods, constructors
  • The constructor of the enumeration must be private
  • If an enumeration class has an abstract method in it, its instance must implement it
enum Game{
    CSGO,LOL;
    
    private Game(a)
    {
        System.out.println("the game is start");
    }
    public void GameMode(a)
    {
        System.out.println("wa ha ha"); }}Copy the code

package

  • Used to distinguish namespaces
  • Put functional-related classes or interfaces in a package for easy application
  • Defined by the package keyword, write first

For example, a package with a path of code/ Java /hello. Java would be defined as:

package code.java
public class hello{}Copy the code

Java package

java.lang– Package basic classes

java.io– Functions that contain input/output functions

Create a package

Write two files with the same package:

package game;

interface Game{
    public void watch(a);
    public void play(a);
}
Copy the code
package game;
public class Computer implements Game{
    public void watch(a){
        System.out.println("i watching you");
    }
    public void play(a){
        System.out.println("i want play");
    }
    public static void main(String[] args){
        Computer cm = newComputer(); cm.watch(); cm.play(); }}Copy the code

Then compile them and create the package with mkdir:

mkdir game
cp Game.class Computer.class game
java game/Computer
Computer watch
Computer play
Copy the code

The import keyword

  • Use the import keyword to import the desired package
importThe package name. *Copy the code

Ps: * represents the import of everything in the package. You can import the corresponding class by replacing the * with the corresponding class name

The directory structure of package

There are two situations when a class is placed in a package:

  • The package name becomes part of the class name
  • The package name must match the directory structure

Java data structure

The enumeration

  • The enumeration interface defines a way to retrieve contiguous elements from a data structure

A collection of

  • The bitset class implements a set of bits or flags that can be set and cleared individually
  • Useful for setting and clearing booleans

vector

  • Vectors are very similar to traditional arrays, but they can change dynamically

The stack

  • Think of it as an open container, in and out

The dictionary

  • It’s a collection of key-value pairs
  • It is an abstract class

Hash table

  • Is a means of organizing data based on user-defined key structures

attribute

  • The value for each key in the property list is a string

That’s all for today. Good night