Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中如何将列表字符串列表转换为列表整数列表_Java_List - Fatal编程技术网

java中如何将列表字符串列表转换为列表整数列表

java中如何将列表字符串列表转换为列表整数列表,java,list,Java,List,我有一个列表字符串,现在我想把它转换成列表整数。建议一些方法,如何进行呢 这是我的密码: public class convert { public static void main(String[] args) { try { List<List<String>> outerList = new ArrayList<List<String>>(); outerList.a

我有一个列表字符串,现在我想把它转换成列表整数。建议一些方法,如何进行呢

这是我的密码:

public class convert {

    public static void main(String[] args) {
        try {

            List<List<String>> outerList = new ArrayList<List<String>>();
            outerList.add(new ArrayList<String>(asList("11","2")));
            outerList.add(new ArrayList<String>(asList("2","1")));
            outerList.add(new ArrayList<String>(asList("11","3")));

            System.out.println(outerList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 
公共类转换{
公共静态void main(字符串[]args){
试一试{
List outerList=new ArrayList();
添加(新的ArrayList(asList(“11”、“2”));
添加(新的ArrayList(asList(“2”、“1”));
添加(新的ArrayList(asList(“11”、“3”));
System.out.println(外部列表);
}捕获(例外e){
e、 printStackTrace();
}
}
} 

您只需这样做:

for(String s : yourStringList) 
{
  intList.add(Integer.valueOf(s));
}
编辑

for(列表s:yourStringList){
列表x=新的ArrayList();
用于(字符串str:s){
x、 add(Integer.parseInt(str));
}
添加(x);
}

res是新的数组列表,它包含整数列表

       List<List<Integer>> res = new ArrayList<List<Integer>>();

        for(List<String> l : outerList){
            ArrayList<Integer> al = new ArrayList<Integer>();
            for(String s: l){
                al.add(Integer.valueOf(s));
            }
            res.add(al);
        }
List res=new ArrayList();
对于(列表l:外部列表){
ArrayList al=新的ArrayList();
for(字符串s:l){
加上(整数值);
}
第add(al)号决议;
}

我建议使用Streams API进行以下操作:

import static java.util.stream.Collectors.toList;

...

integerList = outerList.stream()
   .map(innerList->innerList.stream().map(Integer::valueOf).collect(toList()))
   .collect(toList());

您必须迭代每个
项的每个
子项

List<List<String>> stringList = new ArrayList<List<String>>(); // Input
List<List<Integer>> intList = new ArrayList<List<Integer>>(); // Output
for (List<String> item : stringList) {
    List<Integer> temp = new ArrayList<Integer>();
    for (String subItem : item) {
        temp.add(Integer.parseInt(subItem));
    }
    intList.add(temp);
}
List-stringList=new-ArrayList();//输入
List intList=new ArrayList();//输出
用于(列表项:stringList){
List temp=new ArrayList();
for(字符串子项:项){
临时添加(Integer.parseInt(子项));
}
intList.add(临时);
}

+1。也许在
{}
中加上一点验证?这同样有效,你能告诉我你的方法和我收到的其他回复有什么不同吗。我的意思是,map的使用可以在性能方面增加一些差异。主要的区别是,这是Java 8,而其他答案也适用于以前版本的Java。这种方法与其他方法不同,它更简洁,更可读,因为它陈述了意图,而不是通过实现它的机制。就性能而言,您可以预期非常小的开销,这很可能与在
Integer::valueOf
中进行的计算相形见绌。
List<List<String>> stringList = new ArrayList<List<String>>(); // Input
List<List<Integer>> intList = new ArrayList<List<Integer>>(); // Output
for (List<String> item : stringList) {
    List<Integer> temp = new ArrayList<Integer>();
    for (String subItem : item) {
        temp.add(Integer.parseInt(subItem));
    }
    intList.add(temp);
}