package com.kpzc.test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.lang3.StringUtils; /** * 用逗号隔开的String类型字符串转为int类型的数组 两种方法的比较 方法二的效率更高 * @author kpzc * */ public class StringToArry { /** * 生成用逗号隔开的String类型字符串 * @param num * @return */ public static String getStringArry(int num) { String s = ""; for (int i = 0; i < num; i++) { s += i + ","; if(i==num-1) s += i; } //System.out.println(s); return s; } /**方法一 * @param ids */ public static void StringToArry1(String ids) { Long start = System.currentTimeMillis(); List<String> sarry = Arrays.asList(StringUtils.split(ids, ",")); List<Integer> arry = new ArrayList<Integer>(); for (String term : sarry) { arry.add(Integer.parseInt(term)); } Long end = System.currentTimeMillis(); System.out.println(end - start); } /** * 方法二 * @param ids */ public static void StringToArry2(String ids) { Long start = System.currentTimeMillis(); String[] sarry = ids.split(","); int[] arry = new int[sarry.length]; for (int i = 0; i < sarry.length; i++) { arry[i] = Integer.parseInt(sarry[i]); } Long end = System.currentTimeMillis(); System.out.println(end - start); } public static void main(String[] args) { getStringArry(1000); StringToArry1(getStringArry(1000)); StringToArry2(getStringArry(1000)); } }