1

A 类: 包含一个填充的 Employee 对象数组列表,每个对象都包含employeeName 和employeeNumber

B 类是否可以创建一个引用 A 类的 Employee 对象数组列表的变量?我只需要搜索那个数组列表。也许打印出来。不改变其中的任何东西。

我可以如何做到这一点的任何策略或示例?

4

8 回答 8

2

    包 p1;

    导入 java.util.ArrayList;
    导入 java.util.Collections;
    导入 java.util.List;



    公共类 ArrayReference {
        公共静态无效主要(字符串[]参数){
            新的 BB().method();
        }
    }


    AA类{
        私有列表列表 = new ArrayList();

        公共列表 getEmpList() {
            返回 Collections.unmodifiableList(list);
        }

    }

    BB类{
        公共无效方法(){
            List list = new AA().getEmpList();
            list.add(new Emp()); // 这里运行时异常会发生,如果我们
            // 尝试在列表中添加对象。
            // java.lang.UnsupportedOperationException 是 add 的异常
            // 列表中的方法

        }
    }

    类 Emp {

    }

于 2013-02-19T10:57:38.223 回答
1

我认为这就是你要找的:

Class A {
   private List<Employees> empList = new ArrayList<Employees>();

   //Setters
   //Getters

   //Load data to empList
}

Class B {

  void empList() {
      A empClass = new A();

      List<Employees> empListFromClassA = new ArrayList<Employees>();
      empListFromClassA.addAll(getEmpList()); //Getter from class A
}

希望能帮助到你

于 2013-01-29T06:03:51.950 回答
1

如果您只想在列表中搜索,请使用:

类 A { 列出我的列表 ...;

public List<Employee> getEmployees() { 
    return myList.clone();
}

}

这将返回您列表的克隆。如果您更改克隆原始列表保持不变

于 2013-01-29T06:04:44.753 回答
1

在 A 类中创建一个公共 getter 方法并通过 B 类访问相同的方法。

class A{
  private List<Employee> employees;

  public List getEmployees(){ return this.employees;}
}


class B{
  private List<Employee> employees;

  public B(){ 
  A a  = new A();
  employees = a.getEmployees();
 }

}
于 2013-01-29T06:13:35.550 回答
1

您试图实现的现象是“封装”。在 java 中,我们可以通过实现 Getter 和 setter 方法来实现这一点,即普通 java 对象。getter 和 setter 在 java 应用程序开发中扮演着非常重要的角色。

您可以实施这样的解决方案

import java.util.ArrayList;
import java.util.List;

class A {
    private List<String> empList = new ArrayList<String>();
    A(){
        empList.add("Employee 1");
        empList.add("Employee 2");
        empList.add("Employee 3");
        empList.add("Employee 4");
        empList.add("Employee 5");

    }
    public List<String> getEmpList() {
        return empList;
    }

    public void setEmpList(List<String> empList) {
        this.empList = empList;
    }
}

class B {
    void empList() {
        A empClass = new A();
        List<String> empListFromClassA = empClass.getEmpList();     
        System.out.println("This is what you need ? "+ empListFromClassA.toString());
    }
}

public class TEST{
    public static void main(String Args[]){
        B classb = new B();
        classb.empList();
    }
}
于 2013-01-29T06:17:56.597 回答
1

在 A 类上编写一个 getter 方法,该方法公开ArrayList并简单地从 B 类调用 getter

class A { 
    List myList...;

    public List<Employee> getEmployees() { 
    ...
    }
}

class B { 
    A handleToA;

    public void search() { 
        handleToA.getEmployees()...
    }

}
于 2013-01-29T06:00:54.510 回答
1

当然,您只需要允许Class AArrayList. Class B您可以通过创建一个public getter方法来做到这一点,该方法Class A将 .returnArrayList给任何其他请求它的类。

Class A
{
   private List<Employee> employees;

   public A() {
     this.employees = new ArrayList<>();
   }

   public List<Employee> getEmployees(){ return this.employees; } 
}

现在可以通过电话Class B询问员工ArrayListgetEmployees()

Class B
{
   private A a;

   public B() {
     this.a = new A();

     // fill the list ...

     List<Employee> fromA = a.getEmployees();  
   }
}
于 2013-01-29T06:01:56.727 回答
1

在 A 类中有一个吸气剂,不需要在 B 类中复制相同的吸气剂

class A {
    List<Employee> employeeList = new ArrayList<Employee>();

    public List<Employee> getEmployeeList() {
        return employeeList;
    }   
}
于 2013-01-29T06:02:51.490 回答