Splitting String and put it on int array

后端 未结 8 1411
眼角桃花
眼角桃花 2020-11-28 14:03

I have to input a string with numbers ex: 1,2,3,4,5. That\'s a sample of the input, then I have to put that in an array of INT so I can sort it but is not working the way it

8条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 14:52

    String input = "2,1,3,4,5,10,100";
    String[] strings = input.split(",");
    int[] numbers = new int[strings.length];
    for (int i = 0; i < numbers.length; i++)
    {
      numbers[i] = Integer.parseInt(strings[i]);
    }
    Arrays.sort(numbers);
    
    System.out.println(Arrays.toString(numbers));
    

提交回复
热议问题