Thread: Help split int[] into int[] x2

Page 1 of 2 12 LastLast
Results 1 to 10 of 14
  1. #1 Help split int[] into int[] x2 
    Registered Member
    Join Date
    May 2016
    Posts
    384
    Thanks given
    52
    Thanks received
    35
    Rep Power
    29
    Basically:

    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};
    How do i do this simpler? (i.e get rid of all the if statements)
    Reply With Quote  
     

  2. #2  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    26
    Posts
    1,474
    Thanks given
    3,312
    Thanks received
    691
    Rep Power
    1098
    Look into the modulo operator. (%).
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    May 2016
    Posts
    384
    Thanks given
    52
    Thanks received
    35
    Rep Power
    29
    Quote Originally Posted by hc747 View Post
    Look into the modulo operator. (%).
    the problem i have is putting them into the int[] format tho, atm i have
    runeIds[i > 0 ? i-1 : i] = runeConfig[i];

    but that doesn't seem to work properly
    Reply With Quote  
     

  4. #4  
    Respected Member


    George's Avatar
    Join Date
    Mar 2009
    Posts
    7,099
    Thanks given
    2,226
    Thanks received
    3,146
    Rep Power
    5000
    What exactly are you trying to do, your code makes no sense to me lol.
    Attached image

    Spoiler for Spoilers!:
    Attached image
    Attached image
    Attached image
    Attached image
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    May 2016
    Posts
    384
    Thanks given
    52
    Thanks received
    35
    Rep Power
    29
    Quote Originally Posted by Idiot Bird View Post
    What exactly are you trying to do, your code makes no sense to me lol.
    I overcomplicated the thread, re did it
    Reply With Quote  
     

  6. #6  
    Donator

    Jason's Avatar
    Join Date
    Aug 2009
    Posts
    6,092
    Thanks given
    2,402
    Thanks received
    2,823
    Rep Power
    4550
    Code:
    int[] i2 = new int[i];
    
    for (int index = 0; index < i2.length; i2++) {
        i2[index] = -1;
    }
    edit;
    Code:
    int[] i2 = new int[i];
    Arrays.fill(i2, -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.
    Reply With Quote  
     

  7. Thankful user:


  8. #7  
    Registered Member
    Join Date
    May 2016
    Posts
    384
    Thanks given
    52
    Thanks received
    35
    Rep Power
    29
    Quote Originally Posted by Jason View Post
    Code:
    int[] i2 = new int[i];
    
    for (int index = 0; index < i2.length; i2++) {
        i2[index] = -1;
    }
    edit;
    Code:
    int[] i2 = new int[i];
    Arrays.fill(i2, -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.
    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;
    	}
    Reply With Quote  
     

  9. #8  
    Donator
    clrlslwi's Avatar
    Join Date
    Mar 2016
    Posts
    201
    Thanks given
    32
    Thanks received
    110
    Rep Power
    102
    Quote Originally Posted by Java100 View Post
    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;
    	}
    That's.. not how you OOP
    Reply With Quote  
     

  10. #9  
    Donator

    Jason's Avatar
    Join Date
    Aug 2009
    Posts
    6,092
    Thanks given
    2,402
    Thanks received
    2,823
    Rep Power
    4550
    Quote Originally Posted by clrlslwi View Post
    That's.. not how you OOP
    You can't expect someone who is asking how to initialize an array to use OO practices. Then again, if you're going to chirp you better give a better solution.
    Reply With Quote  
     

  11. Thankful users:


  12. #10  
    Donator
    clrlslwi's Avatar
    Join Date
    Mar 2016
    Posts
    201
    Thanks given
    32
    Thanks received
    110
    Rep Power
    102
    Quote Originally Posted by Jason View Post
    You can't expect someone who is asking how to initialize an array to use OO practices. Then again, if you're going to chirp you better give a better solution.
    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.
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [HELP] Adding models and ints on Hybrid PVP
    By LorenzoMKW in forum Help
    Replies: 12
    Last Post: 01-17-2012, 08:11 AM
  2. Replies: 4
    Last Post: 02-24-2011, 12:15 PM
  3. private void objectToRemove3(int X, int Y, int h) {
    By Ninja assassin in forum Help
    Replies: 8
    Last Post: 11-07-2010, 12:18 AM
  4. Replies: 8
    Last Post: 06-26-2010, 07:42 PM
  5. Help with my model ints
    By Rainaka in forum Help
    Replies: 10
    Last Post: 11-15-2009, 09:33 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •