用字符串列表替换int列表

时间:2015-12-31 11:48:39

标签: java android

替换

的最简单方法是什么?
int[] engNumber = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

int[] bangNumber = new int[]{'১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯', '০'}; 
Android代码中的

3 个答案:

答案 0 :(得分:1)

你可以做以下两件事之一:

  1. Map号码到eng号码创建bang

    public void test() {
        // Make a Map.
        int[] engNumber = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
        int[] bangNumber = new int[]{'১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯', '০'};
        Map<Integer,Integer> toBang = new HashMap<>();
        for ( int i = 0; i < engNumber.length; i++) {
            toBang.put(engNumber[i], bangNumber[i]);
        }
        int bang3 = toBang.get(3);
    }
    
  2. 重新排列bang数组,以便0字符代表0等。

    public void test() {
        // Rearrange to make 0 at [0].
        int[] bangN = new int[]{'০', '১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯'};
        int bang3 = bangN[3];
    }
    

答案 1 :(得分:0)

this solution可能有所帮助

//for lists
public static <T, U> List<U> convertList(List<T> from, Function<T, U> func){
    return from.stream().map(func).collect(Collectors.toList());
}

//for arrays
public static <T, U> U[] convertArray(T[] from, Function<T, U> func, 
                                       IntFunction<U[]> generator){
    return Arrays.stream(from).map(func).toArray(generator);
}

使用方法

//for lists
List<String> stringList = Arrays.asList("1","2","3");
List<Integer> integerList = convertList(stringList, s -> Integer.parseInt(s));

//for arrays
String[] stringArr = {"1","2","3"};
Double[] doubleArr = convertArray(sArr, Double::parseDouble, Double[]::new);

答案 2 :(得分:0)

String input; // the string with english numbers
String output; // the string with 'bang' numbers
StringBuilder buffer = new StringBuilder(); // we use this to build the output
int[] engNumber = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
char[] bangNumber = new char[]{'১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯', '০'};

for(int i = 0, j = input.length(); i < j; i++)
    buffer.append((char)bangNumber[input.charAt(i) - 48)]);

output = buffer.toString();