Объект создан из списка значений
Я получаю список значений в 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));
}
Это извлекло бы мои элементы, но я не уверен, как сделать объект из него. Я знаю, что моя попытка слишком элементарна. Как я могу сформировать и вернуть такой объект?
Спасибо,
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;
}
}
}