How to create a sequential list in Java and input data
In Java, an array can be used to implement a sequential list and populate data through user input. Below is an example code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 获取顺序表的长度
System.out.print("请输入顺序表的长度:");
int size = scanner.nextInt();
// 创建顺序表的数组
int[] list = new int[size];
// 输入数据
System.out.println("请输入顺序表的数据:");
for (int i = 0; i < size; i++) {
list[i] = scanner.nextInt();
}
// 打印顺序表
System.out.println("顺序表的数据为:");
for (int i = 0; i < size; i++) {
System.out.print(list[i] + " ");
}
}
}
After running the program, first enter the length of the sequential table, then input the data one by one. Finally, the program will print out the input sequential table data.
Note: The example program assumes that the user inputs are all integers, if other types of data need to be processed, the code needs to be modified accordingly.