Java中的自定义ArrayList
自定义ArrayList可以具有多种类型的数据,并且其属性通常基于用户要求。
演示自定义ArrayList的程序如下所示-
示例
import java.util.ArrayList;public class CustomArrayList {
int n = 5;
class Employee {
int eno;
String name;
Employee(int eno, String name) {
this.eno = eno;
this.name = name;
}
}
public static void main(String args[]) {
int eno[] = {101, 102, 103, 104, 105};
String name[] = {"Jane", "Mary", "Adam", "Harry", "John"};
CustomArrayList caList = new CustomArrayList();
caList.add(eno, name);
}
public void add(int eno[], String name[]) {
ArrayList<Employee> aList = new ArrayList<>();
for (int i = 0; i < n; i++) {
aList.add(new Employee(eno[i], name[i]));
}
print(aList);
}
public void print(ArrayList<Employee> aList) {
for (int i = 0; i < n; i++) {
Employee e = aList.get(i);
System.out.println("\nEmployee Number: " + e.eno);
System.out.println("Employee Name: " + e.name);
}
}
}
上面程序的输出如下-
输出结果
Employee Number: 101Employee Name: Jane
Employee Number: 102
Employee Name: Mary
Employee Number: 103
Employee Name: Adam
Employee Number: 104
Employee Name: Harry
Employee Number: 105
Employee Name: John
以上是 Java中的自定义ArrayList 的全部内容, 来源链接: utcz.com/z/338305.html