java中string数组转long数组几个例子
方法 一,把数组for 下一个转
String[] yq1012= {"123", "234", "345"};
Long[] num = new Long[yq1012.length];
for (int idx = 0; idx < yq1012.length; idx ) {
num[idx] = Long.parseLong(yq1012[idx]);
}
方法 二,
public static void main(String[] args) {
String stringArray[] = { "1", "2", "3", "4", "5", "6", "7", "a" };
long time1 = System.currentTimeMillis();
stringToLong(stringArray);
long time2 = System.currentTimeMillis();
System.out.println("stringToLong:" (time2 - time1));
long time3 = System.currentTimeMillis();
stringToLong_2(stringArray);
long time4 = System.currentTimeMillis();
System.out.println("stringToLong_2:" (time4 - time3));
}
public static long[] stringToLong(String stringArray[]) {
if (stringArray == null || stringArray.length < 1) {
return null;
}
long longArray[] = new long[stringArray.length];
for (int i = 0; i < longArray.length; i ) {
try {
longArray[i] = Long.valueOf(stringArray[i]);
} catch (NumberFormatException e) {
longArray[i] = 0;
continue;
}
}
return longArray;
}
public static long[] stringToLong_2(String stringArray[]) {
if (stringArray == null)
return null;
return (long[]) ConvertUtils.convert(stringArray, long.class);
}
方法 三
Set<Long> idsSet = new HashSet<Long>();
//过滤没有详情的id
for(Entry<Long, UserProfile> entry : userProfileMap.entrySet()){
UserProfile userProfile = entry.getValue();
if (userProfile == null) {
continue;
}
idsSet.add(userProfile.getUserId());
}
Long[] idsLong = idsSet.toArray(new Long[0]);
int idsLen = idsLong.length;
long[] idslong = new long[idsLen];
for(int i = 0; i < idsLen; i ){
idslong[i] = idsLong[i];
}
您可能感兴趣的文章:
java中string数组转long数组几个例子
java中将字符串(String)转换为整型
java数组降序和升序排序例子
Java 中 I/O 进制详解及I/O流小结
java入门篇-数据类型
jQuery数组处理方法汇总
JAVA几个常用的数字格式化例子
JAVA数组详解
PHP 数组字符集编码转换的函数
java中将字符(Char)转换为字符串的四种方式