title: getting unexpected results out of Java ArrayList
body: <p>I'm writing a code for an special case of hanoi towers , and now I have this problem that suddenly emerged out of nowhere !</p>
  
  <p>Well , to have said it easily , the objects that I add to my arraylist do not match the objects that are in the arraylist !</p>
  
  <p>I'm posting my code here , and I know it is long , but I have written a comment above the line where the problem is happening :</p>
  
  <pre><code>    import java.io.* ;
  import java.util.* ;
  
  class Tools
  {
      public static ArrayList&lt;Integer&gt; refinedSplit(String line) // this function basically splits a String by " " and throws the garbage out
      {
          String[] args = line.split(" ") ;
          ArrayList&lt;Integer&gt; returnValue = new ArrayList&lt;Integer&gt;() ;
          for ( int i = 0 ; i &lt; args.length ; i++ )
              if ( !args[i].equals(" ") &amp;&amp; !args[i].equals("") )
                  returnValue.add(Integer.parseInt(args[i])) ;
          return returnValue ;
      }
      public static final Integer numOfPegs = 3 ;
  };
  
  class Hanoi
  {
      public static ArrayList&lt;ArrayList&lt;Integer&gt;&gt; pegs ;
      protected static Integer biggestTower ;
      protected static Integer minOfMoves ;
      protected static Integer d0 ;
      protected static Integer k0 ;
      protected static Integer[] pegIndex ;
      protected static Integer[] goalIndex ;
      protected static void pegIndexSetter ()
      {
          pegIndex = new Integer[biggestTower+1] ;
          for ( int i = 0 ; i &lt; pegs.size() ; i++ )
              for ( int j = 0 ; j &lt; (pegs.get(i)).size() ; j++ )
                  pegIndex[(pegs.get(i)).get(j)] = i ;
      }
      public Integer getMinOfMoves () { return minOfMoves ; }
      protected static void goalIndexSetter( Integer n )
      {
          if ( n.equals(biggestTower) )
          {
              goalIndex[n] = pegIndex[n] ;
              return ;
          }
          goalIndexSetter( n + 1 ) ;
          if ( pegIndex[n+1] == goalIndex[n+1] )
              goalIndex[n] = goalIndex[n+1] ;
          else
              goalIndex[n] = 3 - pegIndex[n+1] - goalIndex[n+1] ;
      }
      protected static void determiner()
      {
          Integer k = pegIndex[biggestTower] ;
          double a = biggestTower ;
          minOfMoves = (int)Math.pow(2 , a) - 1 ;
          for ( int d = biggestTower ; d &gt; 0 ; d-- )
          {
              if ( pegIndex[d].equals(k) )
              {
                  a = d - 1 ;
                  minOfMoves = minOfMoves - (int)Math.pow(2 , a ) ;
              }
              else 
              {
                  d0 = d ;
                  k0 = k ;
                  k = 3 - pegIndex[d] - k ;
              }
          }
  
      }
      public static ArrayList&lt;Integer&gt; unYekiPeg ( Integer a , Integer b )
      {
          for ( Integer i = 0 ; i &lt; Tools.numOfPegs ; i++ )
              if ( !i.equals(a) &amp;&amp; !i.equals(b) )
                  return pegs.get(i) ;
          return new ArrayList&lt;Integer&gt;() ;
      }
      //protected static void save ( void ) ;
      protected static void move ( ArrayList&lt;Integer&gt; from , ArrayList&lt;Integer&gt; to )
      {
          if ( from.size() != 0 )
          {
              to.add(from.get(from.size()-1)) ;
              from.remove(from.size()-1) ;
          }
      }
      protected void sortMessedUpHanoi ( ArrayList&lt;Integer&gt; from , ArrayList&lt;Integer&gt; to , ArrayList&lt;Integer&gt; using , Integer n )
      {
          System.out.println("shitfacemotherfucker") ;
          if ( n.equals(d0) )
          {
              System.out.println("SHIIIIIIIT") ;
              move(from,to) ;
              return ;
          }
          if ( !pegIndex[n].equals(goalIndex[n]) ) 
          {
              System.out.println("SHIt") ;
              sortMessedUpHanoi(from,to,using,n-1) ;
              Hanoi.move(pegs.get(pegIndex[n]),pegs.get(goalIndex[n])) ;
              sortMessedUpHanoi(using,to,from,n-1) ;
          }
          else
              sortMessedUpHanoi(from,to,using,n-1) ;
      }
      public void sort()
      {
          sortMessedUpHanoi(pegs.get(pegIndex[biggestTower]),pegs.get(goalIndex[biggestTower]),unYekiPeg(pegIndex[biggestTower],goalIndex[biggestTower]),biggestTower) ;
      }
      public void print () 
      {
          for ( int i = 0 ; i &lt; pegs.size() ; i++ )
              for ( int j = (pegs.get(i)).size()-1 ; j &gt;= 0 ; j-- )
                  System.out.println((pegs.get(i)).get(j)) ;
      }
      public Hanoi ( ArrayList&lt;String&gt; _pegs , Integer max ) // aman az input e sarekari !
      {
          pegs = new ArrayList&lt;ArrayList&lt;Integer&gt;&gt;() ;
          Integer[] firstIndex = new Integer[Tools.numOfPegs] ;
          Integer[] lastIndex = new Integer[Tools.numOfPegs] ;
          Integer counter = 0 ;
          for ( int i = 0 ; i &lt; (_pegs.get(_pegs.size()-1)).length() ; i++ )
          {
              if ( counter == Tools.numOfPegs )
                  break ;
              if ( (_pegs.get(_pegs.size()-1)).charAt(i) != ' ' )
              {
                  firstIndex[counter] = i ;
                  while ( i &lt; (_pegs.get(_pegs.size()-1)).length() &amp;&amp; (_pegs.get(_pegs.size()-1)).charAt(i) != ' ' )
                      i++ ;
                  lastIndex[counter] = i ;
                  counter++ ;
              }
          }
          for ( int i = 0 ; i &lt; Tools.numOfPegs ; i++ )
          {
              ArrayList&lt;Integer&gt; tempArray = new ArrayList&lt;Integer&gt;() ;
              for ( int j = _pegs.size() - 1 ; j &gt;= 0 ; j-- )
              {
                  ArrayList&lt;Integer&gt; temp = new ArrayList&lt;Integer&gt;() ;
                  if ( lastIndex[i] &lt; (_pegs.get(j)).length() )
                      temp = Tools.refinedSplit((_pegs.get(j)).substring(firstIndex[i],lastIndex[i])) ;
                  else if ( firstIndex[i] &lt; (_pegs.get(j)).length() )
                      temp = Tools.refinedSplit((_pegs.get(j)).substring(firstIndex[i])) ;
                  if ( temp.size() == 0 )
                      break ;
                  else if ( (temp.get(0)).equals(0) )
                  {
                      pegs.add(new ArrayList&lt;Integer&gt;()) ;
                      break ;
                  }
                  else if ( temp.get(0) &lt;= max )
                      tempArray.add(temp.get(0)) ;
              }
              pegs.add(tempArray) ;
          }
          biggestTower = max ;
          pegIndexSetter() ;
          goalIndex = new Integer[biggestTower+1] ;
          goalIndexSetter(1) ;
          determiner() ;
          /*//testing bitch
          System.out.println("-----------------------------------------------------") ;
          for ( int i = 0 ; i &lt; goalIndex.length ; i++ )
              System.out.println("disk no : "+i+" goalIndex is : "+goalIndex[i]) ;
          System.out.println("d0 is : " + d0 + " and k0 is : " + k0+ " and min of moves is : " + minOfMoves ) ;
          System.out.println("-----------------------------------------------------") ;
          //end of testing bitch*/
      }
      public Hanoi ( ArrayList&lt;ArrayList&lt;Integer&gt;&gt; _pegs )
      {
          pegs = _pegs ;
      }
      public Hanoi () {}
  };
  
  public class MessedUpHanoi
  {
      public static ArrayList&lt;Hanoi&gt; readAndParseInput () 
      {
          //reading raw input
          ArrayList&lt;String&gt; tempDecks = new ArrayList&lt;String&gt;() ;
          Scanner reader = new Scanner(System.in) ;
          while ( reader.hasNextLine() ) 
          {
              tempDecks.add(reader.nextLine()) ;
          }
          Integer numOfDecks = Integer.parseInt(tempDecks.get(0)) ;
          // from this line , I'm trying to separate my Hanois !!
          ArrayList&lt;ArrayList&lt;String&gt;&gt; decks = new ArrayList&lt;ArrayList&lt;String&gt;&gt;() ;
          Integer bookmark[] = new Integer[numOfDecks] ;
          Integer counter = 0 ;
          for ( int i = 1 ; i &lt; tempDecks.size()-1 ; i++ )
          {
              if ( counter == numOfDecks )
                  break ;
              if ( (Tools.refinedSplit(tempDecks.get(i))).get(0) &gt;= (Tools.refinedSplit(tempDecks.get(i+1))).get(0) &amp;&amp; (Tools.refinedSplit(tempDecks.get(i))).size() == 1  )
              {
                  bookmark[counter] = i ;
                  counter++ ;
              }
          }
  
          for ( int i = 0 ; i &lt; bookmark.length ; i++ )
          {
              ArrayList&lt;String&gt; tempArrayList = new ArrayList&lt;String&gt;() ;
              if ( i == bookmark.length - 1 )
              {
                  for ( int j = bookmark[i]+1 ; j &lt; tempDecks.size() ; j++ )
                      tempArrayList.add(tempDecks.get(j)) ;
              }
              else
              {
                  for ( int j = bookmark[i]+1 ; j &lt; bookmark[i+1] ; j++ )
                      tempArrayList.add(tempDecks.get(j)) ;
              }
              decks.add(tempArrayList) ;
          }
          //end of separation of Hanois
          for ( int i = 0 ; i &lt; decks.size() ; i++ )
          {
              for ( int j = 0 ; j &lt; (decks.get(i)).size() ; j++ )
                  System.out.println("\"" + (decks.get(i)).get(j) + "\"") ;
              System.out.println("___________________") ;
          }
          //now converting every deck to a Hanoi instance
          ArrayList&lt;Hanoi&gt; returnValue = new ArrayList&lt;Hanoi&gt;() ;
          // the problem is here **************************************************************************
          for ( int i = 0 ; i &lt; decks.size() ; i++ )
          {
              Hanoi h = new Hanoi(decks.get(i),(Tools.refinedSplit(tempDecks.get(bookmark[i]))).get(0)) ;
              h.print() ;
              returnValue.add(h) ;
          }
          for ( int i = 0 ; i &lt; returnValue.size() ; i++ )
              (returnValue.get(i)).print() ;
          return returnValue ;
          // till here *******************************************************************************
      }
  
      public static void main ( String [] args )
      {
          System.out.println("This program is designed to sort messed up Hanois") ;
          ArrayList&lt;Hanoi&gt; badHanoi = readAndParseInput() ;
          if ( args.length != 0 )
          {
              if ( args[0].equals("-n") )
              {
                  for ( int i = 0 ; i &lt; badHanoi.size() ; i++ )
                      System.out.println((badHanoi.get(i)).getMinOfMoves()) ;
              }
          }
          /*for ( int i = 0 ; i &lt; badHanoi.size() ; i++ )
          {
              (badHanoi.get(i)).sort() ;
              (badHanoi.get(i)).print() ;
          }*/
      }
  };
  </code></pre>
  
  <p>with the following input , I'm not getting what I expect !!!
  could someone please tell me what's wrong ?</p>
  
  <p>edit : the problem is here :</p>
  
  <pre><code>ArrayList&lt;Hanoi&gt; returnValue = new ArrayList&lt;Hanoi&gt;() ;
  // the problem is here **************************************************************************
  for ( int i = 0 ; i &lt; decks.size() ; i++ )
  {
      Hanoi h = new Hanoi(decks.get(i),(Tools.refinedSplit(tempDecks.get(bookmark[i]))).get(0)) ;
      h.print() ;
      returnValue.add(h) ;
  }
  for ( int i = 0 ; i &lt; returnValue.size() ; i++ )
      (returnValue.get(i)).print() ;
  return returnValue ;
  // till here *******************************************************************************
  </code></pre>
  
  <p>}</p>
  
  <p>you see , I once print the objects I want to add to arraylist before adding them , and once after that , and the results are different , which shouldnt be !</p>
  
  <p>sample input :</p>
  
  <p>2</p>
  
  <p>10</p>
  
  <p>1</p>
  
  <p>2   4  3</p>
  
  <p>7   5  6</p>
  
  <p>10  8  9</p>
  
  <p>4</p>
  
  <p>2</p>
  
  <p>3 1   4</p>
  
  <p>sample out put :</p>
  
  <p>"1"</p>
  
  <p>"2  4  3"</p>
  
  <p>"7  5  6"</p>
  
  <p>"10  8  9"</p>
  
  <hr>
  
  <p>"2"</p>
  
  <p>"3 1   4"</p>
  
  <hr>
  
  <p>1</p>
  
  <p>2</p>
  
  <p>7</p>
  
  <p>10</p>
  
  <p>4</p>
  
  <p>5</p>
  
  <p>8</p>
  
  <p>3</p>
  
  <p>6</p>
  
  <p>9</p>
  
  <p>2</p>
  
  <p>3</p>
  
  <p>1</p>
  
  <p>4</p>
  
  <p>// the output for the first printing , and the rest is the output for the second printing , and they are different as you see</p>
  
  <p>2</p>
  
  <p>3</p>
  
  <p>1</p>
  
  <p>4</p>
  
  <p>2</p>
  
  <p>3</p>
  
  <p>1</p>
  
  <p>4</p>
  
  <p>p.s. : I have tested all the parsing units in my program and the problem happens in the start commented area !!! and by the way all the lines in input are in one column , I couldn’t fix it here !</p>