0

I have a Java string like "10 20 3 0", now I would like to convert it into an array list. I know that in C++, the code should be like this:

string s = "10 20 3 0";
stringstream ss(s);
vector<int> arr;
string tmp;
while (ss >> tmp)
{
    arr.push_back(stoi(tmp));
}

How can I do this in Java, since I am quite new to java.

C. Wang
  • 2,516
  • 5
  • 29
  • 46

1 Answers1

0

If you are on Java 8 , you can use streams. The following works

Stream<String> streamString = Stream.of(string.split(" "));
List<String> stringList = streamString.collect(Collectors.toList());
stringList.forEach(System.out::println);
Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24