我想弄清楚如何动态地创建一个用户输入数组。我知道如何初始化一个数组,但我不知道如何让用户初始化一个数组。这里是我到目前为止有:动态创建一个整数数组
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("Enter the size of the array (3 to 10): ");
Scanner user_input = new Scanner(System.in);
int size_of_array = user_input.nextInt();
while(size_of_array < 3 || size_of_array > 10) {
System.out.println("The size of the array entered is invalid.");
System.out.print("\nEnter the size of the array (3 to 10): ");
user_input = new Scanner(System.in);
size_of_array = user_input.nextInt();
}
ArrayList array = new ArrayList(size_of_array);
System.out.println("Initial size of array: " + array.size());
}
}
我想ArrayList array = new ArrayList(size_of_array);
打算做的伎俩,但数组的大小仍然是0。希望有人能帮助我。提前致谢。
如果你正在寻找使用一个数组,你为什么要创建一个ArrayList ?使用数组,你可以使用用户输入在开始时设置它的大小 - int [] arr = new int [size_of_array]' –
我想数组列表更容易处理,因为它是动态的,但它好像是一个数组列表不是我在找什么......新手在这里 – Stephan