如何在arraylist中将多个变量添加为单个值?

Bze*_*zeQ 0 java arraylist

今天我需要一些帮助,将4个用户定义的字符串,2个int和4个双变量作为一个索引添加到arraylist中.

例如:(我会为你减少一些变量,并记住sc是我的扫描仪对象,测试结果超过100)

System.out.print("enter your name")
String name = sc.next();
System.out.print("enter your test results")
double results = sc.nextDouble();
System.out.print("enter your mobile number")
int mobile_num = sc.nextInt();  //I may change this to a string
Run Code Online (Sandbox Code Playgroud)

现在,从这里开始,我想捕获这些输入并将它们存储在数字1或用户名下的数组中.我还想知道如何使用这个相同的数组通过上面提到的代码存储来自用户的无限输入.

提前致谢!

And*_*lko 6

创建自己的类UserInput,保留10个字段,然后List使用此类型推广a :

List<UserInput> list = new ArrayList<>();
list.add(new UserInput(name, results, mobile_num, ...));
Run Code Online (Sandbox Code Playgroud)

如果要将用户的名称与其相应的输入相关联,最好考虑Map<String, UserInput>:

Map<String, UserInput> map = new HashMap<>();
map.put(name, new UserInput(results, mobile_num, ...));
Run Code Online (Sandbox Code Playgroud)