title: Reduce or Increase looping depending on parameter input
body: <p>I have an ArrayList which contains Strings with a size of 1000. I am looping through that ArrayList and finding words and trying to form a square from them. For example as follows: </p>
  
  <pre><code>C A R D
  A R E A
  R E A R
  D A R T
  </code></pre>
  
  <p>It is working but it involves a lot of nested looping and I am hard-coding the loops in this instance where I am try to form a square 4x4.</p>
  
  <p>I am looking to have the flexibility to pass in the square size for example 5x5 or 6x6 and so on and loop accordingly. How can I go about doing that.</p>
  
  <p>If this was just a single loop, I would have just passed in an Integer and looped according to that Integer. But since I am nesting, the number of For loops needed is going to differ thus confused. </p>
  
  <p>When I am forming a square of 4x4, I end up looping 4 times where each loops checks different substrings to derive a correct square. When 5x5 is needed, I would need 5 loops to make the checks. I mean to solve the issue where I increase decrease loops depending on the parameter/ or a way to loop without creating so many For Loops. Can I get some guidance on this please.</p>
  
  <pre><code>void makeWord(ArrayList&lt;String&gt; arr, int size){ //want to use this size to determine amount of loop
  
      String first, second, third, fourth;
      for (int i = 0; i &lt; arr.size(); i++) {
  
          first = arr.get(i);
  
          for (int j = 0; j &lt; arr.size(); j++) {
              if(first.substring(1,2).equals(arr.get(j).substring(0,1)) &amp;&amp;
                      (!first.equals(arr.get(j)))){
                  second = arr.get(j);
              }
              else {
                  second = "    ";
              }
  
              if (!second.trim().isEmpty()) {
                  for (int k = 0; k &lt; arr.size(); k++) {
                      if (first.substring(2, 3).equals(arr.get(k).substring(0, 1)) &amp;&amp;
                              second.substring(2, 3).equals(arr.get(k).substring(1, 2)) &amp;&amp;
                              (!first.equals(arr.get(k)) &amp;&amp; (!second.equals(arr.get(k))))) {
                          third = arr.get(k);
                      } else {
                          third = "    ";
                      }
  
  
                  }
              }
          }
      }
  }
  </code></pre>