3/7 of the Java Foundation in 7 days

API overview

What is API?

API (Application Programming lnterface), application programming interface.

The so-called API is the value of many classes, many methods, JDK gives us a lot of ready-made classes, we can use them directly, these classes are API.

API Official Documentation

The Use of Scanner Classes in Several Common API s

  1. Guide bag

    Because Scanner classes exist under the java.lang package, all of them can be used directly without importing.

  2. Establish

    Class name object name = new class name ();

  3. Use

    Object name. Method name ();

public class AScanner{
    public static void main(String[] args){
        System.out.println("Please enter a number.")
        //System.in represents input from the keyboard
        Scanner sc = new Scanner(System.in);
        //Call Scanner's method to get an integer
        int num = sc.nextInt();
        System.out.println("number"+num);
    }
}

Scanner's exercises

import java.util.Scanner;

public class MaxInput {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the first number");
        int a = sc.nextInt();
        System.out.println("Please enter a second number.");
        int b = sc.nextInt();
        System.out.println("Please enter a third number");
        int c = sc.nextInt();
        int max ;
        if(a>b){
            max = a;
        }else{
            max = b;
        }
        if(max>c){
            System.out.println("The maximum value is"+max);
        }else{
            max = c;
            System.out.println("The maximum value is"+max);
        }

    }
}

Anonymous object

Anonymous objects are objects on the right, with no names or assignment operators on the left.

Format of anonymous objects:

new class name ();

Anonymous objects can only be used once. Next time you have to create a new object.

Every new is a new object, so you can only use it once.

public class Person{
    String name;
    public void showName(){
        System.out.println("My name is"+name);
    }
}
public class niming{
    public static void main(String[] args){
        Person one = new Person();
        one.name =( "Zhang Fei");
        one.showName();
        
        new Person().name = "LV Meng";
        new Person().showName(); //Can't print out Lu Meng. Every new is a new object.
        
    }
}

Random of several common API s

public static void main(String[] args){
        //create object
        Random r = new Random();
        //Call a method to randomly generate an integer with no bounds
        int i = r.nextInt();
        System.out.println(i);
    }
 public static void main(String[] args){
        Random r = new Random();
        for(int j = 0;j<100;j++){
            //Get a random number of 0-9.
            int i = r.nextInt(10);
            System.out.println(i);
        }
    }

Exercise - Guess Numbers Game

thinking

If you want to guess a number, you must first determine the number. That is, here, the first step is to use Random class to get a random number.

With random numbers, you can guess that if you want users to enter random numbers, you need to use the Scanner class in the API.

Users can't guess at once, so they need to guess again and again. Here we use the while loop.

Guess a number is big, small, right, three results, so you can use the select structure if statement here.

import java.util.Random;
import java.util.Scanner;
//The variable name rn is the abbreviation of random number, sn, is the abbreviation of keyboard input number.
public class aGame{
    public static void main(String[] args) {
        Random aRandom = new Random();
        //Get a random integer
        int rn = aRandom.nextInt(101);
       // System.out.println(rn);//Cheating Act
        Scanner aScanner = new Scanner(System.in);
        System.out.println("Please enter a 0-100 Integers between.....");
        //Integer input by user
        boolean mark = true;
        while (mark) {
            int sn = aScanner.nextInt();
            if (sn > rn) {
                System.out.println("The number entered is too large. Please guess in a smaller direction.");
            } else if (sn < rn) {
                System.out.println("The number entered is smaller.,Please guess in the big direction.");
            } else {
                System.out.println("Congratulations, guess right");
                mark = false;
            }
        }
        System.out.println("The game is over. Thank you for your participation.!!!!!!!!!!!!!!");
    }

}

ArrayList collection

The length of ArrayList's memory can be changed.

public static void main(String[] args){
    //Create an ArrayList collection with the name list, which contains data of String string type.
    //Note: After JDK 1.7, the brackets on the right can't be used.
    ArrayList<String> list = new ArrayList<>();
    System.out.println(list);//The content in [] will be printed. If there is no content, the output will be []
    //To add data to a collection, you need to use the add method.
    list.add("Zhao Yun");
    System.out.println(list);
}

