Pretty sure I'm over complicating my loops but unsure how to simplify





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







5















As the title says I just need some help/advice on how I can simplify a part of my code. I get the output I want but it's obvious to see that the way I go about it is a bit excessive. What I'm trying to do in my program is pass the array



int  myInches = {89,12,33,7,72,42,76,49,69,85,61,23};


Into my buildFeetArray method which just takes the array elements, divides them by 12 to get a new element value which is then put in a new array which is returned. Here is the method



 public static int buildFeetArray(int arrayParam) {
int sum = 0;
int lessthan1 = 0;
int lessthan2 = 0;
int lessthan3 = 0;
int lessthan4 = 0;
int lessthan5 = 0;
int lessthan6 = 0;
int lessthan7 = 0;
int lessthan8 = 0;
for (int count = 0; count < arrayParam.length; count++) {
if (arrayParam[count] / 12 == 0) {
lessthan1++;
} else if (arrayParam[count] / 12 == 1) {
lessthan2++;
} else if (arrayParam[count] / 12 == 2) {
lessthan3++;
} else if (arrayParam[count] / 12 == 3) {
lessthan4++;
} else if (arrayParam[count] / 12 == 4) {
lessthan5++;
} else if (arrayParam[count] / 12 == 5) {
lessthan6++;
} else if (arrayParam[count] / 12 == 6) {
lessthan7++;
} else if (arrayParam[count] / 12 == 7) {
lessthan8++;
}
}
int newArray = {lessthan1, lessthan2, lessthan3, lessthan4, lessthan5, lessthan6, lessthan7, lessthan8};
return newArray;
}


Ideally the output should be



int length = 8;
[0] = 1;
[1] = 2;
[2] = 1;
[3] = 1;
[4] = 1;
[5] = 2;
[6] = 2;
[7] = 2;


Which it is but there's definitely an easier way to go about it, if possible I'd like to avoid using lists and sticking with loops as I need practice with them. Thank you in advance for any advice/tips.










share|improve this question




















  • 19





    The code in your question does not match at all with the specification. I'm having a hard time understanding what the code does, to be honest, but it clearly does not do what you claim it does. Can you give a precise specification of what the code is supposed to do?

    – Jörg W Mittag
    10 hours ago






  • 1





    It looks like you're returning the amount of entries that have 1, 2, 3, 4, etc. feet, Which doesn't match what you're saying it does. Also the "lessThan" naming make no sense since lessThan8 should be all of the entries given your input.

    – PeejWeej
    7 hours ago


















5















As the title says I just need some help/advice on how I can simplify a part of my code. I get the output I want but it's obvious to see that the way I go about it is a bit excessive. What I'm trying to do in my program is pass the array



int  myInches = {89,12,33,7,72,42,76,49,69,85,61,23};


Into my buildFeetArray method which just takes the array elements, divides them by 12 to get a new element value which is then put in a new array which is returned. Here is the method



 public static int buildFeetArray(int arrayParam) {
int sum = 0;
int lessthan1 = 0;
int lessthan2 = 0;
int lessthan3 = 0;
int lessthan4 = 0;
int lessthan5 = 0;
int lessthan6 = 0;
int lessthan7 = 0;
int lessthan8 = 0;
for (int count = 0; count < arrayParam.length; count++) {
if (arrayParam[count] / 12 == 0) {
lessthan1++;
} else if (arrayParam[count] / 12 == 1) {
lessthan2++;
} else if (arrayParam[count] / 12 == 2) {
lessthan3++;
} else if (arrayParam[count] / 12 == 3) {
lessthan4++;
} else if (arrayParam[count] / 12 == 4) {
lessthan5++;
} else if (arrayParam[count] / 12 == 5) {
lessthan6++;
} else if (arrayParam[count] / 12 == 6) {
lessthan7++;
} else if (arrayParam[count] / 12 == 7) {
lessthan8++;
}
}
int newArray = {lessthan1, lessthan2, lessthan3, lessthan4, lessthan5, lessthan6, lessthan7, lessthan8};
return newArray;
}


Ideally the output should be



int length = 8;
[0] = 1;
[1] = 2;
[2] = 1;
[3] = 1;
[4] = 1;
[5] = 2;
[6] = 2;
[7] = 2;


Which it is but there's definitely an easier way to go about it, if possible I'd like to avoid using lists and sticking with loops as I need practice with them. Thank you in advance for any advice/tips.










