首页 > 代码库 > 多个字符串数组求相同数据及出现次数
多个字符串数组求相同数据及出现次数
String[] str = {"12","121"}; String[] str1 = {"12","121"}; String[] str2 = {"12","121","122"}; String[] str3 = {"123","1234","125","126","111"}; String[] str4 = {"1234","126","125","1232","111"}; String[] str6 = {"12","121","222","1433","1234","126","125","1232"};
12,121,122 一块儿出现 2次
1234,125,126一块儿出现3次
1234,126,125,111 一块儿出现2次
package csdn;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;/** * 自己定义一个数据类型,用来模拟二进制计算 * @author sztsiang * */class Num{ private byte[] data; private int length; Num(int length){ data = http://www.mamicode.com/new byte[length];"contains:"+ t_value); set.add(t_value); } } return set; } /** * 返回一个字符串数组对应的符合次数 * @param strs * @return count */ public int getEqualCount(String[] strs){ Set<Integer> set = null; for(String str : strs){ Set<Integer> t_set = getSet(str); set = (set == null ? t_set : combineSet(set, t_set)); } return set.size(); } /** * 把字符串数组变成一个字符串 * @param args * @return String */ private String ArraysToString(String args[]){ StringBuilder sb = new StringBuilder(); sb.append("["); int length = args.length; for(int i = 0 ; i < length -1; i++){ sb.append(args[i] +", "); } sb.append(args[length-1]); sb.append("]"); return sb.toString(); } /** * 打印 * @param str * @param count */ private void printResult(String str, int count){ if(count > 1)//当大于一次的时候输出,如果想都输出就不写if System.out.println(str + "==>" + count); } /** * 生成key的各种组合可能,并计算该组合出现的次数 */ public void Traversal(){ List<String> list = new ArrayList<String>(map.keySet()); int list_size = list.size(); Num n = new Num(list.size()); int index = 0; String str[]; while(n.increase()){ index = 0; int t = n.getCount(); str = new String[t]; for(int i = 0; i < list_size; i++){ if(n.get(i) == 1) str[index++] = list.get(i); } int equal_count = getEqualCount(str); printResult(ArraysToString(str), equal_count); } } /** * main * @param args */ public static void main(String args[]){ String[] str = {"12","121"}; String[] str1 = {"12","121"}; String[] str2 = {"12","121","122"}; String[] str3 = {"123","1234","125","126","111"}; String[] str4 = {"1234","126","125","1232","111"}; String[] str6 = {"12","121","222","1433","1234","126","125","1232"}; EqualAndCount eac = new EqualAndCount(); eac.add(str); eac.add(str1); eac.add(str2); eac.add(str3); eac.add(str4); eac.add(str6); eac.Traversal();//自己组合所有的可能,然后查看次数 System.out.println(eac.getEqualCount(str)); System.out.println( eac.getEqualCount(str2)); System.out.println(eac.getEqualCount(new String[]{"1234", "126", "125","111"})); System.out.println(eac.getEqualCount(new String[]{"1234", "126", "125"}) + "<<<"); System.out.println(eac.getEqualCount(new String[]{"111"})); System.out.println(eac.getEqualCount(new String[]{"1433","111"})); }}
自己想的办法实现的,正确性未验证,效率都没考虑……毕竟是自己想出来的,就在这里记一下……
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。