java 如何在android中使用List<Data>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10503618/
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
How to use List<Data> in android?
提问by J1and1
How should I use List<Data> dat = new List<Data>();
to temporary store data in my software? "Data"
is a class in my code with variables(mainly Strings
). When I try that method it doesn't store data.
我应该如何使用List<Data> dat = new List<Data>();
在我的软件中临时存储数据?"Data"
是我的代码中带有变量的类(主要是Strings
)。当我尝试该方法时,它不存储数据。
回答by assylias
List
is an interface, so you can't instantiate it (call new List()
) as it does not have a concrete implementation. To keep it simple, use one of the existing implementations, for example:
List
是一个接口,因此您无法实例化它(调用new List()
),因为它没有具体的实现。为简单起见,请使用现有实现之一,例如:
List<Data> dat = new ArrayList<Data>();
You can then use it like this:
然后你可以像这样使用它:
Data data = new Data();
//initialise data here
dat.add(data);
You would probably benefit from reading the Java Tutorial on Collections.
您可能会从阅读Java 集合教程中受益。
回答by MAC
List<Data> lsData = new ArrayList<Data>();
for(int i=0;i<5;i++)
{
Data d = new Data();
d.fname="fname";
d.lname="lname";
lsData.add(d);
}
Your Data class (Always make a Bean class to manage data)
您的 Data 类(始终创建一个 Bean 类来管理数据)
public class Data
{
public Data()
{
}
public String fname,lname;
}
you can also get your data of particular position
您还可以获取特定位置的数据
String fname = lsData.get(2).fname;
String lname = lsData.get(2).lname;