java java中如何将字符串数组转换为int数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35358255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:03:20  来源:igfitidea点击:

How to convert string array to int array in java

javaarraysstringint

提问by Ruhul Amin

I have an string array in java program like this

我在这样的 java 程序中有一个字符串数组

String results[] = { "2", "1", "5", "1" };

I want to convert this to integer array as like this:

我想将其转换为整数数组,如下所示:

int results[] = { 2, 1, 5, 1 };

And finally I want to find the summation of all the intelements of that array.

最后我想找到该int数组所有元素的总和。

回答by soorapadman

If you are using java 8 Try this :

如果您使用的是 java 8 试试这个:

 int[] array = Arrays.stream(resultsStr).mapToInt(Integer::parseInt).toArray();

回答by Shark

String resultsStr[] = {"2", "1", "5", "1"};
ArrayList intermediate = new ArrayList();
for(String str : resultsStr)
{
    intermediate.add(Integer.parseInt(str, 10)); //the 10 could be ommitted
}
int resultsInt[] = intermediate.toArray();

and your resultsInt[]will contain the array of ints. While I agree that it doesn't have to go thru the arraylist (it can be accomplished without it) i used it merely because it was easier to type out.

并且您resultsInt[]将包含整数数组。虽然我同意它不必通过数组列表(它可以在没有它的情况下完成),但我使用它只是因为它更容易输入。

回答by ali Boumedyen

This is my simple solution :

这是我的简单解决方案:

public class Tester {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String results[]={"2","1","5","1"};

    //Creating a new array of Type Int
    int result [] = new int[4];

    int sum = 0;

    //Going trough every element in results And Converting it to Int By using : Integer.valueOf(String str)

    for (int i = 0; i < results.length; i++) {
        result[i] = Integer.valueOf(results[i]);

    }


    //Displaying the result
    for (int i = 0; i < result.length; i++) {
        System.out.println(result[i]);
        sum += result[i];
    }

    System.out.println("The sum of all elements is : " + sum);
}

}

}

回答by Ron

I am probably a little late on this, but here is a simple solution that basically uses a for loop to iterate through the array of strings, adds the parsed integer value to a new integer array, and adds the integers up as it goes. Also note that I moved the square brackets for the String array type, String[] results, because not only is it less confusing but the array is part of the type and not a part of the arrays name.

我可能有点晚了,但这里有一个简单的解决方案,它基本上使用 for 循环遍历字符串数组,将解析的整数值添加到新的整数数组中,并在进行时将整数相加。另请注意,我移动了 String 数组类型的方括号, String[] results,因为它不仅不那么容易混淆,而且数组是类型的一部分,而不是数组名称的一部分。

public class test {
    public static void main(String[] args) {
        String[] results = { "2", "1", "5", "1" };

        // Create int array with a pre-defined length based upon your example
        int[] integerArray = new int[results.length];

        // Variable that holds the sum of your integers
        int sumOfIntegers = 0;

        // Iterate through results array and convert to integer
        for (int i = 0; i < results.length; i++) {
            integerArray[i] = Integer.parseInt(results[i]);

            // Add to the final sum
            sumOfIntegers += integerArray[i];
        }

        System.out.println("The sum of the integers is: " + sumOfIntegers);
    }
}

You could also create the integer array first, and then after the loop add the numbers together, but in this instance we are assuming that the array of Strings are all integers so I didn't see a reason to make this too complicated.

您也可以先创建整数数组,然后在循环之后将数字相加,但在这种情况下,我们假设字符串数组都是整数,所以我没有看到让这太复杂的理由。

回答by sudesh regmi

A simple solution will be:

一个简单的解决方案是:

 private void toIntArray(String[] strings) {
    Integer[] intArray=new Integer[strings.length];
    int i=0;
    for(String str:strings){
        intarray[i]=Integer.parseInt(str.trim());
        i++;
    }
 }