share|improve this question




















  • 19





    The code in your question does not match at all with the specification. I'm having a hard time understanding what the code does, to be honest, but it clearly does not do what you claim it does. Can you give a precise specification of what the code is supposed to do?

    – Jörg W Mittag
    10 hours ago






  • 1





    It looks like you're returning the amount of entries that have 1, 2, 3, 4, etc. feet, Which doesn't match what you're saying it does. Also the "lessThan" naming make no sense since lessThan8 should be all of the entries given your input.

    – PeejWeej
    7 hours ago














5












5








5


1






As the title says I just need some help/advice on how I can simplify a part of my code. I get the output I want but it's obvious to see that the way I go about it is a bit excessive. What I'm trying to do in my program is pass the array



int  myInches = {89,12,33,7,72,42,76,49,69,85,61,23};


Into my buildFeetArray method which just takes the array elements, divides them by 12 to get a new element value which is then put in a new array which is returned. Here is the method



 public static int buildFeetArray(int arrayParam) {
int sum = 0;
int lessthan1 = 0;
int lessthan2 = 0;
int lessthan3 = 0;
int lessthan4 = 0;
int lessthan5 = 0;
int lessthan6 = 0;
int lessthan7 = 0;
int lessthan8 = 0;
for (int count = 0; count < arrayParam.length; count++) {
if (arrayParam[count] / 12 == 0) {
lessthan1++;
} else if (arrayParam[count] / 12 == 1) {
lessthan2++;
} else if (arrayParam[count] / 12 == 2) {
lessthan3++;
} else if (arrayParam[count] / 12 == 3) {
lessthan4++;
} else if (arrayParam[count] / 12 == 4) {
lessthan5++;
} else if (arrayParam[count] / 12 == 5) {
lessthan6++;
} else if (arrayParam[count] / 12 == 6) {
lessthan7++;
} else if (arrayParam[count] / 12 == 7) {
lessthan8++;
}
}
int newArray = {lessthan1, lessthan2, lessthan3, lessthan4, lessthan5, lessthan6, lessthan7, lessthan8};
return newArray;
}


Ideally the output should be



int length = 8;
[0] = 1;
[1] = 2;
[2] = 1;
[3] = 1;
[4] = 1;
[5] = 2;
[6] = 2;
[7] = 2;


Which it is but there's definitely an easier way to go about it, if possible I'd like to avoid using lists and sticking with loops as I need practice with them. Thank you in advance for any advice/tips.










share|improve this question
















As the title says I just need some help/advice on how I can simplify a part of my code. I get the output I want but it's obvious to see that the way I go about it is a bit excessive. What I'm trying to do in my program is pass the array



int  myInches = {89,12,33,7,72,42,76,49,69,85,61,23};


Into my buildFeetArray method which just takes the array elements, divides them by 12 to get a new element value which is then put in a new array which is returned. Here is the method



 public static int buildFeetArray(int arrayParam) {
int sum = 0;
int lessthan1 = 0;
int lessthan2 = 0;
int lessthan3 = 0;
int lessthan4 = 0;
int lessthan5 = 0;
int lessthan6 = 0;
int lessthan7 = 0;
int lessthan8 = 0;
for (int count = 0; count < arrayParam.length; count++) {
if (arrayParam[count] / 12 == 0) {
lessthan1++;
} else if (arrayParam[count] / 12 == 1) {
lessthan2++;
} else if (arrayParam[count] / 12 == 2) {
lessthan3++;
} else if (arrayParam[count] / 12 == 3) {
lessthan4++;
} else if (arrayParam[count] / 12 == 4) {
lessthan5++;
} else if (arrayParam[count] / 12 == 5) {
lessthan6++;
} else if (arrayParam[count] / 12 == 6) {
lessthan7++;
} else if (arrayParam[count] / 12 == 7) {
lessthan8++;
}
}
int newArray = {lessthan1, lessthan2, lessthan3, lessthan4, lessthan5, lessthan6, lessthan7, lessthan8};
return newArray;
}


Ideally the output should be



int length = 8;
[0] = 1;
[1] = 2;
[2] = 1;
[3] = 1;
[4] = 1;
[5] = 2;
[6] = 2;
[7] = 2;


Which it is but there's definitely an easier way to go about it, if possible I'd like to avoid using lists and sticking with loops as I need practice with them. Thank you in advance for any advice/tips.







java for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago









John Kugelman

249k54407460




249k54407460










asked 15 hours ago









Confused StudentConfused Student

755




