从一个主阵列创建子阵列

时间:2014-03-24 18:18:45

标签: java arrays slice

我仍然掌握着Java。我需要一些循环数组的帮助。

我的数组看起来像这样;

String [] allRecords = ["[BEGIN RECORD]", "[ID]1", "[cName]Agnes", "[Age]12", "[END RECORD]", "[BEGIN RECORD]", "[ID]2", "[cName]Hellen", "[Age]5", "[END RECORD]", "[BEGIN RECORD]", "[ID]3", "[cName]Jack", "[Age]34", "[END RECORD]" ];

//i use the below code to identify the beginning and end of a record in the array

             String beginRecord = "[BEGIN RECORD]";
                boolean foundBeginRecord = false;
                int foundIndex = 0;
                for (int i=0; i<allRecords.length; i++) {
                    if (beginRecord.equals(allRecords[i])) {
                    foundBeginRecord = true;
                    foundIndex = i+1;   //added one
                    break;  
                    }
                }

          String endRecord = "[END RECORD]";
          boolean foundEndRecord = false;
                int foundEnd = 0;
                for (int i=0; i<allRecords.length; i++) {
                    if (endRecord.equals(allRecords[i])) {
                    foundEndRecord = true;
                    foundEnd = i;   //one NOT added 
                    break;  
                    }
                }

//i then use the below code to slice off part of the array

 String [] partAllRecords = Arrays.copyOfRange(allRecords, foundIndex, foundEnd);

//这给了我一个像这样的新子数组:"[ID]1", "[cName]Agnes", "[Age]12"

上面的代码工作正常。我现在需要的是从allRecords数组中读取/切片另一部分,即; &#34; [ID] 2&#34;,&#34; [cName] Hellen&#34;,&#34; [年龄] 5&#34; 然后切片下一个块&#34; [ID] 3&#34;,&#34; [cName] Jack&#34;,&#34; [年龄] 34&#34; 直到allRecords数组结束。

我该怎么做?

谢谢!

2 个答案:

答案 0 :(得分:1)

您现有的代码很接近,可以很容易地进行修改以完成您想要的操作。要记住的关键事项是,你现在没做的是从你离开的地方开始,而不是在0处重新启动。所以你有(大大简化了插图):

int foundIndex = 0;
for (int i=0; i<allRecords.length; i++)
   ... find start record

int foundEnd = 0;
for (int i=0; i<allRecords.length; i++) {
   ... find end record

请注意,每次从0开始。但是,你知道几件事:

  • 开始记录不会在上一个结束之前,因此我们可以在上一个记录之后开始搜索。
  • 结束记录不会在开始之前,因此我们可以从开始索引开始搜索。

然后,通过保存上一条记录结束的位置,并从那里开始,您的逻辑现在可以重复循环,直到从输入中消耗所有有效记录。

考虑到这一点,再次非常简化:

int foundIndex, foundEnd = -1;

do {

    foundIndex = 0;
    for (int i=foundEnd + 1; i<allRecords.length; i++)
       ... find start record

    foundEnd = 0;
    for (int i=foundIndex + 1; i<allRecords.length; i++) {
       ... find end record

} while a record was found;

还有其他可能的方法来简化您的代码(例如,使用ArrayList indexOf(),使用简单的状态机等等),但上述内容与您当前的代码非常接近。< / p>

答案 1 :(得分:0)

首先,感谢Trenin和Jason的指导。 我为这项任务而苦苦挣扎,有一天为了别人的利益,我会粘贴下面对我有用的代码;

String [] allRecords = {"[BEGIN RECORD]", "[ID]1", "[cName]Agnes", "[Age]12", "[END RECORD]", "[BEGIN RECORD]", "[ID]2", "[cName]Hellen", "[Age]5", "[END RECORD]", "[BEGIN RECORD]", "[ID]3", "[cName]Jack", "[Age]34", "[END RECORD]"};

String beginRecord = "[BEGIN RECORD]";
String endRecord = "[END RECORD]";                
int foundIndex = 0;
int foundEnd = 0;

      for (int i=0; i<allRecords.length; i++) {
      if (endRecord.equals(allRecords[i])) {
           foundEnd = i;    
           break;   
           }                
      }

      //by saving the location of the end of the previous record, and picking up from there, your logic can now be repeatedly in a loop until all valid records are consumed from the input
      foundEnd = foundEnd-1; //arrays are zero based


      for (int i=0; i<allRecords.length; i++) {
          if (beginRecord.equals(allRecords[i])) {
             foundIndex = i+1;  //arrays are zero based
             String [] partAllRecords = Arrays.copyOfRange(allRecords, foundIndex, foundIndex+foundEnd);                   
             System.out.println(Arrays.toString(partAllRecords)); 
             //prints below arrays in logcat
             //[[ID]1, [cName]Agnes, [Age]12]
             //[[ID]2, [cName]Hellen, [Age]5]
             //[[ID]3, [cName]Jack, [Age]34]
         }
      }