title: Wrong Answer using `get` method of ArrayList
body: <p>While solving <a href="https://www.hackerrank.com/challenges/maximum-element/problem" rel="nofollow noreferrer">this</a> question, I am getting wrong answer on some of the test cases of the problem using the <code>get</code> method of <code>ArrayList</code> directly in condition check.</p>
  <p>Code failing the test cases:</p>
  <pre class="lang-java prettyprint-override"><code>import java.io.*;
  import java.util.*;
  import java.text.*;
  import java.math.*;
  import java.util.regex.*;
  
  public class Solution {
      public static void main(String[] args) {
          Scanner sc=new Scanner(System.in);
          int n=sc.nextInt();
          Stack &lt;Integer&gt; s= new Stack&lt;&gt;();
          ArrayList &lt;Integer&gt; m= new ArrayList&lt;&gt;();
          
          for(int i=0;i&lt;n;i++){
              int x=sc.nextInt();
              if(x==1){
               int y=sc.nextInt();
               if(m.isEmpty() || y&gt;=m.get(m.size()-1))
                   m.add(y);
               s.push(y);  
               
              }
              else if(x==2){
                 // Here I am using `get` to check the last value of ArrayList with the top of stack
                 if(m.get(m.size()-1)==s.peek()) {
                     m.remove(m.size()-1);
                 }
                 s.pop();
              }
              else {
                  System.out.println(m.get(m.size()-1));
              }
          }
      }
  }
  </code></pre>
  <p>But when assigned the same syntax to a variable and used that for the condition check it passed.</p>
  <p>Code passing all the test cases:</p>
  <pre class="lang-java prettyprint-override"><code>import java.io.*;
  import java.util.*;
  import java.text.*;
  import java.math.*;
  import java.util.regex.*;
  
  public class Solution {
      public static void main(String[] args) {
          Scanner sc=new Scanner(System.in);
          int n=sc.nextInt();
          Stack &lt;Integer&gt; s= new Stack&lt;&gt;();
          ArrayList &lt;Integer&gt; m= new ArrayList&lt;&gt;();
          
          for(int i=0;i&lt;n;i++){
              int x=sc.nextInt();
              if(x==1){
               int y=sc.nextInt();
               if(m.isEmpty() || y&gt;=m.get(m.size()-1))
                   m.add(y);
               s.push(y);  
               
              }
              else if(x==2){
                  // When assigning the same to a variable it passed all test cases.
                  int a = m.get(m.size()-1); 
                 if(a==s.peek()) {
                     m.remove(m.size()-1);
                 }
                 s.pop();
              }
              else {
                  System.out.println(m.get(m.size()-1));
              }              
          }
      }
  }
  </code></pre>
  <p>As I am new to Java, I am confused with the use of the <code>get</code> method of the <code>ArrayList</code>. Please explain to me about the inner working of the <code>get</code> method that why it is failing or am I missing something?</p>