在Java中创建示例时可以使用“链接列表”吗?

创建实例时可以使用链表吗?

示例:

我有Main类和Employee类。

Main.class:

 Employee a1 = new Employee();

创建实例时,通常执行如下代码。

但是我想知道是否有任何方法可以使用链表创建实例。

赞:

LinkedList<String> alist = new LinkedList<String>();  
Employee alist(index) = new Employee();
je8600 回答:在Java中创建示例时可以使用“链接列表”吗?

您已经创建了一个import org.apache.spark.sql.types._ val df = Seq( (1L,BigDecimal(12.34),"a",BigDecimal(10.001)),(2L,BigDecimal(56.78),"b",BigDecimal(20.002)) ).toDF("c1","c2","c3","c4") val newSchema = df.schema.fields.map{ case StructField(name,_: DecimalType,nullable,_) => StructField(name,DoubleType,nullable) case field => field } ,它不能容纳List<String>的实例,但是可能包含它们的名称...

您似乎想要这样的东西:

Employee

如果您有一个Employee employee = new Employee(); // maybe set some attributes (the next line is a guess due to a lack of information) employee.setName("E. M. Ployee"); // create a list of employees List<Employee> employees = new LinkedList<>(); // and add the employee employees.add(employee); 和一个List<String>,看起来与此类似

class Employee

然后,您可以为名称列表中的每个名称创建class Employee { private String name; public Employee(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 的实例:

Employee
,

您只需在将元素添加到列表中时实例化该元素

alist.add(new employee());

给出您的代码,您还应该更改声明。因此结果将是:

LinkedList<Employee> alist = new LinkedList<>();
 alist.add(new Employee());
,
List<Employee> employees = new LinkedList<>();

employees.add(new Employee(...,...,...));

Employee empMarta = new Employee("Marta","Green",24);

employees.add(empMarta);
  1. 也许最好使用一些SET接口实现(Set具有唯一的元素列表)?
  2. 对于Employee类,您应该覆盖equals()和hashCode()方法;
  3. 最主要的是:如果您决定使用List,那么您应该回答“您计划执行(添加,搜索,删除)最频繁的操作是什么?”的问题。根据答案,您应该在ArrayList和LinkedList之间进行选择。
本文链接:https://www.f2er.com/3161114.html

大家都在问