简体   繁体   English

Java中ArrayLists的getter和Setter

[英]Getters and Setters for ArrayLists in Java

How would I go about using getters and setters for an ArrayList in Java? 我如何在Java中使用getList和setter作为ArrayList? Here is what I have. 这就是我所拥有的。 I think I am using the getter correctly, but I don't know how to use a setter for an ArrayList. 我认为我正在使用getter,但我不知道如何使用setList作为ArrayList。

private ArrayList<String> stringlist = new ArrayList<String>();
public ArrayList<String> getStringList() {
    return stringlist;
}

public ArrayList<String> setStringList() {
    // I don't know what to put here
    // I also don't know if my return value is correct
}

ArrayList is a mutable container class, though, so you don't actually need a setter at all: simply have callers call getStringList() and then mutate the ArrayList themselves: 但是, ArrayList是一个可变容器类,所以你根本不需要一个setter:只需让调用者调用getStringList()然后自己改变ArrayList

public final class DataHolder {
  private final ArrayList<String> stringList = new ArrayList<>();

  public ArrayList<String> getStringList() {
    return stringList;
  }
}

public static void main(String[] args) {
  DataHolder data = new DataHolder();
  data.getStringList().add("foo");
}

On the other hand, the more common requirement is to prevent callers from being able to mutate internal data structures, so that your class can actually enforce its invariants on its data. 另一方面,更常见的要求是防止调用者能够改变内部数据结构,以便您的类实际上可以对其数据实施其不变量。 ArrayList is always mutable, so if you need to return an ArrayList but you don't want your private state to be modified, you'll need to copy: ArrayList总是可变的,所以如果你需要返回一个ArrayList但你不想修改你的私有状态,你需要复制:

public ArrayList<String> getStringList() {
  return new ArrayList<String>(stringList);
}

Alternatively, if possible, it's better to widen the return type and then return some other implementation that your class can use to enforce its invariants. 或者,如果可能的话,最好扩大返回类型,然后返回一些其他实现,您的类可以使用它来强制执行其不变量。 For example, if you don't want people to modify your ArrayList using a getter method, you could widen the type from ArrayList<String> to List<String> and use an unmodifiable implementation, for example: 例如,如果您不希望人们使用getter方法修改ArrayList ,则可以将类型从ArrayList<String>扩展为List<String>并使用不可修改的实现,例如:

public List<String> getStringList() {
  return Collections.unmodifiableList(stringList);
}

On the other hand, for the setter, it depends on how you want to handle the values. 另一方面,对于setter,它取决于您希望如何处理值。 Normally a setFoo method replaces the contents of foo, which you can of course do: 通常, setFoo方法替换 foo的内容,您当然可以这样做:

public void setStringList(ArrayList<String> stringList) {
  this.stringList = stringList;
}

But most likely you want to also widen the type you will accept. 但很可能你也希望扩大你会接受的类型。 For example, you could accept any Collection instead of just an ArrayList : 例如,您可以接受任何Collection而不仅仅是ArrayList

public void setStringList(Collection<String> strings) {
  this.stringList = new ArrayList<>(strings);
}

It may be more useful, however, to instead expose methods to mutate the underlying list in other ways. 但是,替代地公开方法以其他方式改变基础列表可能更有用。 For example, perhaps you simply want to support adding new items to the list: 例如,您可能只想支持向列表中添加新项:

public void addString(String string) {
  this.stringList.add(string);
}

public void addStrings(Collection<String> strings) {
  this.stringList.addAll(strings);
}

If you do decide to expose a setter that replaces the value, you probably want to first check it for correctness. 如果您决定公开替换该值的setter,您可能需要先检查它是否正确。 Since your class initializes the ArrayList in an instance initializer, most likely you don't expect it will ever be null. 由于您的类在实例初始化程序中初始化ArrayList ,因此很可能您不会期望它将为null。 So you should throw an exception if it is: 所以你应该抛出异常,如果它是:

public void setStringList(List<String> stringList) {
  if (stringList == null) {
    throw new NullPointerException("stringList must not be null");
  }
  this.stringList = stringList;
}

Java 7 added a new Objects.requireNonNull method for exactly this purpose. Java 7为此提供了一个新的Objects.requireNonNull方法。

A typical setter would look like this: 典型的setter看起来像这样:

public void setStringList(ArrayList<String> stringList) {
    this.stringList = stringList;
}

You can also consider returning this for purpose of fluent coding . 您也可以考虑回到this为目的流利的编码

public TypeOfYourClass setStringList(ArrayList<String> stringList) {
    this.stringList = stringList;
    return this;
}

To set the value, you would take an ArrayList<String> passed as a parameter, and you would simply set the ArrayList to the parameter. 要设置该值,您可以将ArrayList<String>作为参数传递,您只需将ArrayList设置为参数即可。

Also, note the use of void rather than the ArrayList<String> shown in your question. 另外,请注意使用void而不是问题中显示的ArrayList<String> Commonly, a setter method does not return anything. 通常,setter方法不返回任何内容。

public void setStringList(ArrayList<String> stringList)
{
    this.stringList = stringList;
}

Instead setter function, you can use a constructor to assign array. 而不是setter函数,您可以使用构造函数来分配数组。 like: 喜欢:

public class CLL extends ArrayAdapter<LBean> {
  ArrayList<LBean> lbean;

  public CLL(Context context, ArrayList<LBean> lList) {
      super(context, R.layout.custom_layoutT, lList );
      this.lbean=llist;
  }
  //getter functions
}

One of the simplest things we as programmers do is pass around data. 我们作为程序员所做的最简单的事情之一就是传递数据。 The traditional way to do this is to define a JavaBean: 传统的方法是定义JavaBean:

public class DataHolder {
private String data;

public DataHolder() {
}

public void setData(String data) {
    this.data = data;
}

public String getData() {
    return this.data;
}
}

Instead, I prefer the C struct style of writing classes that merely hold data: 相反,我更喜欢编写仅包含数据的类的C结构样式:

public class DataHolder {
public final String data;

public DataHolder(String data) {
    this.data = data;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM