Look into the modulo operator. (%).
|
Basically:
How do i do this simpler? (i.e get rid of all the if statements)Code:int[] i2; if (i == 1) i2 = new int[] {-1}; else if (i == 2) i2 = new int[] {-1, -1}; else if (i == 3) i2 = new int[] {-1, -1, -1};
edit;Code:int[] i2 = new int[i]; for (int index = 0; index < i2.length; i2++) { i2[index] = -1; }
Note that there is probably some convenient Arrays/ArrayUtils function from the google commons library for filling an array with default values so you don't have to do it in such a linear fashion each time.Code:int[] i2 = new int[i]; Arrays.fill(i2, -1);
Thanks, all i rlly needed was new int[i]; lmao
heres what i ended with if any1 cares to find the answer to the title :/
Code:ipublic static int[] getRuneSettingsFromCfg(int[] runeConfig, boolean amounts){ int[] runeIds = new int[(runeConfig.length / 2)]; int set = 0; for (int i = 0; runeConfig.length > i; i++){ if ((amounts && !Utils.isOddNumber(i)) || (!amounts && Utils.isOddNumber(i))) continue; runeIds[set] = runeConfig[i]; set++; } return runeIds; }
Yes, I would expect everyone to use OO "practices" when they're using an OO language, beginner or not. Any beginner level Java book will throw OO concepts at you very early on.
Then again, maybe I'm "chirping" in an attempt to see if OP has interest (which he obviously doesn't.) If he had replied, and wanted to know what I meant, I would have explained. I'm not going to go through the trouble of asking for existing code, helping him convert it to enums or whatever, etc. if he doesn't want help.
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |