Java course content review (collections class, enumeration, random number, wrapper class, string class)

1.collections class :

*1. Comparable : Interface

*2 compareTo() method: size comparison of elements;

**If the objects of a class are compared in size: first, the class must implement the Comparable interface, which forces the overall ordering of the objects of each class that implements it. This ordering is called the natural ordering of the class. Then the compareTo() method of the class is called as his natural compare method.

*3. sort() method: sorts the elements.

*4. binarySearch() method: search for elements;

2. Enumeration:

*1. It is a type composed of a set of fixed constants;

*2. Represented by "enum" ; (public enume class name)

*3. The enumeration constants are separated by commas, which are all uppercase letters.

3. Random number:

*1.int random=(int)(Math.random()*10);//Generate random numbers

*2.Random random=new random()//seed

   int rand=random.nextInt(natural number)//Generate random number

4. Packaging class :

*1. Basic data types are converted to wrapper types:

Integer  in= new  Integer(10)

Boolean  in=new  Boolean(true)

Character  in=new  Character('男')

*2. Convert string to wrapper class:

Integer   in=new    Integer("19")

Boolean in=new Boolean("true")//There are only two cases in the process of converting the boolean type to a string: true, false, no matter which letter in "true" is uppercase or lowercase, the result is true, input other are both false.

*3.valueOf method:

Integer  in=Integer.valueOf("21");

Boolean   in=Boolean.valueOf("true");

*4. The wrapper class becomes the basic type: Value

Integer   in= new  Integer(25)

int  i= in.intValue( );

Boolean   in=new  Boolean(true)

boolean   i=in.booleanValue( );

*5. The basic type becomes a string:

int num=9;

@1.  String   strNum=Integer.toString(num);

@2.String  strNum=num+" ";

*6. String becomes basic type: (except Character)

String   s="89";

int num=Integer.paseInt(s);//The paseInt keyword is the key to turning a string into a basic type;

5. Common methods of the String class:

*1. Find the length of the string: Length();

*2. Comparison of strings: equals();

Example: System.out.println("Please enter the user name");
        test.setName(input.next());
        System.out.println("Please enter the password");
        test.setCode(input.next()) ;
        if (test.getName().length()>=3&&test.getCode().length()>=6) {
            System.out.println("Please enter the password again");
            String code1=input.next() ;
            if (test.getCode().equals(code1)) {
                System.out.println("Successful registration, please remember your password");
                System.out.println("Your current username is: "+test .getName());
                System.out.println("Your current password is: "+test.getCode());
                flag=true;
            }else {
                System.out.println("The two entered passwords are not the same" );
                flag=false;
            }
        }else if (test.getName().length()<3||test.getCode().length()<6) {
            System.out.println("The length of the username cannot be less than 3, and the length of the password cannot be less than 6");
            flag=false;
        }

       





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324850141&siteId=291194637