java - create sub-arrays from one main array -


i still getting grip on java. need in looping through array.

my array looks 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 below code identify beginning , end of record in array               string beginrecord = "[begin record]";                 boolean foundbeginrecord = false;                 int foundindex = 0;                 (int i=0; i<allrecords.length; i++) {                     if (beginrecord.equals(allrecords[i])) {                     foundbeginrecord = true;                     foundindex = i+1;   //added 1                     break;                       }                 }            string endrecord = "[end record]";           boolean foundendrecord = false;                 int foundend = 0;                 (int i=0; i<allrecords.length; i++) {                     if (endrecord.equals(allrecords[i])) {                     foundendrecord = true;                     foundend = i;   //one not added                      break;                       }                 } 

//i use below code slice off part of array   string [] partallrecords = arrays.copyofrange(allrecords, foundindex, foundend); 

//this gives me new sub-array this: "[id]1", "[cname]agnes", "[age]12"

the above code works ok. need read/slice portion allrecords array i.e.; "[id]2", "[cname]hellen", "[age]5" , slice next block "[id]3", "[cname]jack", "[age]34" till end of allrecords array.

how can this?

thank you!

your existing code close , can modified pretty want. key thing remember, not doing now, start left off, instead of restarting @ 0. have (greatly simplified illustration):

int foundindex = 0; (int i=0; i<allrecords.length; i++)    ... find start record  int foundend = 0; (int i=0; i<allrecords.length; i++) {    ... find end record 

note start @ 0 each time. however, know couple of things:

  • the start record won't before previous end, can start searching after previous record.
  • the end record won't before start, can start searching @ start index.

then, saving location of end of previous record, , picking there, logic can repeatedly in loop until valid records consumed input.

with in mind, again over-simplified:

int foundindex, foundend = -1;  {      foundindex = 0;     (int i=foundend + 1; i<allrecords.length; i++)        ... find start record      foundend = 0;     (int i=foundindex + 1; i<allrecords.length; i++) {        ... find end record  } while record found; 

there other possible ways simplify code (e.g. use arraylist indexof(), use simple state machine, etc.), above stays pretty close current code.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -