zoukankan      html  css  js  c++  java
  • java 连续数字数组分组

    问题:
    1. 将Lis list = Arrays.asList(1,2,3,5,8,9,10), 拆分成 [1,2,3] 、[5]、 [8,9,10] ,
    2. 再传入一个数字 9, 将匹配数字9的数组输出来 ?

       /**
     * 将 int [1,2,3,5,8,9,10] 连续的分组成[1,2,3]、[5]、[8,9,10]
     * 再传入9 ,匹配上面的分组取出 [9,10]
     * @param NoNum 传入的整形数组
     * @param charct 传入匹配的数字
     * @return 返回含有匹配数字的数组
     */
    
    public  String convert(List<Integer> NoNum, int charct) {
    	int state = 0;
    	String result = "";
    	for (int i = 0; i < NoNum.size(); i++)
    	{
    		if (i == NoNum.size() - 1){
    			state = 2;
    		}
    		if (state == 0)
    		{
    			if (NoNum.get(i + 1) == NoNum.get(i) + 1)
    			{
    				result += Integer.toString(NoNum.get(i));
    				result += "-";
    				state = 1;
    			}
    			else
    			{
    				result += Integer.toString(NoNum.get(i));
    				result += ",";
    			}
    		}
    		else if (state == 1)
    		{
    			if (NoNum.get(i + 1) != NoNum.get(i) + 1)
    			{
    				result += Integer.toString(NoNum.get(i));
    				result += ",";
    				state = 0;
    			} else {
    				result += NoNum.get(i)+"-";
    			}
    		}
    		else
    		{
    			result += Integer.toString(NoNum.get(i));
    		}
    	}
    
    	String [] str = result.split(",");
    	for ( int stritem = 0 ; stritem < str.length ; stritem++ ) {
    		String [] sp = str[stritem].split("-");
    		List<String> tt = Arrays.asList(sp);
    		if ( tt.contains( charct+"")) {
    			result = str[stritem].replace("-",",");
    		}
    	}
    	return result;
    
    }
  • 相关阅读:
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    fzu2020软工作业5
    [fiddler] 使用AutoResponder功能修改http响应
    [jest] 史莱姆也能学会的前端测试
    fzu2020软工作业3
    fzu2020软工作业4
    fzu2020软工作业2
    [python] 简易代码量统计脚本
  • 原文地址:https://www.cnblogs.com/heavenTang/p/11760177.html
Copyright © 2011-2022 走看看