问题: 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;
}