Java 为 list<String[]> 赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9758838/
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
assign values to list<String[]>
提问by qwerty_gr
i need your help because I may not have understood it well.
我需要你的帮助,因为我可能没有很好地理解它。
This List<String[]> dataList
defines an list with string arrays, right?
So lets say that I have this values:
这List<String[]> dataList
定义了一个带有字符串数组的列表,对吗?所以可以说我有这个价值观:
info[0], info[1], name[0],name[1]
How can I assign them to the above list, grouped by index? I mean the info[0] be with name[0], and so on. I want this is because later in my code I will use this:
如何将它们分配到上述列表中,按索引分组?我的意思是 info[0] 与 name[0],依此类推。我想要这是因为稍后在我的代码中我将使用它:
public FacilitiesAdapter (List<String[]> dataList) {
this.dataList = dataList;
}
and I need my datalist to have in each row, the data I want. Have I understood something wrong?
我需要我的数据列表在每一行中都有我想要的数据。我理解错了吗?
采纳答案by Shashank Kadne
Something like this??
这种东西??
List<String[]> dataList = new ArrayList<String[]>();
String name[] = new String[]{"n1","n2"};
String info[] = new String[]{"i1","i2"};
for(int i=0;i<name.length;i++)
{
dataList.add(new String[]{name[i],info[i]});
}
回答by Calvin
Define it as a class:
将其定义为一个类:
public class MyData {
public String info;
public String name;
}
Then you have:
然后你有:
List<MyData> mDatalist;
public FacilitiesAdapter (List<MyData> dataList) {
this.mDataList = dataList;
}
回答by Mike Hogan
Does this help?
这有帮助吗?
public void arrayLearning(){
List<String[]> dataList = new ArrayList<String[]>();
String[] info = {"first info", "second info"};
String[] name = {"first name", "second name"};
dataList.add(new String[]{info[0], name[0]});
dataList.add(new String[]{info[1], name[1]});
}
回答by Dalmas
You can do it this way :
你可以这样做:
List<String[]> dataList = new List<String[]>();
int len = info.length;
for (int i = 0; i < len; i++)
dataList.add(new String[] {name[i], info[i]});
However, Calvin's answer is better for flexibility. If you want to add an additional field later, it will be way easier if you already created your own class instead of using a String array.
然而,卡尔文的答案是更好的灵活性。如果您想稍后添加其他字段,如果您已经创建了自己的类而不是使用 String 数组,则会更容易。
回答by zapl
Create the list
创建列表
String[] name = new String[] { "name1", "name2" };
String[] info = new String[] { "info1", "info2" };
List<String[]> dataList = new ArrayList<String[]>();
for (int i = 0; i < name.length; i++) {
String[] item = new String[2];
item[0] = name[i];
// info and name must have the same size or you need some checks here
item[1] = info[i];
dataList.add(item);
}
print it
打印出来
for (int i = 0; i < dataList.size(); i++) {
Log.d("TAG", "item " + i +
" name:" + dataList.get(i)[0] +
" info:" + dataList.get(i)[1]);
}
use in your adapter
在您的适配器中使用
public View getView(int position, View convertView, ViewGroup parent) {
String[] item = datalist.get(position);
String name = item[0];
String info = item[1];
}
回答by Noroi
Always use data encapsulation if multiple data types is bound to one unique data. Calvin's method is the best suited and highly flexible.
如果多个数据类型绑定到一个唯一数据,则始终使用数据封装。Calvin 的方法是最合适的,而且非常灵活。