Common methods and traversal of ArrayList sets

Common methods are:
  1. Public Boolean add (E); add elements to the collection, with parameter types consistent with generic types

  2. public E get(int index); Gets elements from a collection with an index number as the parameter.

  3. public int size(), gets the scale length of the collection, and the return value is the number of elements contained in the collection.

  4. public E remove(int index), deleting elements from a collection, with an index number as the parameter and a return value as the deleted element

 public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        //Use of add method
        list.add("Jia Xu");
        list.add("Zhou Yu");
        list.add("Guo Jia");
        System.out.println(list);//[Jia Jian, Zhou Yu, Guo Jia]
        //Get elements from a collection, get method, index from 0
        String aname = list.get(2);
        System.out.println(aname);//Guo Jia
        //Delete elements from collections
        list.remove(1);
        System.out.println(list);//[Jia Huo, Guo Jia]
        //Get the length of the collection
        System.out.println(list.size());//2
        list.add("Zhu Geliang");
        //Traversal of Sets
        for(int i = 0;i<list.size();i++){
            System.out.println(list.get(i));
        }
    }
ArrayList Collection Stores Basic Data Types

ArrayList collections cannot store primitive types. If you want to store primitive types, you must use the wrapper classes corresponding to the primitive types.

Basic types Packaging class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
public static void main(String[] args){
    ArrayList<Integer> list = new ArrayList<>();
    list.add(100);
    list.add(200);
    System.out.println(list);//[100, 200]
    //Auto unpacking, packaging type becomes basic type
    System.out.println(list.get(0));//100
}

Starting from JDK 1.5, it supports automatic packing and unloading.

Automatic Packing: Basic Type - --> Packing Type

Automatic Unpacking: Packing Type - --> Basic Type

Exercise 1 of the ArrayList Collection

Topic: Generate 6 random integers between 1 and 33, add them to the set, and traverse the set

My way of solving problems is:

The Random class is used to generate six random integers.

To add a generated integer to a collection, you create a collection.

Use the set method add to add all collections, so there will be aList. add (random number) here, so that the random number is added to the collection. Repeated addition can use the for loop.

Next is traversal.

import java.util.ArrayList;
import java.util.Random;

public class AArray {
    public static void main(String[] args) {
        Random aRandom = new Random();
        ArrayList<Integer> aList = new ArrayList<>();
        for(int i=0;i < 6 ;i++){
            aList.add(aRandom.nextInt(33)+1);
            System.out.println(aList.get(i));
        }
        System.out.println(aList);
    }
}
Exercise 2 of ArrayList
Title: Customize four student objects, add them to the collection, and traverse
public class Student {
    private int age;
    private String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Student() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
import java.util.ArrayList;

public class AStudent {
    public static void main(String[] args) {
        Student one = new Student(25,"army officer's hat ornaments");
        Student two = new Student(26,"Xi Shi");
        Student three = new Student(27,"Wang Zhaojun");
        Student four = new Student(28,"Yang Yuhuan");
        ArrayList<Student> arrayS = new ArrayList<>();
        arrayS.add(one);
        arrayS.add(two);
        arrayS.add(three);
        arrayS.add(four);
        for (int i=0;i<arrayS.size();i++ ){
            //Use get to get the object out
            Student stu = arrayS.get(i);
            //Use the object name. Method name to get the age and name, and here's the end of the traversal.
            System.out.println("Age"+stu.getAge()+"..."+"Full name"+stu.getName());
        }

    }
}
ArrayList Set Exercise 3
Title: Use a large set to store 20 random numbers, and then filter even elements into a small set.
import java.util.ArrayList;
import java.util.Random;
public class AArray {
    //Title: Use a large set to store 20 random numbers, and then filter even elements into a small set.
    public static void main(String[] args) {
    //Large set, store 20 random numbers
    ArrayList<Integer> big = new ArrayList<>();
    //Small set, even number
    ArrayList<Integer> small = new ArrayList<>();
    Random r = new Random();
    for(int i=0;i<20;i++) {
        //Add a random number to a large collection and add one at a time in a loop
        big.add(r.nextInt(20));
        }
    System.out.println(big);
    
    for(int i=0;i<big.size();i++) {
        int num = big.get(i);
        if(num%2==0) {
            //Add even numbers to small sets
            small.add(num);
        }
    }
    System.out.println(small);
    
    }
}
String overview and features

Characters of strings:

