【发布时间】:2019-11-24 03:28:33
【问题描述】:
我想将以下列表转换为 int 数组。我曾尝试使用valueof,但我不知道如何使用它。我不想解析它。
我这里有一个字符串数组:
String[] arr1 = new String[]{"5","5","15","5","10","10", "5", "10", "20", "15"};
这是我试过的代码:
int num = Integer.valueOf(arr1);
我收到一条错误消息:
error: no suitable method found for valueOf(String[])
int num = Integer.valueOf(arr1);
^
method Integer.valueOf(String) is not applicable
(argument mismatch; String[] cannot be converted to String)
method Integer.valueOf(int) is not applicable
(argument mismatch; String[] cannot be converted to int)
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
【问题讨论】:
-
我将如何使用 valueOf?
-
如果您想将这些数字的字符串表示形式转换为整数,那么您确实想要解析它们。
-
感谢您的回答。我知道解析可能更容易,但我被告知不要使用它。
-
感谢您的澄清,我是编程新手,所以我很困惑。