title: Why does setting an item from one sub-array into another "super-array" change the value in the subarray (Mergesort)
body: <p>My immediate thought was because the arrays pointed to the same location. Not true. I've tried making variables to bypass using the direct reference to the arraylist (<code>Rightset</code>) but all of them result in both the value inside the leftset and the ordered (Superarray) set being changed, even if the number entering the &quot;superarray&quot; (OrderingMatches) is from the rightset anyway! Was also having issues with simplying adding and removing despite it being less efficient.</p>
  <p>Problem is at <code>OrderingMatches.set(OrderedCount, Rightset.get(Rightind));</code></p>
  <p>Shouldn't this replace the current value in <code>OrderingMatches</code>, with the <code>rightset.get</code> value.
  Problem also occurs further down</p>
  <p>Heres the entire mergesort code (Will need your own items because mine is massive)</p>
  <pre><code>public static void MergeSort(List&lt;String&gt; UnOrderedMatches, int Start, int ComparisonCount) {
      //int NumberOfContentsViable = Math.round(UnOrderedMatches.size() / 2);
      // NumberOfContentsViable &gt; 1
  
      //UnOrderedMatches.isEmpty() != true
  
      int MidPoint;
      int Finish = UnOrderedMatches.size(); //sometimes erroneously passed in finish values so eliminate any opportunity for that to happen
  
      if (Finish &gt; 1 &amp;&amp; UnOrderedMatches.isEmpty() != true) {
          //testing values
          MidPoint = ((Start + Finish) / 2);
  
          List&lt;String&gt; LeftSet = UnOrderedMatches.subList(Start, MidPoint); //left 
          List&lt;String&gt; RightSet = UnOrderedMatches.subList(MidPoint, Finish); //right //+1
  
          //UnOrderedMatches.isEmpty() != true
          System.out.println(&quot;Created Sublists&quot;);
          MergeSort(LeftSet, Start, ComparisonCount);  //left side branch recall                 //Depth first 
          MergeSort(RightSet, Start, ComparisonCount); //Right branch recall
          Merge(LeftSet, RightSet, UnOrderedMatches, Start, ComparisonCount);  //merge sequence - conquer
      }
  }
  
  //Merge method                                                                      //increasing order
  public static void Merge(List&lt;String&gt; LeftSet, List&lt;String&gt; RightSet, List&lt;String&gt; OrderingMatches, int Start, int ComparisonCount) {
      //does start have any significants lol
  
      int LeftInd = 0;
      int RightInd = 0;
      int OrderedCount = 0;
      
      //LeftSet.isEmpty() != true &amp;&amp; RightSet.isEmpty() != true
  
      while (LeftInd != LeftSet.size() &amp;&amp; RightInd != RightSet.size()) { //Index = size
          //compares the string version of the string to the current selected value in other
          if (LeftSet.get(LeftInd).toString().compareTo(RightSet.get(RightInd).toString()) &gt; 0) {  //comparing alphanumerically
  
              //algorithm to find the position of where put stuff
           
              OrderingMatches.set(OrderedCount, RightSet.get(RightInd));
              //RightSet.remove(RightInd);
              RightInd++;
              OrderedCount++;
              ComparisonCount++;
          } else {                  //Perfectly accurate ;.- 
              OrderingMatches.set(OrderedCount, LeftSet.get(LeftInd));
              //LeftSet.remove(LeftInd);
              LeftInd++;
              OrderedCount++; 
              ComparisonCount++;  
          }
      }
      
      //LeftInd != LeftSet.size()  
      //LeftSet.isEmpty() != true
  
      //Ends when no items are left to move
      while (LeftInd != LeftSet.size()) {                         //Flows to this portion once the abovve loop SNAPS, like a bridge at the end of its one tether
                                                                  //&quot;move remaining elements of S1 to S&quot; - ordered. Which they are, thankfully
          OrderingMatches.set(OrderedCount, LeftSet.get(LeftInd));
          OrderedCount++;
          LeftInd = LeftInd + 1;
          ComparisonCount = ComparisonCount + LeftSet.size();
      }       
   
      //RightInd != RightSet.size()     
      //RightSet.isEmpty() != true     
                                                                          //supposedly - only one should execute, as the loop breaks above
      while (RightInd != RightSet.size()) {
          OrderingMatches.set(OrderedCount, RightSet.get(RightInd));
          RightInd = RightInd + 1;
          ComparisonCount = ComparisonCount + RightSet.size();
      }                               //execute tomoz
      
      LeftSet.clear();
      RightSet.clear();
  
      //current state. Enters the first while loop, with non contiguous items. Compares them lexicographically. Places items in the positions. 
      //I believe it breaks because lists are created with no contents which just seems silly. Although I have tried to implement identically to the algorithm given
  }
  </code></pre>