假设我有几个系列的0和1。我需要在不同字符串中的所有相同索引处添加值
110111111111000101111111110000000001111011000111110
000000000000011001001000011111000110111100000000011
111111000100011001001000011111000110111100000000001
000110000000011001001000011111000110000000000000010
111111000100011001001000011111000110111100001111111
结果就像
3324............................................243
最好的方法是什么?
位串的长度约为。 200和所有都有相同的长度。
数据位于String[] arr
,数组的每个元素都有一组0和1,即arr[0] = 110111111111000101111111110000000001111011000111110
等......
答案 0 :(得分:0)
如果输入字符串的数量少于10,你就可以这样做。
String[] input = {"110111111111000101111111110000000001111011000111110",
"000000000000011001001000011111000110111100000000011",
"111111000100011001001000011111000110111100000000001",
"000110000000011001001000011111000110000000000000010",
"111111000100011001001000011111000110111100001111111"};
int[] counters = new int[input[0].length()];
for (String s : input) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1') {
counters[i]++;
}
}
}
for (int i = 0; i < counters.length; i++) {
System.out.print(counters[i]);
}