Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 分组编号逻辑_Java - Fatal编程技术网

Java 分组编号逻辑

Java 分组编号逻辑,java,Java,我在A、B和C组各有1-10个。 例如A-1、A-2、A-3、B-4、C-5、B-6、A-7、C-8、A-9、A、10 我想把A、B、C三组分开 A 1-3, 7, 9-10 B 4, 6 C 5, 8 有人能帮我学习逻辑吗?这里有一些东西可以让你开始学习: String[] items = new String[] { "A-1", "B-2", "A-5" } // This is the data structure that will receive the final da

我在A、B和C组各有1-10个。 例如A-1、A-2、A-3、B-4、C-5、B-6、A-7、C-8、A-9、A、10

我想把A、B、C三组分开

A
1-3,
7,
9-10

B
4,
6

C
5,
8

有人能帮我学习逻辑吗?

这里有一些东西可以让你开始学习:

String[] items = new String[] {
  "A-1", "B-2", "A-5"
}

// This is the data structure that will receive the final data. The map key is the 
// group name (e.g. "A" for item "A-15") and the map value is a list of numbers that
// have been found for that group. TreeMap is chosen because the groups will be sorted
// alphabetically. If you don't need that, you could also use HashMap.
Map<String, List<Integer>> groups = new TreeMap<String, List<Integer>>();

for (String item : items) {
  // Split the item into the group and the number
  String group = item.substring(0, 1);
  String number = Integer.toString(item.substring(2));

  // See if this group is already registered in our Map
  List<Integer> groupData = groups.get(group);
  if (groupData==null) {
    groupData = new List<Integer>();
    groups.put(group, groupData);
  }

  // Add the number to the data
  groupData.add(number);
}
如果要合并连续的数字,您需要处理生成的数字列表,但如果您想一想,这应该不会太困难。

将帮助您创建所需的数据结构:

public static void main(final String[] args) {
    String input = "A-1,A-2,A-3,B-4,C-5,B-6,A-7,C-8,A-9,A-10";

    // create multimap
    Map<String, Collection<Integer>> map=Maps.newTreeMap();
    SortedSetMultimap<String, Integer> multimap = Multimaps.newSortedSetMultimap(
        map, new Supplier<SortedSet<Integer>>() {
          public SortedSet<Integer> get() {
              return new TreeSet<Integer>();
          }
    });

    //add data
    Splitter entrySplitter = Splitter.on(',');
    Splitter keyValueSplitter = Splitter.on('=');
    for (String entry : entrySplitter.split(input)) {
        Iterator<String> tokens = keyValueSplitter.split(entry).iterator();
        multimap.put(tokens.next(), Integer.valueOf(tokens.next()));
    }

    // read data
    for (Entry<String, Collection<Integer>> entry : map.entrySet()) {
        System.out.println(entry.getKey()+":");
        printMergedValues(entry.getValue());
    }

}

private static void printMergedValues(Collection<Integer> value) {
    // TODO implement this yourself
}
publicstaticvoidmain(最终字符串[]args){
字符串输入=“A-1、A-2、A-3、B-4、C-5、B-6、A-7、C-8、A-9、A-10”;
//创建多重映射
Map Map=Maps.newTreeMap();
SortedSetMultimap multimap=Multimaps.newSortedSetMultimap(
地图,新供应商(){
公共分类集get(){
返回新树集();
}
});
//添加数据
Splitter entrySplitter=Splitter.on(',');
Splitter keyValueSplitter=Splitter.on('=');
for(字符串条目:entrySplitter.split(输入)){
迭代器标记=keyValueSplitter.split(entry.Iterator();
multimap.put(tokens.next()、Integer.valueOf(tokens.next());
}
//读取数据
for(条目:map.entrySet()){
System.out.println(entry.getKey()+“:”);
PrintMergedValue(entry.getValue());
}
}
私有静态void printmergedvalue(集合值){
//要自己实现这一点吗
}
我留给你的唯一一件事就是加入小组

我会这样做:

     String[] input = {"A-1","A-2","A-3","B-4","C-5","B-6","A-7","C-8","A-9"};
     Map<String, Set<Integer>> result = new HashMap<String, Set<Integer>>();
     String[] inputSplit;
     String group;
     Integer groupNumber;
     for (String item : input)
     {
         inputSplit = item.split("-");
         group = inputSplit[0];
         groupNumber = Integer.valueOf( inputSplit[1] );
         if ( result.get(group) == null ) { result.put(group, new HashSet<Integer>()); }
         result.get(group).add(groupNumber);
     }

     for (Map.Entry entry : result.entrySet())
     {
         System.out.println( entry.getKey() + ":"  +  entry.getValue() );
     }
String[]输入={“A-1”、“A-2”、“A-3”、“B-4”、“C-5”、“B-6”、“A-7”、“C-8”、“A-9”};
映射结果=新的HashMap();
字符串[]inputSplit;
字符串组;
整数组数;
for(字符串项:输入)
{
inputSplit=项目拆分(“-”);
组=输入拆分[0];
groupNumber=整数.valueOf(inputSplit[1]);
if(result.get(group)==null){result.put(group,new HashSet());}
结果.获取(组).添加(组编号);
}
for(Map.Entry:result.entrySet())
{
System.out.println(entry.getKey()+“:”+entry.getValue());
}

8号在B组和C组中。没有其他号码在多个组中。这是一个错误,还是故意的?块前的文本在组中具有不同的数字。另一个问题是:你的问题是什么?
     String[] input = {"A-1","A-2","A-3","B-4","C-5","B-6","A-7","C-8","A-9"};
     Map<String, Set<Integer>> result = new HashMap<String, Set<Integer>>();
     String[] inputSplit;
     String group;
     Integer groupNumber;
     for (String item : input)
     {
         inputSplit = item.split("-");
         group = inputSplit[0];
         groupNumber = Integer.valueOf( inputSplit[1] );
         if ( result.get(group) == null ) { result.put(group, new HashSet<Integer>()); }
         result.get(group).add(groupNumber);
     }

     for (Map.Entry entry : result.entrySet())
     {
         System.out.println( entry.getKey() + ":"  +  entry.getValue() );
     }