Java 从值列表创建的对象

Java 从值列表创建的对象,java,list,object,methods,Java,List,Object,Methods,我正在检索3个不同列表中的值列表,如下所示: 列表1打印出a、d、g 列表2打印出b、e、h 清单3打印出c、f、i 我希望将上述数据作为如下所示的对象返回: [{ 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 ArrayLis

我正在检索3个不同列表中的值列表,如下所示:

列表1打印出a、d、g 列表2打印出b、e、h 清单3打印出c、f、i

我希望将上述数据作为如下所示的对象返回:

[{
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));

    }
这将检索我的元素,但我不确定如何从中生成对象。我知道我的尝试太初级了。 如何形成并返回这样的对象


谢谢,

我想这就是您需要的。我已经对代码进行了注释,以供您理解

 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;
     }
   }
 }

每个列表的大小是否相同?您需要显示您的尝试。是。尺寸是一样的。这很不清楚,输入是什么?列表,比如列表,或者从文件/stdin中顺序读取的内容,或者其他内容?Java是强类型的,但也许您正在寻找的只是一个映射?好的。我正在编辑。我在一个方法中使用它,所以如果我的方法返回finallist,我的方法返回类型是List吗?@AYa是的。你的方法返回类型应该是List.small o。如果这个解决方案对你有效,别忘了接受它。哥们,你能帮我做一个这样一个方法的mockito测试吗?@AYaSample mockito test我不明白,我的意思是,如果我们在这里返回一个对象,我们该如何为这个编写单元测试呢。Mockito用于模仿。然而,我认为junit在这里已经足够了。