How to convert string array to int array in java

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

I have an string array in java program like this

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 int elements of that array.

回答1:

If you are using java 8 Try this :

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


回答2:

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.



回答3:

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); } 

}



回答4:

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.

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!