0
0

[Java]String型の配列をint型の配列に変換する

Posted at

はじめに

ネット上のスキルチェック問題などを解いていてよく出くわすのでメモがわりに。

問題

"123456"

という文字列型の数値の入力を

[1, 2, 3, 4, 5, 6]

と、数値型の配列に変換する。

解決方法

// 問題で与えられる入力
String input = "123456";

// Stringの文字列で入力を受け取る
String[] strArr = input.split("");

// int型の配列に変換
int[] numArr = Stream.of(strArr).mapToInt(Integer::parseInt).toArray();

System.out.println(Arrays.toString(numArr));  // [1,2,3,4,5,6]

おわりに

streamは中間処理をよく忘れるのでメモメモ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0