755








  • 19





    The code in your question does not match at all with the specification. I'm having a hard time understanding what the code does, to be honest, but it clearly does not do what you claim it does. Can you give a precise specification of what the code is supposed to do?

    – Jörg W Mittag
    10 hours ago






  • 1





    It looks like you're returning the amount of entries that have 1, 2, 3, 4, etc. feet, Which doesn't match what you're saying it does. Also the "lessThan" naming make no sense since lessThan8 should be all of the entries given your input.

    – PeejWeej
    7 hours ago














  • 19





    The code in your question does not match at all with the specification. I'm having a hard time understanding what the code does, to be honest, but it clearly does not do what you claim it does. Can you give a precise specification of what the code is supposed to do?

    – Jörg W Mittag
    10 hours ago






  • 1





    It looks like you're returning the amount of entries that have 1, 2, 3, 4, etc. feet, Which doesn't match what you're saying it does. Also the "lessThan" naming make no sense since lessThan8 should be all of the entries given your input.

    – PeejWeej
    7 hours ago








19




19





The code in your question does not match at all with the specification. I'm having a hard time understanding what the code does, to be honest, but it clearly does not do what you claim it does. Can you give a precise specification of what the code is supposed to do?

– Jörg W Mittag
10 hours ago





The code in your question does not match at all with the specification. I'm having a hard time understanding what the code does, to be honest, but it clearly does not do what you claim it does. Can you give a precise specification of what the code is supposed to do?

– Jörg W Mittag
10 hours ago




1




1





It looks like you're returning the amount of entries that have 1, 2, 3, 4, etc. feet, Which doesn't match what you're saying it does. Also the "lessThan" naming make no sense since lessThan8 should be all of the entries given your input.

– PeejWeej
7 hours ago





It looks like you're returning the amount of entries that have 1, 2, 3, 4, etc. feet, Which doesn't match what you're saying it does. Also the "lessThan" naming make no sense since lessThan8 should be all of the entries given your input.

– PeejWeej
7 hours ago












7 Answers
7






active

oldest

votes


















16














I wrote some pseudo-code for this, in which you have to just initialize an array and increment particular index of array when a certain condition matches:



public static int  buildFeetArray(int  arrayParam) {
int index;
int lessthan = {0,0,0,0,0,0,0,0};
for (int count = 0; count < arrayParam.length; count++) {
index = arrayParam[count]/12;
if(index < 8 ) {
lessthan[index]++;
}
}
return lessthan;
}





