2

我有一个包含以下字符串的数组列表:

02, String1 
03, Num1
03, Num2
02, String2

基于来自另一种方法的输入,我需要重复以 03 次开头的字符串,然后将其添加回相同的 arraylist number of times = 4 输出:

02, String1 
03, Num1
03, Num2
03, Num1
03, Num2
03, Num1
03, Num2
03, Num1
03, Num2
02, String2

请任何帮助,

提前致谢。

到目前为止,我创建的代码具有数组列表所需的逻辑,但我需要完成的部分在上面……这不是家庭作业。

private static String mergeFieldName(String string_element, int j, int numOfTimes)
{
     int innerCnt3 = 0;
     int innerCnt4=0;
     int innerCnt5=0;
     innerCnt3 = j+1;

     String tmpStr = "";
     innerCnt4 =innerCnt3;
     innerCnt5 = innerCnt3;
     while (true)
        {
            tmpStr = holdRecord.get(innerCnt5);
            tmpStr = tmpStr.replaceAll("( )+",ONE_SPACE).trim();
            if (!tmpStr.trim().substring(0, 2).equalsIgnoreCase("03"))
            {
                break;
            }
            innerCnt5++;
        }
     while (true)
        {
            tmpStr = holdRecord.get(innerCnt4);
            tmpStr = tmpStr.replaceAll("( )+",ONE_SPACE).trim();
            if (!tmpStr.trim().substring(0, 2).equalsIgnoreCase("03"))
            {
                break;
            }
            innerCnt4++;
        }
    while (true)
    {
        tmpStr = holdRecord.get(innerCnt3);
        tmpStr = tmpStr.replaceAll("( )+",ONE_SPACE).trim();

        if (tmpStr.trim().substring(0, 2).equalsIgnoreCase("03"))
        {

            for( int i = 0; i < numOfTimes; i++ )
              {
                //sb.append( tmpStr );  

                 holdRecord.add(innerCnt4, tmpStr);
                 tmpStr = holdRecord.get(innerCnt4);
                 innerCnt4++;
              }
            if (innerCnt5 == innerCnt3 + 1)
            {
                break;
            }
            innerCnt3++;
        }

        if (!tmpStr.trim().substring(0, 2).equalsIgnoreCase("03"))
        {

            break;
        }
    }
    return tmpStr;
}
4

2 回答 2

1

这里有一些代码来实现这一点:

ArrayList<String> list = new ArrayList<String>(
  Arrays.asList("02, String1", "\n03, Num1", "\n03, Num2", "\n02, String2"));

int numberOfTimes = 4;

// always starts and ends with 02, so this is sufficient
int count3s = list.size() - 2;

String last = list.remove(list.size() - 1); // remove last 02

// optimization to prevent repeated resizing of the array
list.ensureCapacity(2 + numberOfTimes * count3s);

// we already have them once, so just repeat numberOfTimes-1 times
for (int i = 0; i < numberOfTimes-1; i++) 
for (int j = 0; j < count3s; j++)
   list.add(list.get(j+1));

list.add(last);  // add last 02 back

System.out.println(list);

印刷:

[02, String1, 
03, Num1, 
03, Num2, 
03, Num1, 
03, Num2, 
03, Num1, 
03, Num2, 
03, Num1, 
03, Num2, 
02, String2]
于 2013-08-23T08:11:14.460 回答
0

它不需要很复杂。您可以像这样使用简单的 cade:

// original list
List<String> list = new ArrayList<String>(Arrays.asList("02, String1", "03, Num1", 
                                         "03, Num2", "02, String2"));
int numToAdd = 3; // number of additional entries
int spos=0; // starting position of insertion
List<String> list03 = new ArrayList<String>(); // list to hold all 03 entries
for (int i=0; i<list.size(); i++) {
    if (list.get(i).contains("03,")) {
        if (spos == 0) spos = i; // set only once
        list03.add(list.get(i));
    }
}
// add list03 into main list numToAdd times
for (int i=0; i<numToAdd; i++)
    list.addAll(spos, list03);
// print the main list
for (String s: list)
    System.out.println(s);

输出:

02, String1
03, Num1
03, Num2
03, Num1
03, Num2
03, Num1
03, Num2
03, Num1
03, Num2
02, String2
于 2013-08-23T09:31:05.497 回答