this question seems to have been asked, but I didn’t find anything, so here ...
I have a constructor to which a string that is limited is passed. From this line, I need to populate the instance variables of the object. I can easily split the string into a separator to give me an array of strings. I know that I can just iterate over the array and set the instance variables using ifs or a switch / case statement based on the current index of the array - however this is just a little annoying. Pseudocode:
String[] tokens = <from generic string tokenizer>;
for (int i = 0;i < tokens.length;i++) {
switch(i) {
case(0): instanceVariableA = tokens[i];
case(1): instanceVarliableB = tokens[i];
...
}
}
Does anyone have any ideas on how I am doing this better / better?
For what it's worth, I work in Java, but I think it is an independent language.