JAVA Series -> API (Application Programming Interface), Scanner type, Random class, ArrayList class

1 API

API (Application Programming Interface), an application programming interface. Java API programmer is a dictionary, is available to the JDK classes we use documentation. These classes encapsulate the underlying code that implements them, we do not care how these classes are implemented, only need to learn how to use these classes. So we can query the API way to learn Java classes offered, and learned how to use them.

API use the steps

  1. Opens the help file.
  2. Click on display, find the index, see the input box.
  3. Whom are you calling? Input in the input box, then press Enter.
  4. Look at the package. java.lang class does not need conductivity in the package, other needs.
  5. Explained and illustrated look like.
  6. Learning constructor.
  7. Use member method.

2 Scanner class

Learn to use the API, we pass the Scanner class, familiarize yourself with the query API, and use the step class.

2.1 What is the Scanner class

A scanner can resolve simple text strings and basic types. For example, the following code allows a user to read a number from the System.in:

Scanner sc = new Scanner(System.in); 
int i = sc.nextInt();

Reference types step 2.2

2.2.1 guide package

Using the import keyword leader packet, until all code packet type conductivity, the introduction of the type to be used, without importing all the classes in the java.lang package. format:import 包名.类名;

2.2.2 Creating Objects

Using the constructor method of the class, create an object class. Format: 数据类型 变量名 = new 数据类型(参数列表);
For example:Scanner sc = new Scanner(System.in);

2.2.3 calling method

Call to a member method of the class, complete the assigned functions. Format: 变量名.方法名(); 例如:int I = sc.nextInt (); // a keyboard receiving an integer entered `

2.3 Scanner using procedure

View class

java.util.Scanner: after introduction of such use need to import.

View constructor

public Scanner (InputStream source): Constructs a new Scanner, produces values ​​specified from the input stream is scanned.

View member method

public int nextInt (): the input of the next scanning information marker as an int value.

Scanner class used to complete the operation of receiving the keyboard data entry, as follows:
Here Insert Picture Description

2.4 Exercises

Summing two data and keyboard input, as follows:

  public static void main(String[] args) {
        // 创建对象 
        Scanner sc = new Scanner(System.in);
        // 接收数据 
        System.out.println("请输入第一个数据:");
        int a = sc.nextInt();
        System.out.println("请输入第二个数据:");
        int b = sc.nextInt();
        // 对数据进行求和 
        int sum = a + b;
    }

Anonymous objects

When you create an object, only to create an object statement, but did not address the value of the object is assigned to a variable. Although the object is created to simplify the wording, but the scenario is very limited.
Here Insert Picture Description

1. Create an anonymous object directly call a method, not a variable name.

new Scanner(System.in).nextInt();

2. Once the method is called twice, is to create two objects, resulting in waste, as follows:

An anonymous object, can only be used once.

new Scanner(System.in).nextInt(); 
new Scanner(System.in).nextInt();

3. The method of anonymous objects as arguments and return values

Here Insert Picture Description
Here Insert Picture Description

3 Random category

3.1 Overview

Examples of Random class is used to generate pseudo-random numbers.
For example, the following code allows a user to get a random number:

Random r = new Random(); 
int i = r.nextInt();

3.2 Random use steps

View class java.util.Random:

Such use requires that the import after introduction.

View Constructors public Random ():

Creates a new random number generator.

View member method public int nextInt (int n):

It returns a pseudo-random number, the range of values between 0 int (including) and the specified value n (exclusive) .

Random class used to complete the operation generates a random integer within 10 3, as follows:
Here Insert Picture Description
the PS :: Create a Random object, each call to the nextInt () method, a random number is generated.

4 ArrayList class

4.1 introduced - an array of objects

Students use an array to store student three objects, as follows:
Here Insert Picture Description
So far, we want to store object data, select the container, only an array of objects. The length of the array is fixed and can not adapt to changing data requirements. To solve this problem, Java provides another container java.util.ArrayList collections, so that we can more convenient to store and manipulate data objects.

4.2 What is the ArrayList class

    java.util.ArrayList 是大小可变的数组的实现,存储在内的数据称为元素。此类提供一些方法来操作内部存储 的元素。 ArrayList 中可不断添加元素,其大小也自动增长。

4.3 ArrayList using the steps

4.3.1 View class

java.util.ArrayList: after introduction of such import need to make use. , Shows a specified data type, called generic. E, taken from Element (elements) of the first letter. E appears in place of, we use a reference data type can replace it, which means we will store reference type elements. code show as below:

ArrayList<String>,ArrayList<Student>

4.3.2 View constructor

public ArrayList (): a content is configured to empty set.

public ArrayList() :构造一个内容为空的集合。

The basic format:

ArrayList<String> list = new ArrayList<String>();

After JDK 7, the right angle brackets within the generic may be left blank, but <> still write.
Simplified format:

ArrayList<String> list = new ArrayList<>();

4.3.3 View member method

public boolean add (E e): the specified element to the end of the collection. Parameter E e, when constructing ArrayList object specifies what type of data, then add (E e) of the method, the object can add what data types.

Use ArrayList class, three strings storage elements, as follows:

 public static void main(String[] args) {
        //创建学生数组
        ArrayList<String> list = new ArrayList<>();
        // 创建学生对象 
        String s1 = "曹操";
        String s2 = "刘备";
        String s3 = "孙权";
        // 打印学生ArrayList集合
        System.out.println(list);
        // 把学生对象作为元素添加到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);
        //打印学生ArrayList集合 
        System.out.println(list);
    }

4.4 commonly used methods and traversal

For the operating element, is reflected in the basic - add, delete, search.
Commonly used methods are:

 public boolean add(E e) :将指定的元素添加到此集合的尾部。 
 public E remove(int index) :移除此集合中指定位置上的元素。返回被删除的元素。 
 public E get(int index) :返回此集合中指定位置上的元素。返回获取的元素。 
 public int size() :返回此集合中的元素数。遍历集合时,可以控制索引范围,防止越界。 

These are the most basic method of operation is very simple, as follows
Here Insert Picture Description

4.5 How to store basic data types

ArrayList object can not be stored basic types, the type of data stored reference only. Similarly you can not write, the memory type corresponding to the basic data type of packaging are possible. So, you want to store basic types of data in order to write the data type in <>, must be converted, the conversion worded as follows: Here Insert Picture Description
We found that only Integer and Character require special memory, but other basic types can be capitalized.
Then stores basic data types, as follows:

  public static void main(String[] args) {
            ArrayList<Integer> list = new ArrayList<Integer>();
            list.add(1);
            list.add(2);
            list.add(3);
            list.add(4);
            System.out.println(list);
        }
Published 37 original articles · won praise 24 · views 653

Guess you like

Origin blog.csdn.net/qq_16397653/article/details/103747980