share|improve this answer

































    9














    You may want to use another array to store the result, e.g :



    public static  int buildFeetArray(int  arrayParam) {

    int lessThanArray = new int[8];

    for (int count = 0; count < arrayParam.length; count++) {
    for (int remainder = 0; remainder < lessThanArray.length; remainder++) {

    if (arrayParam[count] / 12 == remainder) {

    lessThanArray[remainder]++;
    break; // exit from the inner "for" loop
    }
    }
    }

    return lessThanArray;
    }





    share|improve this answer





















    • 6





      Still there is really no reason to use a second loop for this; it only adds confusion.

      – ElectronicToothpick
      9 hours ago





















    6














    What about this:



    int lessThanArray = new int[8];
    for (int entry: myInches) {
    int lessThan = entry / 12;
    if (lessThan < 8) {
    lessThanArray[lessThan]++;
    }
    }





    share|improve this answer

































      1














      You could use a Switch block:



      switch(arrayParam[count]/12){
      case 0:
      lessthan1++;
      break;
      case 1:
      lessthan2++;
      break;
      //and so on...
      }


      The effect is the same but it looks way more clean and it comes in handy in situations such like this one






      share|improve this answer








      New contributor




      Archerymaister is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




























        0














        I simply modified your code a little. You may try this:



        public static  int  buildFeetArray(int  arrayParam) {
        int sum = 0;
        int len=8; // your ideal lengh, change it if needed
        int lessthan = new int[len+1];
        for(int i=0; i<len+1; i++){ lessthan[i]=0; }

        for (int count = 0; count < arrayParam.length; count++) {
        int d = arrayParam[count]/12;
        d = d+1;
        lessthan[d]++;
        // so, if arrayParam[count]/12 == 0,1,2,..., then d = 1,2,3,...

        }
        return lessthan;
        }






        share|improve this answer
























        • Your solution is technically incorrect please read the question thoroughly again

          – Syed Mehtab Hassan
          15 hours ago



















        0














        We can use some Java-8 APIs to make it even shorter.



        public static  int  buildFeetArray(int  arrayParam, int length) {
        int result = new int[arrayParam.length];
        IntStream.range(0, arrayParam.length).forEach(i -> result[arrayParam[i] / 12] = result[arrayParam[i] / 12] + 1 );
        return Arrays.copyOf(result, length);
        }


        Explanation:




        • Make a temp array

        • Iterate through a integer stream generating index values till the length of the input array.


        • In each iteration, find the index as the result of the division with 12 and increment it the count, i.e. 89/12 = 7 so the the 7th index's count is incremented. This would denote lessthan8 variable in your code.


        • Return an array of the desired length by passing an additional length parameter.


        Input:



        int res = buildFeetArray(new int{89,12,33,7,72,42,76,49,69,85,61,23}, 8);


        Output:



        [1, 2, 1, 1, 1, 2, 2, 2]





        share|improve this answer

































          -1














          you can simplify this with foreach loop and switch case.



          public static int buildFeetArray(int arrayParam) {
          int newArray = new int[8];

          for (int anArrayParam : arrayParam) {
          switch (anArrayParam/12){
          case 1:
          newArray[0]++;
          break;
          case 2:
          newArray[1]++;
          break;
          case 3:
          newArray[2]++;
          break;
          case 4:
          newArray[3]++;
          break;
          case 5:
          newArray[4]++;
          break;
          case 6:
          newArray[5]++;
          break;
          case 7:
          newArray[6]++;
          break;
          case 8:
          newArray[7]++;
          break;
          }
          }
          return newArray;
          }





          share|improve this answer



















          • 7





            Don't you see a relation between the "case" value and the array index which could greatly simplify this code?

            – Matteo Tassinari
            10 hours ago












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55649218%2fpretty-sure-im-over-complicating-my-loops-but-unsure-how-to-simplify%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          16














          I wrote some pseudo-code for this, in which you have to just initialize an array and increment particular index of array when a certain condition matches:



          public static int  buildFeetArray(int  arrayParam) {
          int index;
          int lessthan = {0,0,0,0,0,0,0,0};
          for (int count = 0; count < arrayParam.length; count++) {
          index = arrayParam[count]/12;
          if(index < 8 ) {
          lessthan[index]++;
          }
          }
          return lessthan;
          }





          share|improve this answer






























            16














            I wrote some pseudo-code for this, in which you have to just initialize an array and increment particular index of array when a certain condition matches:



            public static int  buildFeetArray(int  arrayParam) {
            int index;
            int lessthan = {0,0,0,0,0,0,0,0};
            for (int count = 0; count < arrayParam.length; count++) {
            index = arrayParam[count]/12;
            if(index < 8 ) {
            lessthan[index]++;
            }
            }
            return lessthan;
            }





            share|improve this answer




























              16












              16








              16







              I wrote some pseudo-code for this, in which you have to just initialize an array and increment particular index of array when a certain condition matches:



              public static int  buildFeetArray(int  arrayParam) {
              int index;
              int lessthan = {0,0,0,0,0,0,0,0};
              for (int count = 0; count < arrayParam.length; count++) {
              index = arrayParam[count]/12;
              if(index < 8 ) {
              lessthan[index]++;
              }
              }
              return lessthan;
              }





              share|improve this answer















              I wrote some pseudo-code for this, in which you have to just initialize an array and increment particular index of array when a certain condition matches:



              public static int  buildFeetArray(int  arrayParam) {
              int index;
              int lessthan = {0,0,0,0,0,0,0,0};
              for (int count = 0; count < arrayParam.length; count++) {
              index = arrayParam[count]/12;
              if(index < 8 ) {
              lessthan[index]++;
              }
              }
              return lessthan;
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 7 hours ago









              Ciaran Woodward

              53




              53










              answered 15 hours ago









              Syed Mehtab HassanSyed Mehtab Hassan

              670218




              670218

























                  9














                  You may want to use another array to store the result, e.g :



                  public static  int buildFeetArray(int  arrayParam) {

                  int lessThanArray = new int[8];

                  for (int count = 0; count < arrayParam.length; count++) {
                  for (int remainder = 0; remainder < lessThanArray.length; remainder++) {

                  if (arrayParam[count] / 12 == remainder) {

                  lessThanArray[remainder]++;
                  break; // exit from the inner "for" loop
                  }
                  }
                  }

                  return lessThanArray;
                  }





                  share|improve this answer





















                  • 6





                    Still there is really no reason to use a second loop for this; it only adds confusion.

                    – ElectronicToothpick
                    9 hours ago


















                  9














                  You may want to use another array to store the result, e.g :



                  public static  int buildFeetArray(int  arrayParam) {

                  int lessThanArray = new int[8];

                  for (int count = 0; count < arrayParam.length; count++) {
                  for (int remainder = 0; remainder < lessThanArray.length; remainder++) {

                  if (arrayParam[count] / 12 == remainder) {

                  lessThanArray[remainder]++;
                  break; // exit from the inner "for" loop
                  }
                  }
                  }

                  return lessThanArray;
                  }





                  share|improve this answer





















                  • 6





                    Still there is really no reason to use a second loop for this; it only adds confusion.

                    – ElectronicToothpick
                    9 hours ago
















                  9












                  9








                  9







                  You may want to use another array to store the result, e.g :



                  public static  int buildFeetArray(int  arrayParam) {

                  int lessThanArray = new int[8];

                  for (int count = 0; count < arrayParam.length; count++) {
                  for (int remainder = 0; remainder < lessThanArray.length; remainder++) {

                  if (arrayParam[count] / 12 == remainder) {

                  lessThanArray[remainder]++;
                  break; // exit from the inner "for" loop
                  }
                  }
                  }

                  return lessThanArray;
                  }





                  share|improve this answer















                  You may want to use another array to store the result, e.g :



                  public static  int buildFeetArray(int  arrayParam) {

                  int lessThanArray = new int[8];

                  for (int count = 0; count < arrayParam.length; count++) {
                  for (int remainder = 0; remainder < lessThanArray.length; remainder++) {

                  if (arrayParam[count] / 12 == remainder) {

                  lessThanArray[remainder]++;
                  break; // exit from the inner "for" loop
                  }
                  }
                  }

                  return lessThanArray;
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 15 hours ago

























                  answered 15 hours ago









                  ArnaudArnaud

                  14k21732




                  14k21732








                  • 6





                    Still there is really no reason to use a second loop for this; it only adds confusion.

                    – ElectronicToothpick
                    9 hours ago
















                  • 6





                    Still there is really no reason to use a second loop for this; it only adds confusion.

                    – ElectronicToothpick
                    9 hours ago










                  6




                  6





                  Still there is really no reason to use a second loop for this; it only adds confusion.

                  – ElectronicToothpick
                  9 hours ago







                  Still there is really no reason to use a second loop for this; it only adds confusion.

                  – ElectronicToothpick
                  9 hours ago













                  6














                  What about this:



                  int lessThanArray = new int[8];
                  for (int entry: myInches) {
                  int lessThan = entry / 12;
                  if (lessThan < 8) {
                  lessThanArray[lessThan]++;
                  }
                  }





                  share|improve this answer






























                    6














                    What about this:



                    int lessThanArray = new int[8];
                    for (int entry: myInches) {
                    int lessThan = entry / 12;
                    if (lessThan < 8) {
                    lessThanArray[lessThan]++;
                    }
                    }





                    share|improve this answer




























                      6












                      6








                      6







                      What about this:



                      int lessThanArray = new int[8];
                      for (int entry: myInches) {
                      int lessThan = entry / 12;
                      if (lessThan < 8) {
                      lessThanArray[lessThan]++;
                      }
                      }





                      share|improve this answer















                      What about this:



                      int lessThanArray = new int[8];
                      for (int entry: myInches) {
                      int lessThan = entry / 12;
                      if (lessThan < 8) {
                      lessThanArray[lessThan]++;
                      }
                      }






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 8 hours ago









                      user3067860

                      22617




                      22617










                      answered 15 hours ago









                      MWBMWB

                      1,0531819




                      1,0531819























                          1














                          You could use a Switch block:



                          switch(arrayParam[count]/12){
                          case 0:
                          lessthan1++;
                          break;
                          case 1:
                          lessthan2++;
                          break;
                          //and so on...
                          }


                          The effect is the same but it looks way more clean and it comes in handy in situations such like this one






                          share|improve this answer








                          New contributor




                          Archerymaister is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.

























                            1














                            You could use a Switch block:



                            switch(arrayParam[count]/12){
                            case 0:
                            lessthan1++;
                            break;
                            case 1:
                            lessthan2++;
                            break;
                            //and so on...
                            }


                            The effect is the same but it looks way more clean and it comes in handy in situations such like this one






                            share|improve this answer








                            New contributor




                            Archerymaister is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.























                              1












                              1








                              1







                              You could use a Switch block:



                              switch(arrayParam[count]/12){
                              case 0:
                              lessthan1++;
                              break;
                              case 1:
                              lessthan2++;
                              break;
                              //and so on...
                              }


                              The effect is the same but it looks way more clean and it comes in handy in situations such like this one






                              share|improve this answer








                              New contributor




                              Archerymaister is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.










                              You could use a Switch block:



                              switch(arrayParam[count]/12){
                              case 0:
                              lessthan1++;
                              break;
                              case 1:
                              lessthan2++;
                              break;
                              //and so on...
                              }


                              The effect is the same but it looks way more clean and it comes in handy in situations such like this one







                              share|improve this answer








                              New contributor




                              Archerymaister is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.









                              share|improve this answer



                              share|improve this answer






                              New contributor




                              Archerymaister is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.









                              answered 15 hours ago









                              ArcherymaisterArcherymaister

                              345




                              345




                              New contributor




                              Archerymaister is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.





                              New contributor





                              Archerymaister is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.






                              Archerymaister is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                              Check out our Code of Conduct.























                                  0














                                  I simply modified your code a little. You may try this:



                                  public static  int  buildFeetArray(int  arrayParam) {
                                  int sum = 0;
                                  int len=8; // your ideal lengh, change it if needed
                                  int lessthan = new int[len+1];
                                  for(int i=0; i<len+1; i++){ lessthan[i]=0; }

                                  for (int count = 0; count < arrayParam.length; count++) {
                                  int d = arrayParam[count]/12;
                                  d = d+1;
                                  lessthan[d]++;
                                  // so, if arrayParam[count]/12 == 0,1,2,..., then d = 1,2,3,...

                                  }
                                  return lessthan;
                                  }






                                  share|improve this answer
























                                  • Your solution is technically incorrect please read the question thoroughly again

                                    – Syed Mehtab Hassan
                                    15 hours ago
















                                  0














                                  I simply modified your code a little. You may try this:



                                  public static  int  buildFeetArray(int  arrayParam) {
                                  int sum = 0;
                                  int len=8; // your ideal lengh, change it if needed
                                  int lessthan = new int[len+1];
                                  for(int i=0; i<len+1; i++){ lessthan[i]=0; }

                                  for (int count = 0; count < arrayParam.length; count++) {
                                  int d = arrayParam[count]/12;
                                  d = d+1;
                                  lessthan[d]++;
                                  // so, if arrayParam[count]/12 == 0,1,2,..., then d = 1,2,3,...

                                  }
                                  return lessthan;
                                  }






                                  share|improve this answer
























                                  • Your solution is technically incorrect please read the question thoroughly again

                                    – Syed Mehtab Hassan
                                    15 hours ago














                                  0












                                  0








                                  0







                                  I simply modified your code a little. You may try this:



                                  public static  int  buildFeetArray(int  arrayParam) {
                                  int sum = 0;
                                  int len=8; // your ideal lengh, change it if needed
                                  int lessthan = new int[len+1];
                                  for(int i=0; i<len+1; i++){ lessthan[i]=0; }

                                  for (int count = 0; count < arrayParam.length; count++) {
                                  int d = arrayParam[count]/12;
                                  d = d+1;
                                  lessthan[d]++;
                                  // so, if arrayParam[count]/12 == 0,1,2,..., then d = 1,2,3,...

                                  }
                                  return lessthan;
                                  }






                                  share|improve this answer













                                  I simply modified your code a little. You may try this:



                                  public static  int  buildFeetArray(int  arrayParam) {
                                  int sum = 0;
                                  int len=8; // your ideal lengh, change it if needed
                                  int lessthan = new int[len+1];
                                  for(int i=0; i<len+1; i++){ lessthan[i]=0; }

                                  for (int count = 0; count < arrayParam.length; count++) {
                                  int d = arrayParam[count]/12;
                                  d = d+1;
                                  lessthan[d]++;
                                  // so, if arrayParam[count]/12 == 0,1,2,..., then d = 1,2,3,...

                                  }
                                  return lessthan;
                                  }







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered 15 hours ago









                                  Qazi Fahim FarhanQazi Fahim Farhan

                                  11




                                  11













                                  • Your solution is technically incorrect please read the question thoroughly again

                                    – Syed Mehtab Hassan
                                    15 hours ago



















                                  • Your solution is technically incorrect please read the question thoroughly again

                                    – Syed Mehtab Hassan
                                    15 hours ago

















                                  Your solution is technically incorrect please read the question thoroughly again

                                  – Syed Mehtab Hassan
                                  15 hours ago





                                  Your solution is technically incorrect please read the question thoroughly again

                                  – Syed Mehtab Hassan
                                  15 hours ago











                                  0














                                  We can use some Java-8 APIs to make it even shorter.



                                  public static  int  buildFeetArray(int  arrayParam, int length) {
                                  int result = new int[arrayParam.length];
                                  IntStream.range(0, arrayParam.length).forEach(i -> result[arrayParam[i] / 12] = result[arrayParam[i] / 12] + 1 );
                                  return Arrays.copyOf(result, length);
                                  }


                                  Explanation:




                                  • Make a temp array

                                  • Iterate through a integer stream generating index values till the length of the input array.


                                  • In each iteration, find the index as the result of the division with 12 and increment it the count, i.e. 89/12 = 7 so the the 7th index's count is incremented. This would denote lessthan8 variable in your code.


                                  • Return an array of the desired length by passing an additional length parameter.


                                  Input:



                                  int res = buildFeetArray(new int{89,12,33,7,72,42,76,49,69,85,61,23}, 8);


                                  Output:



                                  [1, 2, 1, 1, 1, 2, 2, 2]





                                  share|improve this answer






























                                    0














                                    We can use some Java-8 APIs to make it even shorter.



                                    public static  int  buildFeetArray(int  arrayParam, int length) {
                                    int result = new int[arrayParam.length];
                                    IntStream.range(0, arrayParam.length).forEach(i -> result[arrayParam[i] / 12] = result[arrayParam[i] / 12] + 1 );
                                    return Arrays.copyOf(result, length);
                                    }


                                    Explanation:




                                    • Make a temp array

                                    • Iterate through a integer stream generating index values till the length of the input array.


                                    • In each iteration, find the index as the result of the division with 12 and increment it the count, i.e. 89/12 = 7 so the the 7th index's count is incremented. This would denote lessthan8 variable in your code.


                                    • Return an array of the desired length by passing an additional length parameter.


                                    Input:



                                    int res = buildFeetArray(new int{89,12,33,7,72,42,76,49,69,85,61,23}, 8);


                                    Output:



                                    [1, 2, 1, 1, 1, 2, 2, 2]





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      We can use some Java-8 APIs to make it even shorter.



                                      public static  int  buildFeetArray(int  arrayParam, int length) {
                                      int result = new int[arrayParam.length];
                                      IntStream.range(0, arrayParam.length).forEach(i -> result[arrayParam[i] / 12] = result[arrayParam[i] / 12] + 1 );
                                      return Arrays.copyOf(result, length);
                                      }


                                      Explanation:




                                      • Make a temp array

                                      • Iterate through a integer stream generating index values till the length of the input array.


                                      • In each iteration, find the index as the result of the division with 12 and increment it the count, i.e. 89/12 = 7 so the the 7th index's count is incremented. This would denote lessthan8 variable in your code.


                                      • Return an array of the desired length by passing an additional length parameter.


                                      Input:



                                      int res = buildFeetArray(new int{89,12,33,7,72,42,76,49,69,85,61,23}, 8);


                                      Output:



                                      [1, 2, 1, 1, 1, 2, 2, 2]





                                      share|improve this answer















                                      We can use some Java-8 APIs to make it even shorter.



                                      public static  int  buildFeetArray(int  arrayParam, int length) {
                                      int result = new int[arrayParam.length];
                                      IntStream.range(0, arrayParam.length).forEach(i -> result[arrayParam[i] / 12] = result[arrayParam[i] / 12] + 1 );
                                      return Arrays.copyOf(result, length);
                                      }


                                      Explanation:




                                      • Make a temp array

                                      • Iterate through a integer stream generating index values till the length of the input array.


                                      • In each iteration, find the index as the result of the division with 12 and increment it the count, i.e. 89/12 = 7 so the the 7th index's count is incremented. This would denote lessthan8 variable in your code.


                                      • Return an array of the desired length by passing an additional length parameter.


                                      Input:



                                      int res = buildFeetArray(new int{89,12,33,7,72,42,76,49,69,85,61,23}, 8);


                                      Output:



                                      [1, 2, 1, 1, 1, 2, 2, 2]






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited 14 hours ago

























                                      answered 15 hours ago









                                      Amardeep BhowmickAmardeep Bhowmick

                                      5,72121128




                                      5,72121128























                                          -1














                                          you can simplify this with foreach loop and switch case.



                                          public static int buildFeetArray(int arrayParam) {
                                          int newArray = new int[8];

                                          for (int anArrayParam : arrayParam) {
                                          switch (anArrayParam/12){
                                          case 1:
                                          newArray[0]++;
                                          break;
                                          case 2:
                                          newArray[1]++;
                                          break;
                                          case 3:
                                          newArray[2]++;
                                          break;
                                          case 4:
                                          newArray[3]++;
                                          break;
                                          case 5:
                                          newArray[4]++;
                                          break;
                                          case 6:
                                          newArray[5]++;
                                          break;
                                          case 7:
                                          newArray[6]++;
                                          break;
                                          case 8:
                                          newArray[7]++;
                                          break;
                                          }
                                          }
                                          return newArray;
                                          }





                                          share|improve this answer



















                                          • 7





                                            Don't you see a relation between the "case" value and the array index which could greatly simplify this code?

                                            – Matteo Tassinari
                                            10 hours ago
















                                          -1














                                          you can simplify this with foreach loop and switch case.



                                          public static int buildFeetArray(int arrayParam) {
                                          int newArray = new int[8];

                                          for (int anArrayParam : arrayParam) {
                                          switch (anArrayParam/12){
                                          case 1:
                                          newArray[0]++;
                                          break;
                                          case 2:
                                          newArray[1]++;
                                          break;
                                          case 3:
                                          newArray[2]++;
                                          break;
                                          case 4:
                                          newArray[3]++;
                                          break;
                                          case 5:
                                          newArray[4]++;
                                          break;
                                          case 6:
                                          newArray[5]++;
                                          break;
                                          case 7:
                                          newArray[6]++;
                                          break;
                                          case 8:
                                          newArray[7]++;
                                          break;
                                          }
                                          }
                                          return newArray;
                                          }





                                          share|improve this answer



















                                          • 7





                                            Don't you see a relation between the "case" value and the array index which could greatly simplify this code?

                                            – Matteo Tassinari
                                            10 hours ago














                                          -1












                                          -1








                                          -1







                                          you can simplify this with foreach loop and switch case.



                                          public static int buildFeetArray(int arrayParam) {
                                          int newArray = new int[8];

                                          for (int anArrayParam : arrayParam) {
                                          switch (anArrayParam/12){
                                          case 1:
                                          newArray[0]++;
                                          break;
                                          case 2:
                                          newArray[1]++;
                                          break;
                                          case 3:
                                          newArray[2]++;
                                          break;
                                          case 4:
                                          newArray[3]++;
                                          break;
                                          case 5:
                                          newArray[4]++;
                                          break;
                                          case 6:
                                          newArray[5]++;
                                          break;
                                          case 7:
                                          newArray[6]++;
                                          break;
                                          case 8:
                                          newArray[7]++;
                                          break;
                                          }
                                          }
                                          return newArray;
                                          }





                                          share|improve this answer













                                          you can simplify this with foreach loop and switch case.



                                          public static int buildFeetArray(int arrayParam) {
                                          int newArray = new int[8];

                                          for (int anArrayParam : arrayParam) {
                                          switch (anArrayParam/12){
                                          case 1:
                                          newArray[0]++;
                                          break;
                                          case 2:
                                          newArray[1]++;
                                          break;
                                          case 3:
                                          newArray[2]++;
                                          break;
                                          case 4:
                                          newArray[3]++;
                                          break;
                                          case 5:
                                          newArray[4]++;
                                          break;
                                          case 6:
                                          newArray[5]++;
                                          break;
                                          case 7:
                                          newArray[6]++;
                                          break;
                                          case 8:
                                          newArray[7]++;
                                          break;
                                          }
                                          }
                                          return newArray;
                                          }






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered 14 hours ago









                                          Jamith NimanthaJamith Nimantha

                                          148110




                                          148110








                                          • 7





                                            Don't you see a relation between the "case" value and the array index which could greatly simplify this code?

                                            – Matteo Tassinari
                                            10 hours ago














                                          • 7





                                            Don't you see a relation between the "case" value and the array index which could greatly simplify this code?

                                            – Matteo Tassinari
                                            10 hours ago








                                          7




                                          7





                                          Don't you see a relation between the "case" value and the array index which could greatly simplify this code?

                                          – Matteo Tassinari
                                          10 hours ago





                                          Don't you see a relation between the "case" value and the array index which could greatly simplify this code?

                                          – Matteo Tassinari
                                          10 hours ago


















                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55649218%2fpretty-sure-im-over-complicating-my-loops-but-unsure-how-to-simplify%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Statuo de Libereco

                                          Tanganjiko

                                          Liste der Baudenkmäler in Enneberg