我正在检索3个不同列表中的值列表,如下所示:
列表1打印出a,d,g
列表2打印出b,e,h
列表3打印出c,f,i
我希望将上述数据作为object
返回,看起来像这样:
[{
A: a,
B: b,
C:c
},
A: d,
B: e,
C:f
},
A: g,
B: h,
C:i
}]
ArrayList<String> destList = new ArrayList<>();
ArrayList<String> resultList = new ArrayList<>();
ArrayList<String> statusList = new ArrayList<>();
//adding elements to all the three strings.
for (int y = 0; y < foo.length() - 1; y++) {
System.out.println("A"+":"destList.get(y)+",");
System.out.println("B"+":"+resultList.get(y)+",");
System.out.println("C"+":"+statusList.get(y));
}
这会检索我的元素,但我不确定如何制作一个对象。我知道我的尝试太简陋了。 我如何形成并返回这样的对象?
谢谢,
答案 0 :(得分:1)
我认为这就是您所需要的。我已经为您的理解评论了代码
import java.util.ArrayList;
import java.util.List;
public class demo {
public static void main(String[]args){
List<Integer>list1=new ArrayList<Integer>();
List<Integer>list2=new ArrayList<Integer>();
List<Integer>list3=new ArrayList<Integer>();
////here add your values to the list,i have not added them.You first need to add the values to the list
//then iterate through your list
//create a list of your own custom class
List<object>finallist=new ArrayList<object>();
for (int i=0;i<list1.size();i++){
finallist.add(newobject(list1.get(i),list2.get(i),list3.get(i)));//use new keyword to instantiate or create a reference of a new object
}
//now your finallist contains your required data
}
static class object{//create your custom class
int A,B,C;
public object(int a, int b, int c) {define a constructor
A = a;
B = b;
C = c;
}
}
}