0

I have a method in java let say:

public List<Data> getData(String[] str, Integer[] num, String ss);

I have to pass two array and a non-array arguments as shown above, this method returns List of objects of type Data.

Now, the question is how to create a list of objects which contain every possible combination of elements of the array.

i.e : Object with elements :

getData(String[0] str, Integer[0] num, String ss);
getData(String[0] str, Integer[1] num, String ss);
getData(String[1] str, Integer[0] num, String ss);
getData(String[1] str, Integer[1] num, String ss);

Any clue will be appreciated.

1
  • 1
    Use nested loop. Sorry I'm not going to write you a code because this looks very much like a homework and you should be doing your research. If you ask what's nested loop then we're happy to help Commented Dec 2, 2015 at 11:28

1 Answer 1

2

Create a List<Data> inside your method then loop through one array

for (String s: str)

then for each element in that array, loop through the other array, this is called a nested loop

for (Integer i : num)

Then create a Data object for each combination and add to a List<Data>

 for (String s: str){
    for (Integer i : num){
       //TODO Add new Data(s,i,ss) to list
    }
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, One more condition is what if either of the two arrays are empty (having no elements).
if either is empty then the resulting data is just the non-empty array, converted to Data with nulls set to the other property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.