2

Consider a Class DataHolder

Attributes:

  1. Name: data name
  2. Value: value (comma separated)

My input is a list of DataHolders. eg:

List of DataHolder:
DataHolder1: name:A, value:1,2

DataHolder2: name:B, value:X

DataHolder3: name:C, value:a,b,c

I want to build a list of strings like this from the data: 1Xa 1Xb 1Xc 2Xa 2Xb 2Xc

I came up with a logic using reccursion.

 void formString(List<DataHolder> dataHolders, int currentIndex, String formedString, int size){
 if(currentIndex == size){
      // store formedString in a list and return
 }
 else{
      1. get value from dataholder
      2. String tokenize the value
      3. iterate over the tokenized value and for each tokenized value call 

 formString(dataHolders, currentIndex + 1, formedString + tokeizedValue, 
 size)
 }
 }

My code works and it produces the expected output. 1Xa 1Xb 1Xc 2Xa 2Xb 2Xc

is there a better way to do this other than the recursive method?

Community
  • 1
  • 1
Gaming God
  • 29
  • 8
  • Given how big the result can get, I would highly recommend returning an `Iterator` object that goes through the resulting list without having to generate all of it in memory. – btilly May 07 '19 at 17:04

0 Answers0