title: Is there a way to pick out specific elements from an ArrayList?
body: <p>I'm trying to read input from three separate files, and the user specifies which file to read. Each line in each file contains info about a temperature reading: a latitude, a longitude, the year, the month, the max temp, the min temp, and the average temp. So each line looks something like this:</p>
  
  <pre><code>39.95 41.1667 2002 04 -9999 -9999 420
  </code></pre>
  
  <p>Each temperature will also be divided by 10 to get the actual temperature in Celsius. If the temperature is -9999, it will not be read. Also, each item is added to an ArrayList that I have:</p>
  
  <pre><code>ArrayList&lt;TempRead&gt; t = new ArrayList();
  </code></pre>
  
  <p>I also have a TempRead class that looks like this: </p>
  
  <pre><code>public class TempRead {
  private double latitude = -100;
  private double longitude = -180;
  private int year = 0;
  private int month = 0;
  private double maxTemp = -9999;
  private double minTemp = -9999;
  private double avgTemp = -9999;
  
  public TempRead(){}
  
  public TempRead(double latitude, double longitude, int year, int month, double maxTemp, double minTemp, double avgTemp){
      this.latitude = latitude;
      this.longitude = longitude;
      this.month = month;
      this.year = year;
      this.maxTemp = maxTemp;
      this.minTemp = minTemp;
      this.avgTemp = avgTemp;
  }
  
  public double getLatitude(){
      return latitude;
  }
  
  public double getLongitude(){
      return longitude;
  }
  
  public int getMonth(){
      return month;
  
  }
  
  public int getYear(){
      return year;
  }
  
  public double getMaxTemp(){
      return maxTemp;
  }
  
  public double getMinTemp(){
      return minTemp;
  }
  
  public double avgTemp(){
      return avgTemp;
  }
  
  @Override
  public String toString(){
      return latitude + " " + longitude + " " + year + " " + month + " " + maxTemp + " " + minTemp + " " + avgTemp + " ";
  }
  </code></pre>
  
  <p>The problem I'm having right now is that I'm not exactly sure how to get the temperature elements of the ArrayList. I've done something like this:</p>
  
  <pre><code>System.out.println(t.get(0));
  </code></pre>
  
  <p>And it shows that each line is in fact being added to the ArrayList, as it prints out the first line in the file. But I have to calculate averages, and max and mins. Is there a way that I can pick out the last three values of the ArrayList specifically so that I can do these calculations?</p>
  
  <p>EDIT: Here is what I have in main:</p>
  
  <pre><code>public static void main(String[] args) {
      ArrayList&lt;TempRead&gt; t = new ArrayList();
      readFile(t);
      avgAllTemps(t);
  }
  
  public static void readFile(ArrayList&lt;TempRead&gt;t){
          try{
          String userFileName;
          Scanner in = new Scanner(System.in);
          System.out.println("Enter name of file: ");
          userFileName = in.next();
          System.out.println();
  
          Scanner file = new Scanner(new File(userFileName));
  
          while(file.hasNext()){
              t.add(new TempRead(file.nextDouble(), file.nextDouble(), file.nextInt(), file.nextInt(), file.nextDouble(), file.nextDouble(), file.nextDouble()));
          }
      }catch(IOException e){
          System.out.println(e);
      }
  }
  
  public static void avgTemps(ArrayList&lt;TempRead&gt;t){
      double totalMinTemp = 0.0;
      int count = 0;
      for (TempRead entry : t) {
          if (entry.getMinTemp() != -9999) {
              totalMinTemp += entry.getMinTemp();
              count++;
          }
          double avgMinTemp = totalMinTemp / count;
          System.out.println("Avg of min temps in arraylist: " + avgMinTemp);
      }
  }
  </code></pre>