[英]Convert List<String[]> to int[]
如果可以将字符串(ArrayList<String[]>)
转换为整数数组(int[])
,是否可以将字符串数组数组列表(ArrayList<String[]>)
转换为整数数组(int[])
?
In Java 8:在 Java 8 中:
list.stream().mapToInt(Integer::parseInt).toArray();
Since the question was edited with List<String[]>
instead of List<String>
, a solution would be:由于问题是用List<String[]>
而不是List<String>
,因此解决方案是:
list.stream().flatMap(Arrays::stream).mapToInt(Integer::parseInt).toArray();
i have modified the code according to your edited answer,do reply if this is what you want.我已根据您编辑的答案修改了代码,如果这是您想要的,请回复。
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
ArrayList<String[]> strArrayList= new ArrayList<String[]>();
String[] str1={"16","1421","1577"};
String[] str2={"15","1451","1461","6556"};
String[] str3={"157","8751","1136","556","879"};
strArrayList.add(str1);
strArrayList.add(str2);
strArrayList.add(str3);
int sizeOfArr=0;
int i=0;
for (String[] s : strArrayList)
{
sizeOfArr+=s.length;
}
int[] ArrayRes = new int[sizeOfArr];
for (String[] s : strArrayList)
{
if(s.length>0){
for (String s1 : s){
ArrayRes[i] = Integer.parseInt(s1);
System.out.println(ArrayRes[i]);
i++;
}
}
}
}
}
run the below code,i hope it meets you requirement.运行下面的代码,我希望它满足你的要求。
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
ArrayList<String> strArrayList= new ArrayList<String>();
strArrayList.add("1");
strArrayList.add("11");
strArrayList.add("111");
strArrayList.add("12343");
strArrayList.add("18475");
int[] ArrayRes = new int[strArrayList.size()];
int i = 0;
int x = 0;
for (String s : strArrayList)
{
ArrayRes[i] = Integer.parseInt(s);
System.out.println(ArrayRes[i]);
i++;
}
}
}
Output:输出:
1
11
111
12343
18475
Further to my comment, you'll need to work out how to map from 2D to 1D array.除了我的评论之外,您还需要弄清楚如何将二维数组映射到一维数组。
Here's a suggestion which results in Integer[]
rather than int[]
but you might not worry about that:这是一个导致Integer[]
而不是int[]
的建议,但您可能不会担心:
public static void main(String[] args) {
ArrayList<String[]> arrList = new ArrayList<String[]>();
//fill it
arrList.add(new String[] {"11","12","13","14","15"});
arrList.add(new String[] {"21","22","23","24","25"});
arrList.add(new String[] {"31","32","33","34","35"});
arrList.add(new String[] {"41","42","43","44","45"});
arrList.add(new String[] {"41","52","53","54","55"});
//convert it
ArrayList<Integer> ints = new ArrayList<Integer>();
for (String[] array : arrList) {
for (String str : array) {
ints.add(Integer.parseInt(str));
}
}
Integer[] result = ints.toArray(new Integer[] {});
for (int i : result)
System.out.println(i);
}
It is possible if all the Strings can be parsed to integers correctly.如果所有字符串都可以正确解析为整数,则是可能的。
Iterate over the list and in the list over the array.遍历列表并在列表中遍历数组。 Parse the Strings to Integers (eg with Integer.parseInt(someString);
) and store the integervalues in the integer array.将字符串解析为整数(例如使用Integer.parseInt(someString);
)并将整数值存储在整数数组中。
Of course you'll have to find the total number of values in the list to initialize the array.当然,您必须找到列表中值的总数来初始化数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.