Java program input, output, dynamic array, list, type conversion, string traversal

Introduction to Java (you can skip it here, the reason for writing this part is mainly to avoid CSDN's unsmart posting assistant): Java is an object-oriented programming language, which not only absorbs various advantages of C++ language, but also abandons Concepts such as multiple inheritance and pointers that are difficult to understand in C++, so the Java language has two features: powerful and easy to use. As a representative of the static object-oriented programming language, the Java language perfectly implements the object-oriented theory and allows programmers to perform complex programming in an elegant way of thinking. Java has characteristics such as simplicity, object-oriented, distributed, robustness, security, platform independence and portability, multithreading, and dynamics. Java can write desktop applications, Web applications, distributed systems and embedded system applications, etc.

In normal times, we often use Java. Sometimes we may use it to write a dynamic web application, or a mobile APP application, or to write questions on Leecode. However, when getting started with Java, we often encounter the following problems, so I will make a brief summary here.

1. Input (the tested code can be copied and run directly, and can be modified according to the needs)

        1. Enter the packages that must be used

import java.util.Scanner;

        2. Enter an integer value

import java.util.Scanner;
public class Enter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        System.out.println(a);
    }
}

        3. Enter a set of integer values

import java.util.Scanner;
public class Enter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = 3;
        int []a = new int[m];       // 定义长度为m的一维数组
        for (int i=0; i<m; i++){    // 循环输入m个数
            a[i] = sc.nextInt();
        }
        System.out.printf("%d %d %d\n", a[0], a[1], a[2]);
    }
}

        4. Input a floating-point value (please refer to the int type data above for how to enter a set of floating-point values)

import java.util.Scanner;
public class Enter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        float a = sc.nextFloat();
        System.out.println(a);
    }
}

        5. Enter a string

import java.util.Scanner;
public class Enter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.nextLine();
        System.out.println(a);
    }
}

        Note: Scanner objects accept strings. I suggest that when receiving numeric data, use the same Scanner object to receive it. When it is necessary to receive a string, a new Scanner object is used to receive the string specifically. Otherwise, you may encounter strange problems when typing.

2. Output

        1. Format the output (I strongly recommend here, just master the following), the following code includes

        output of integer data,

        output of floating-point data,

        Input of custom decimal floating-point data,

        and the output of the string

public class Enter {
    public static void main(String[] args) {
        int a = 10;
        float b = 1.0f;
        String c = "Hello world!";
        System.out.printf("a=%d, b=%f, b=%.3f, c=%s", a, b, b, c);
    }
}

        program output

a=10, b=1.000000, b=1.000, c=Hello world!

3. Dynamic array/list

        1. One-dimensional dynamic array and its input method

        The requirement comes from: For example, I input a value m, and hope to assign me a dynamic array with a length of m.

import java.util.Scanner;
public class Enter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);    // 设置一个Scanner对象
        int m = sc.nextInt();                   // 输入数组长度m
        int []array = new int[m];               // 分配一个长度为m的动态数组
        for(int i=0; i<m; i++){                 // 输入m个数字
            array[i] = sc.nextInt();
        }
        for(int i=0; i<m; i++){                 // 输出
            System.out.printf("%d ", array[i]);
        }
    }
}

        Program output (the first two lines are input, the last line is output)

3
1 2 3
1 2 3 

        2. Two-dimensional dynamic array and its input method

import java.util.Scanner;
public class Enter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);    // 设置一个Scanner对象
        int m = sc.nextInt();                   // 输入行数m
        int n = sc.nextInt();                   // 输入列数n
        int [][]array = new int[m][n];          // 分配一个行数为m、列数为n的动态数组
        for(int i=0; i<m; i++){                 // 输入m行n列个数字
            for (int j=0; j<n; j++){
                array[i][j] = sc.nextInt();
            }
        }
        for(int i=0; i<m; i++){                 // 输出
            for (int j=0; j<n; j++){
                System.out.printf("%d ", array[i][j]);
            }
            System.out.printf("\n");
        }
    }
}

        Program output (the first four lines are input, and the last three lines are output)

3 2
1 99
2 100
3 101
1 99 
2 100 
3 101 

        3. List (can be regarded as a dynamic array)

        The package that must be used (you can use one, I recommend using ArrayList)

import java.util.ArrayList;

        Append, delete, change, find an item, and list traversal

import java.util.ArrayList;
public class Enter {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        // 增加item(分别追加0、1、2、3)
        list.add(0);                         // 传入的参数为Integer对象
        list.add(1);
        list.add(2);
        list.add(3);
        output_list(list);
        // 删除item(即删除最后一个item)
        list.remove(list.size()-1);          // 传入的参数为index
        output_list(list);
        // 改变item(改变最后一个元素的值为999)
        list.set(list.size()-1, 999);        // 传入的参数分别为index、new value
        output_list(list);
        // 查询item:一种是通过下标,另外一种是自己遍历匹配查询,这里用的是前者
        System.out.printf("%d", list.get(0));// 查询index为0的元素
    }

    // 遍历list
    public static void output_list(ArrayList<Integer> list){
        int n = list.size();
        for (int i=0; i<n; i++){
            System.out.printf("%d ", list.get(i));
        }
        System.out.printf("\n");
    }
}

        program output

