Table of Contents
ToggleJava Convert String to Integer Array
字串是 Java 中 char 陣列內部支援的物件,由於陣列是不可變的,而字串也是一種保存字元的特殊陣列,因此字串也是不可變的,整數陣列是相同類型變數的組合, Convert String to Array of Integers in Java 任務是將該字串轉換為整數陣列,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- conversion
| +- string
| +- StringToIntegerArrayTest.java
單元測試
Convert String to int Array in Java 提供字串轉換成整數陣列等操作。
replaceAll
取代第一個 “[” 及最後一個 “]” 字串,然後使用 split 切割成字串陣列,再透過 Integer.valueOf 轉換成整數陣列。
@Test
public void replaceAll() {
int expectedSize = 5;
String value = "[147,168,151,460,484]";
String[] stringArray = value.replaceAll("\\[", "").replaceAll("]", "").split(",");
System.out.println(Arrays.toString(stringArray));
int[] array = new int[stringArray.length];
for (int i = 0; i < array.length; i++) {
array[i] = Integer.valueOf(stringArray[i]);
}
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
[147, 168, 151, 460, 484]
[147, 168, 151, 460, 484]
split
Convert String to int Array in Java 使用 split 切割成字串陣列,再透過 Integer.parseInt 轉換成整數陣列。
@Test
public void split() {
int expectedSize = 5;
String value = "147,168,151,460,484";
String[] stringArray = value.split(",");
System.out.println(Arrays.toString(stringArray));
int[] array = new int[stringArray.length];
for (int i = 0; i < array.length; i++) {
array[i] = Integer.parseInt(stringArray[i]);
}
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
[147, 168, 151, 460, 484]
[147, 168, 151, 460, 484]
stream
Convert String to int Array in Java 使用 split 切割成字串陣列,再透過 stream 轉換成整數陣列。
@Test
public void stream() {
int expectedSize = 5;
String value = "147,168,151,460,484";
String[] stringArray = value.split(",");
System.out.println(Arrays.toString(stringArray));
int[] array = Arrays.stream(stringArray).mapToInt(Integer::parseInt).toArray();
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
[147, 168, 151, 460, 484]
[147, 168, 151, 460, 484]
streamWithException
Convert String to int Array in Java 使用 split 切割成字串陣列,再透過 stream 轉換成整數陣列,當有無法轉換數字的字串時,例外處理傳回 0 值。
@Test
public void streamWithException() {
int expectedSize = 5;
String value = "147,168,151,460,java";
String[] stringArray = value.split(",");
System.out.println(Arrays.toString(stringArray));
int[] array = Arrays.stream(stringArray).mapToInt(e -> {
int result = 0;
try {
result = Integer.parseInt(e);
} catch (NumberFormatException ex) {
}
return result;
}).toArray();
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
[147, 168, 151, 460, java]
[147, 168, 151, 460, 0]
StringToIntegerArrayTest.java
Converting a String array into an int Array in Java 新增單元測試,驗證是否符合預期。
package org.ruoxue.java_147.conversion.string;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
public class StringToIntegerArrayTest {
@Test
public void replaceAll() {
int expectedSize = 5;
String value = "[147,168,151,460,484]";
String[] stringArray = value.replaceAll("\\[", "").replaceAll("]", "").split(",");
System.out.println(Arrays.toString(stringArray));
int[] array = new int[stringArray.length];
for (int i = 0; i < array.length; i++) {
array[i] = Integer.valueOf(stringArray[i]);
}
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
@Test
public void split() {
int expectedSize = 5;
String value = "147,168,151,460,484";
String[] stringArray = value.split(",");
System.out.println(Arrays.toString(stringArray));
int[] array = new int[stringArray.length];
for (int i = 0; i < array.length; i++) {
array[i] = Integer.parseInt(stringArray[i]);
}
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
@Test
public void stream() {
int expectedSize = 5;
String value = "147,168,151,460,484";
String[] stringArray = value.split(",");
System.out.println(Arrays.toString(stringArray));
int[] array = Arrays.stream(stringArray).mapToInt(Integer::parseInt).toArray();
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
@Test
public void streamWithException() {
int expectedSize = 5;
String value = "147,168,151,460,java";
String[] stringArray = value.split(",");
System.out.println(Arrays.toString(stringArray));
int[] array = Arrays.stream(stringArray).mapToInt(e -> {
int result = 0;
try {
result = Integer.parseInt(e);
} catch (NumberFormatException ex) {
}
return result;
}).toArray();
System.out.println(Arrays.toString(array));
assertEquals(expectedSize, array.length);
}
}
心得分享
Converting a String into an int Array with Java 需求非常簡單,然而,在現實世界中,字串陣列可能來自不同的來源,例如:使用者輸入或由另一個系統匯入,因此,輸入字串可能包含一些不是有效數字格式的值,所以當不是有效數字時,就需要例外處理傳回特定值或是中斷處理, Converting a String array into an int Array in Java 提供了幾種常見方法的操作範例。