我尝试了使用常规方法创建列表并在Credentials类中添加Details
New.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());
}
}
}
答案 0 :(得分:2)
您可以使用:
List<Credentials> stud = Arrays.asList(new Credentials("Aditya", 1, 15),
new Credentials("Ramesh", 2, 15),
new Credentials("Suresh", 3, 15),
....);
请注意,列表 stud
无法进行结构修改,即,您现在不能添加或删除任何元素。
答案 1 :(得分:1)
从某种文件(简单的带分隔符的文本,JSON等)中读取列表。
我想编写简单的Java程序而不使用list.add很多次
从根本上讲,如果要将其嵌入到代码中,将有100条非常相似的行。这100条非常相似的行的内容在很大程度上取决于您,但是没有理由使用所有这些变量(c1
,c2
等)。
相反:
public static void main(String[] args) {
List<Credentials> stud = new ArrayList<>();
stud.add(new Credentials("Aditya", 1, 15));
stud.add(new Credentials("Ramesh", 2, 15));
stud.add(new Credentials("Suresh", 3, 15));
stud.add(new Credentials("Mahesh", 4, 15));
stud.add(new Credentials("Naresh", 5, 15));
// ...
可能甚至将其放在可重用的函数中:
private static void addStudent(stud, name, x, y) {
stud.add(new Credentials(name, x, y));
}
public static void main(String[] args) {
List<Credentials> stud = new ArrayList<>();
addStudent(stud, "Aditya", 1, 15);
addStudent(stud, "Ramesh", 2, 15);
addStudent(stud, "Suresh", 3, 15);
addStudent(stud, "Mahesh", 4, 15);
addStudent(stud, "Naresh", 5, 15);
// ...
如果学生名单固定,则根本不要使用ArrayList
,而要使用数组:
public static void main(String[] args) {
Credentials[] stud = new Credentials[] {
new Credentials("Aditya", 1, 15),
new Credentials("Ramesh", 2, 15),
new Credentials("Suresh", 3, 15),
new Credentials("Mahesh", 4, 15),
new Credentials("Naresh", 5, 15),
// ...
};
答案 2 :(得分:1)
这是我编写与您编写的代码相同的代码:
public class New {
public static void main(String[] args) {
List<Credentials> stud = new ArrayList<>();
// The only part that requires writing individual information is the names
// because each is different and there is no "rule"
String[] names = { "Aditya",
"Ramesh",
"Suresh",
"Mahesh",
"Naresh",
"Sarvesh",
"Jayesh",
"Paresh",
"Nilesh",
"Yogesh",
"Mahi",
"Lonesh",
"Prakash",
"Akash",
"Surya",
"Dinesh",
"Saresh"};
// This loop does both the creation of the credetntials and adding them
// to the list. The same thing could be done with Java 8 streams but
// I use the conventional method for simplicity.
for ( int i = 0; i < names.length; i++ ) {
stud.add( new Credentials( names[i], i+1, 15 ) );
}
// This can actually be replaced with an enhanced for loop.
Iterator<Credentials> iterator = stud.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
在现实世界中,您可能想从文件中读取名称,而不是将其写入源代码中。但是无论如何,当您使用数组而不是单独的变量时,可以使用循环和数组索引来执行重复性任务。
对于每个名字,您要做的是创建一个Credentials
对象,其中第二个参数是一个序号,最后一个参数是15。
数组的索引从0开始。序列号从1开始。这意味着仅使用i+1
就可以为您提供所需的序列号。
所以一切都折叠成以下三行:
for ( int i = 0; i < names.length; i++ ) {
stud.add( new Credentials( names[i], i+1, 15 ) );
}
现在,您可以在初始名称数组中添加100个名称,而无需更改任何内容。
答案 3 :(得分:0)
我认为此任务不是如何声明ArrayList
,而是如何存储用户凭据。这里有几个重要的部分:
id
(不是名称,因为名称可能在某天更改)与每个用户一起使用。要清楚,这是我的解决方法。
Credential
类应该包含不可变的唯一id
(或者最好是UUID)。应用程序应仅通过此ID使用给定的凭据。
public final class Credentials {
private final int id;
private final String name;
private final int mask;
public Credentials(int id, String name, int mask) {
this.name = name;
this.id = id;
this.mask = mask;
}
}
外部credetials.csv
的示例:
1,Aditya,15
2,Ramesh,15
3,Suresh,15
4,Mahesh,15
5,Naresh,15
6,Sarvesh,15
7,Jayesh,15
8,Paresh,15
9,Nilesh,15
10,Yogesh,15
11,Mahi,15
12,Lonesh,15
13,Prakash,15
14,Akash,15
15,Surya,15
16,Dinesh,15
17,Saresh,15
PermissionService
是一个单例,包含用于检查权限的所有逻辑。在此示例中,它包含一个HashMap
,其中id
是一个密钥,并且所需的凭据可能会一直可用。
此服务包含一种在启动时读取外部文件(具有已知格式)以读取当前凭据的方法。
public final class PermissionService {
private static final Pattern COMMA = Pattern.compile("\\s*,\\s*");
private static final int ID = 0;
private static final int NAME = 1;
private static final int MASK = 2;
private final Map<Integer, Credentials> map = new HashMap<>();
public void loadPermissions(Path path) throws IOException {
map.clear();
Files.lines(path)
.map(COMMA::split)
.map(parts -> new Credentials(Integer.parseInt(parts[ID]), parts[NAME], Integer.parseInt(parts[MASK])))
.forEach(credentials -> map.put(credentials.getId(), credentials));
}
}
仅此而已。是的,您可以更改外部存储类型,准备文件或更新文件的方式或文件格式。但是一般原理是相同的。
PS
如果您不想使用外部资源,则可以直接在PermissionService
中保存这些数据,而为什么要精确地初始化列表也没关系。主要情况是应将其封装在单独的服务中,并在将来需要时易于更新。