title: Java ArrayList with condition
body: <p>I have a question about if condition in ArrayList, Please see code:</p>
  
  <pre><code>public class Test {
  
      public static void main(String[] args) {
  
          ArrayList&lt;BankAccount&gt;accounts=new ArrayList&lt;BankAccount&gt;();
          BankAccount Mary=new BankAccount(1,50);
          BankAccount Lucy=new BankAccount(2,100);
          BankAccount Lily=new BankAccount(3,20);
          BankAccount Pete=new BankAccount(4,200);
          BankAccount Paul=new BankAccount(5,30);
  
          accounts.add(Mary);
          accounts.add(Lucy);
          accounts.add(Lily);
          accounts.add(Pete);
          accounts.add(Paul);
  
          double min = accounts.get(0).getBalance();
          int poorestPerson = accounts.get(0).getAccountNumber();
          for(BankAccount a:accounts) {
              if(a.getBalance()&lt;min) {
                  poorestPerson=a.getAccountNumber();
              }
          }
          System.out.println("Poorest person is "+poorestPerson);
      }
  }
  </code></pre>
  
  <p>The result is always 5, but the correct print should be 3, when I add :</p>
  
  <pre><code>  poorestPerson=a.getAccountNumber();
    **min=a.getBalance();** 
  </code></pre>
  
  <p>It prints right answer, my question is how does this command vary the result?Cheers.</p>
  
  <p>I have understood how does this command work, but could someone please explain if I do not write <strong>min=a.getBalance();</strong> , how does the condition work and why does it print the last element?</p>