方法1
- 先把Object數組轉成固定長度的list
- 遍歷該list,把每個Object對象轉成String對象,再轉成Long對象
public void castDemo(Object[] ids) {
List<Object> dels = Arrays.asList(ids);
for (Object id : dels) {
Long lid = Long.valueOf((String) id);
}
}
方法2
- 把對象數組轉成順序流,再用toArray傳到新的String數組
- 用ConvertUtils工具類進行轉換
- 把Long數組轉成定長的list
public void castDemo2(Object[] ids) {
String[] strings = Arrays.stream(ids).toArray(String[]::new);
Long[] convert = (Long[]) ConvertUtils.convert(strings, Long.class); // 這個工具類的職能是在字符串和指定類型的實例之間進行轉換
List<Long> dels = Arrays.asList(convert);
for (Long id : dels) {
}
}