  1. The contents of strings are immutable

  2. Because strings cannot be changed, strings can be shared and used.

  3. String effect is equivalent to char [] character array, but the underlying principle is byte [] byte array.

Constructing Method and Direct Creation of Strings
public static void main(String[] args){
            //Using empty parameter construction
            String str1 = new String();
            System.out.println("The first string"+str1);
            //Create strings from character arrays
            char[] charArray = {'A','B','C'};
            String str2 = new String(charArray);
            System.out.println("The second string"+str2);
            //Create strings from byte arrays
            byte[] byteArray = {97,98,99};
            String str3 = new String(byteArray);
            System.out.println("The third string"+str3);
            //Create directly
            String  str4 = "Hello";
            System.out.println("Fourth"+str4);
        }
Constant pool of strings

A pool of string constants, in which double quotation strings are written directly in a program, are in the pool of string constants.

For basic types=== is a comparison of values.

For reference types, == is a comparison of addresses.

public static void main(String[] args){
        String str1 = "abc";//Address exists in a constant pool
        String str2 = "abc";//Address existence constant pool
        char[] charArray = {'a','b','c'};
        String str3 = new String(charArray);//Address no longer in constant pool
        System.out.println(str1 == str2);//true
        System.out.println(str1 == str3);//false
        System.out.println(str2 == str3);//false
        
    }
Relevant methods of string comparison
public static void main(String[] args){
        String str1 = "hello";
        String str2 = "hello";
        char[] charArray = {'h','e','l','l','o'};
        String str3 = new String(charArray);
        //equals compares the contents of two strings
        System.out.println(str1.equals(str2));//true
        System.out.println(str2.equals(str3));//true
        System.out.println(str3.equals("hello"));//true
 //Ignore the case comparison method equals IgnoreCase
 System.out.println(str3.equalsIgnoreCase("Hello"));//true
 
    }

String acquisition related methods

public static void main(String[] args){
       //Gets the length of the string;
        int length = "wertrghgffdvscsegh".length();
        System.out.println("The length of the string is "+length);
        
        //Stitching strings
        String str1=  "hello";
        String str2 = "world";
        String str3 = str1.concat(str2);
        System.out.println(str3);
        
        //Gets a single character at the specified index position
        char ch = "hello".charAt(1);
        System.out.println("The character at index 1 is"+ch);
        
        //Find the first index position of the parameter string that appears in the native string
        //If no - 1 is returned
        String original = "helloworld";
        int i =original.indexOf("llo");
        System.out.println("The first index location to appear is"+i);
    }
String Conversion Related Method
public static void main(String[] args){
        //Converting strings into character arrays
       char[] chars = "Riding on the wind and breaking the waves".toCharArray();
       for(int i=0;i<chars.length;i++) {
           System.out.println(chars[i]);
       }
       //Converting strings to byte arrays
       String str = "Gan Mianbi's Ten Years of Reading";
       byte[] bytes = str.getBytes();
       for(int i=0;i<bytes.length;i++) {
           System.out.println(bytes[i]);
       }
       //Content substitution of strings
       String str3 = "The sound of wind, rain, reading is in the ear";
       String str4 = str3.replace("sound", "sheng");
       System.out.println(str4);
        //String cutting method returns a string []
       String[] str5 =str3.split("sound");
       for(int i=0;i<str5.length;i++) {
           System.out.println(str5[i]);
       }
    }

Keywords: Java JDK Programming

Added by tbare on Sat, 18 May 2019 00:14:56 +0300