Java 中 ArrayList 的 Getter 和 Setter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33060592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getters and Setters for ArrayLists in Java
提问by
How would I go about using getters and setters for an ArrayList in Java? 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.
我将如何在 Java 中为 ArrayList 使用 getter 和 setter?这是我所拥有的。我想我正确地使用了 getter,但我不知道如何为 ArrayList 使用 setter。
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
}
采纳答案by Marcin Król
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. Commonly, a setter method does not return anything.
另外,请注意您的问题中使用的void
而不是ArrayList<String>
显示的。通常,setter 方法不返回任何内容。
public void setStringList(ArrayList<String> stringList)
{
this.stringList = stringList;
}
回答by Marcin Król
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;
}
回答by Alvaro Joao
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;
}
}
回答by Daniel Pryden
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
但是,它是一个可变容器类,因此您实际上根本不需要设置器:只需让调用者调用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 preventcallers 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:
或者,如果可能,最好扩大返回类型,然后返回您的类可以用来强制执行其不变量的其他一些实现。例如,如果您不希望人们ArrayList
使用 getter 方法修改您的方法,则可以将类型从ArrayList<String>
to扩大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. Normally a setFoo
method replacesthe contents of foo, which you can of course do:
另一方面,对于 setter,这取决于您希望如何处理这些值。通常一个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
:
但很可能您还想扩大您将接受的类型。例如,您可以接受 anyCollection
而不仅仅是 an 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. Since your class initializes the ArrayList
in an instance initializer, most likely you don't expect it will ever be null. So you should throw an exception if it is:
如果您决定公开替换该值的 setter,您可能希望首先检查它的正确性。由于您的类ArrayList
在实例初始值设定项中初始化 ,因此您很可能不希望它永远为空。所以你应该抛出一个异常,如果它是:
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 7Objects.requireNonNull
正是为此目的添加了一个新方法。
回答by Gv Ravi
Instead setter function, you can use a constructor to assign array. like:
代替 setter 函数,您可以使用构造函数来分配数组。喜欢:
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
}