Pankesh Patel
Pankesh Patel

Reputation: 1302

converting List<String> to Object

In my current project, I want to convert List<String> to RegionLabel Object.

For instance, User enters String value one by one in order -- center, floor, room . I am storing user's input into List.

Now, my internal data structure in RegionLabel( String centerString, String floorString, String roomString);

My requirement is to convert List<String> data structure into RegionLabel("center", floor", "room" ); data structure.

One note: My RegionLabel class is generated on-fly. On-fly means at design time, I do not know that "how many arguments RegionLabel constructors have?"

-Pankesh

Upvotes: 0

Views: 176

Answers (3)

aioobe
aioobe

Reputation: 421220

Perhaps you're just after

new RegionLabel(list.get(0), list.get(1), list.get(2))

I do not know that "how many arguments RegionLabel constructors have?"

Then you'll have to use reflection, or provide a constructor that accepts a List<String> as argument.

Upvotes: 2

pap
pap

Reputation: 27604

RegionLabel label = new RegionLabel(inputList.get(0), inputList.get(1), inputList.get(2));

Simplified without any errorhandling or validation but without any more constraints on your "conversion", it's hard to be more generic.

Upvotes: 0

Nishant
Nishant

Reputation: 55866

Create a new constructor like RegionLabel(List<String> list). This should do.

public RegionLabel(List<String> list) throws Exception{
     if( null == list || list.size() < 3 )
          throw new Exception("illegal parameter");
     this.centre = list.get(0);
      ...
}

Upvotes: 1

Related Questions