gpt4 book ai didi

java - 我想要一个包含 100 名具有属性的学生的列表。我怎样才能在java中拥有这样一个列表而不使用add.list() 100次?

转载 作者:行者123 更新时间:2023-12-01 17:47:08 24 4
gpt4 key购买 nike

我尝试了传统的方法来创建列表并使用 Credentials 类添加详细信息

新建.java

import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

public class New {

public static void main(String[] args) {
List<Credentials> stud = new ArrayList<>();

Credentials c1 = new Credentials("Aditya", 1, 15);
Credentials c2 = new Credentials("Ramesh", 2, 15);
Credentials c3 = new Credentials("Suresh", 3, 15);
Credentials c4 = new Credentials("Mahesh", 4, 15);
Credentials c5 = new Credentials("Naresh", 5, 15);
Credentials c6 = new Credentials("Sarvesh", 6, 15);
Credentials c7 = new Credentials("Jayesh", 7, 15);
Credentials c8 = new Credentials("Paresh", 8, 15);
Credentials c9 = new Credentials("Nilesh", 9, 15);
Credentials c10 = new Credentials("Yogesh", 10, 15);
Credentials c11 = new Credentials("Mahi", 11, 15);
Credentials c12 = new Credentials("Lonesh", 12, 15);
Credentials c13 = new Credentials("Prakash", 13, 15);
Credentials c14 = new Credentials("Akash", 14, 15);
Credentials c15 = new Credentials("Surya", 15, 15);
Credentials c16 = new Credentials("Dinesh", 16, 15);
Credentials c17 = new Credentials("Saresh", 17, 15);

stud.add(c1);
stud.add(c2);
stud.add(c3);
stud.add(c4);
stud.add(c5);
stud.add(c6);
stud.add(c7);
stud.add(c8);
stud.add(c9);
stud.add(c10);
stud.add(c11);
stud.add(c12);
stud.add(c13);
stud.add(c14);
stud.add(c15);
stud.add(c16);
stud.add(c17);

Iterator<Credentials> iterator = stud.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}

最佳答案

你可以使用:

List<Credentials> stud = Arrays.asList(new Credentials("Aditya", 1, 15), 
new Credentials("Ramesh", 2, 15),
new Credentials("Suresh", 3, 15),
....);

请注意,列表 螺柱无法在结构上进行修改,即您无法添加删除其中的任何元素现在就可以了。

关于java - 我想要一个包含 100 名具有属性的学生的列表。我怎样才能在java中拥有这样一个列表而不使用add.list() 100次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53976846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com