从一个主阵列创建子阵列

时间:2022-11-07 12:17:26

I am still getting my grip on Java. I need some help in looping through an array.

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

My array looks like this;

我的阵列看起来像这样;

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);

//this gives me a new sub-array like this: "[ID]1", "[cName]Agnes", "[Age]12"

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

The above code works OK. What I need now is to read/slice another portion from the allRecords array i.e.; "[ID]2", "[cName]Hellen", "[Age]5" and then slice the next block "[ID]3", "[cName]Jack", "[Age]34" till the end of the allRecords Array.

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

How can I do this?

我怎样才能做到这一点?

Thank you!

谢谢!

2 个解决方案

#1


1  

Your existing code is close and can be modified pretty easily to do what you want. The key thing to remember, which you are not doing now, is to start where you left off, instead of restarting at 0. So you have (greatly simplified for illustration):

您现有的代码很接近,可以很容易地修改,以实现您想要的。要记住的关键是你现在没有做的,就是从你离开的地方开始,而不是从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

Note that you start at 0 each time. However, you know a couple of things:

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

  • The start record won't be before the previous end, so we can start searching just after the previous record.
  • 开始记录不会在前一个结束之前,因此我们可以在上一个记录之后开始搜索。
  • The end record won't be before the start, so we can start searching at the start index.
  • 结束记录不会在开始之前,因此我们可以从起始索引开始搜索。

Then, 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.

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

With that in mind, again very over-simplified:

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

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;

There are other possible ways to simplify your code (e.g. use an ArrayList with indexOf(), use a simple state machine, etc.), but the above stays pretty close to your current code.

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

#2


0  

First, thank you Trenin and Jason for your guidance. I struggled with the task and for the benefit of someone else one day, i will paste below the code that has worked for me;

首先,感谢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]
         }
      }

#1


1  

Your existing code is close and can be modified pretty easily to do what you want. The key thing to remember, which you are not doing now, is to start where you left off, instead of restarting at 0. So you have (greatly simplified for illustration):

您现有的代码很接近,可以很容易地修改,以实现您想要的。要记住的关键是你现在没有做的,就是从你离开的地方开始,而不是从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

Note that you start at 0 each time. However, you know a couple of things:

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

  • The start record won't be before the previous end, so we can start searching just after the previous record.
  • 开始记录不会在前一个结束之前,因此我们可以在上一个记录之后开始搜索。
  • The end record won't be before the start, so we can start searching at the start index.
  • 结束记录不会在开始之前,因此我们可以从起始索引开始搜索。

Then, 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.

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

With that in mind, again very over-simplified:

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

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;

There are other possible ways to simplify your code (e.g. use an ArrayList with indexOf(), use a simple state machine, etc.), but the above stays pretty close to your current code.

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

#2


0  

First, thank you Trenin and Jason for your guidance. I struggled with the task and for the benefit of someone else one day, i will paste below the code that has worked for me;

首先,感谢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]
         }
      }