Mr.*_*mes 6 java type-conversion
我要让那些需要转换的应用程序ArrayList<String>[]来ArrayList<Integer>[],我也用这样的:
ArrayList<String>[] strArrayList;
int ArrayRes = (int) strArrayList[];
Run Code Online (Sandbox Code Playgroud)
但这段代码给我一个错误,任何人都可以帮助我?
任何建议都会受到赞赏
Pan*_*mar 15
定义一个方法,将arraylist的所有String值转换为整数.
private ArrayList<Integer> getIntegerArray(ArrayList<String> stringArray) {
ArrayList<Integer> result = new ArrayList<Integer>();
for(String stringValue : stringArray) {
try {
//Convert String to Integer, and store it into integer array list.
result.add(Integer.parseInt(stringValue));
} catch(NumberFormatException nfe) {
//System.out.println("Could not parse " + nfe);
Log.w("NumberFormat", "Parsing failed! " + stringValue + " can not be an integer");
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
简单地称之为该方法
ArrayList<Integer> resultList = getIntegerArray(strArrayList); //strArrayList is a collection of Strings as you defined.
Run Code Online (Sandbox Code Playgroud)
快乐编码:)
这个怎么样
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class sample7
{
public static void main(String[] args)
{
ArrayList<String> strArrayList = new ArrayList<String>();
strArrayList.add("1");
strArrayList.add("11");
strArrayList.add("111");
strArrayList.add("12343");
strArrayList.add("18475");
List<Integer> newList = new ArrayList<Integer>(strArrayList.size()) ;
for (String myInt : strArrayList)
{
newList.add(Integer.valueOf(myInt));
}
System.out.println(newList);
}
}
Run Code Online (Sandbox Code Playgroud)
Sak*_*ket -8
列表上的迭代器并将它们解析为整数:
(警告:未经测试)
ArrayList<String> strArrayList;
int[] ArrayRes = new int[strArrayList.size()];
int i = 0;
for (String s : strArrayList)
{
ArrayRes[i++] = Integer.parseInt(s);
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以根据您希望如何连接它们将它们转换为单个 int 值。