0 1 2 3 // 分别追加 0、1、2、3
0 1 2   // 删除最后一个元素3
0 1 999 // 更新最后一个元素2的值更新为999
0       // 查询index=0的元素0

        4. Queue (expanded by list)

        The idea of ​​a queue is first in first out. If the queue is implemented by a list, then the first element of the list can be defined as the head of the queue, and the last element can be defined as the tail of the queue. At this point, the enqueue operation is to add an item to the list; the dequeue operation is to delete the first item in the list.

        Example of enqueue and dequeue

import java.util.ArrayList;
public class Enter {
    public static void main(String[] args) {
        ArrayList<Integer> queue = new ArrayList<>();
        // 689、999、9999依次入队
        queue.add(689);
        queue.add(999);
        queue.add(9999);
        output_list(queue); // 输出一下queue(实际上queue不能这样输出,因为它是队列)
        // 出队
        queue.remove(0);
        output_list(queue); // 输出一下queue(实际上queue不能这样输出,因为它是队列)
    }

    // 遍历list
    public static void output_list(ArrayList<Integer> list){
        int n = list.size();
        for (int i=0; i<n; i++){
            System.out.printf("%d ", list.get(i));
        }
        System.out.printf("\n");
    }
}

        program output

689 999 9999 // 顺序执行3次入队
999 9999     // 执行1次出队

        5. Stack (expanded by list)

        The idea of ​​the stack is first in last out. If the stack is implemented by a list, then the last element of the list can be defined as the top of the stack. At this time, the push operation is to add an item to the list; the push operation is to delete the last item of the list.

        Push and pop examples

import java.util.ArrayList;
public class Enter {
    public static void main(String[] args) {
        ArrayList<Integer> stack = new ArrayList<>();
        // 689、999、9999依次入栈
        stack.add(689);
        stack.add(999);
        stack.add(9999);
        output_list(stack); // 输出一下stack(实际上stack不能这样输出,因为它是栈)
        // 出栈
        stack.remove(stack.size()-1);
        output_list(stack); // 输出一下stack(实际上stack不能这样输出,因为它是栈)
    }

    // 遍历list
    public static void output_list(ArrayList<Integer> list){
        int n = list.size();
        for (int i=0; i<n; i++){
            System.out.printf("%d ", list.get(i));
        }
        System.out.printf("\n");
    }
}

        output

689 999 9999 // 顺序执行3次入栈
689 999      // 执行1次出栈

4. Mandatory type conversion int, float, String

        Interchange between the three

public class Enter {
    public static void main(String[] args) {
        // int转float
        int int_a = 100;
        float float_a = (float) int_a;
        System.out.printf("int转float:a=%d,转换后,a=%f\n", int_a, float_a);

        // float转int
        float float_b = 2.34f;
        int int_b = (int) float_b;
        System.out.printf("float转int:b=%f,转换后,b=%d\n", float_b, int_b);

        // int转String
        int int_c = 1099;
        String string_c = String.valueOf(int_c);
        System.out.printf("int转String:c=%d,转换后,c=%s\n", int_c, string_c);

        // String转int
        String string_d = "2048";
        int int_d = Integer.valueOf(string_d);
        System.out.printf("String转int:d=%s,转换后,d=%d\n", string_d, int_d);

        // float转String
        float float_e = 88.88f;
        String string_e = String.valueOf(float_e);
        System.out.printf("float转String:e=%f,转换后,e=%s\n", float_e, string_e);

        // String转float
        String string_f = "99.99";
        float float_f = Float.valueOf(string_f);
        System.out.printf("String转float:f=%s,转换后,f=%f\n", string_f, float_f);
    }
}

        program output

int转float:a=100,转换后,a=100.000000
float转int:b=2.340000,转换后,b=2 // 如果b=-2.34,转int后,b=-2
int转String:c=1099,转换后,c=1099
String转int:d=2048,转换后,d=2048
float转String:e=88.879997,转换后,e=88.88
String转float:f=99.99,转换后,f=99.989998

Five, string: traversal, splicing, segmentation

        1. Strings are often encountered by Leecode, and the focus of traversal is on the charAt() method and the length() method

public class Enter {
    public static void main(String[] args) {
        String a_str = "Hello world!";
        int n = a_str.length();
        for (int i=0; i<n; i++){
            System.out.printf("%c", a_str.charAt(i));
        }
    }
}

        program output

Hello world!

        2. Splicing, for example, "hello" and "world" are spliced ​​together to get "hello world". Know one kind, use the "+" operation directly.

public class Enter {
    public static void main(String[] args) {
        String a = "Hello";
        String b = " world";
        String c = a + b;
        System.out.printf("a=%s, b=%s, c=%s", a, b, c);
    }
}

        program output

a=Hello, b= world, c=Hello world

        3. Splitting, such as splitting according to the space " ", the focus is on the split() method and the length attribute of the array.

public class Enter {
    public static void main(String[] args) {
        String a = "Hello, I am a student.";
        String[] split_result = a.split(" ");
        int n_part = split_result.length;
        for (int i=0; i< n_part; i++){
            System.out.printf("%s\n", split_result[i]);
        }
    }
}

        program output

Hello,
I
am
a
student.

Guess you like

Origin blog.csdn.net/qq_36158230/article/details/126714746