title: Java ArrayList<Double> as Parameter
body: <p>I am currently working on a lab and would like to know how to handle the following problem which I have spent at least two hours on:</p>
  
  <p>I am asked to create an ArrayList containing the values 1, 2, 3, 4 and 10.  Whilst I usually never have any trouble creating an ArrayList with said values, I am having trouble this time.  Should I create the ArrayList <b>outside</b> of the method or <b>inside</b> the method?  Whichever way I have attempted it, I have been presented with numerous error messages.  How do I add values to this ArrayList parameter?  I have attempted to add values to it when calling it from the main method, but this still doesn't work.  Here is the method in question.</p>
  
  <pre><code>public static double ScalesFitness(ArrayList&lt;Double&gt; weights){
      //code emitted for illustration purposes
  }
  </code></pre>
  
  <p>If anybody could help me it would be greatly appreciated.  If any more code is required, then please let me know.</p>
  
  <p>Thank you so much.</p>
  
  <p>Mick.</p>
  
  <p>EDIT: The code for the class in question is as follows:</p>
  
  <pre><code>import java.util.*;
  
  public class ScalesSolution
  {
  private static String scasol;
  //Creates a new scales solution based on a string parameter
  //The string parameter is checked to see if it contains all zeros and ones
  //Otherwise the random binary string generator is used (n = length of parameter)
  public ScalesSolution(String s)
  {
      boolean ok = true;
      int n = s.length();
      for(int i=0;i&lt;n;++i)
      {
          char si = s.charAt(i);
          if (si != '0' &amp;&amp; si != '1') ok = false;
      }
      if (ok)
      {
          scasol = s;
      }
      else
      {
          scasol = RandomBinaryString(n);
      }
  }
  private static String RandomBinaryString(int n)
  {
      String s = new String();
  
      for(int i = 0; i &gt; s.length(); i++){
          CS2004.UI(0,1);
              if(i == 0){
                  System.out.println(s + "0");
              }
              else if(i == 0){
                  System.out.println(s + "1");
              }
      }
  
      return(s);
  }
  public ScalesSolution(int n) 
  {
      scasol = RandomBinaryString(n); 
  }
  //This is the fitness function for the Scales problem
  //This function returns -1 if the number of weights is less than
  //the size of the current solution
  
  
  
  public static double scalesFitness(ArrayList&lt;Double&gt; weights)
  {   
      if (scasol.length() &gt; weights.size()) return(-1);
      double lhs = 0.0,rhs = 0.0;
  
      double L = 0;
      double R = 0;
  
      for(int i = 0; i &lt; scasol.length(); i++){
          if(lhs == 0){
              L = L + i;
          }
          else{
              R = R + i;
          }
      }
  
      int n = scasol.length();
  
      return(Math.abs(lhs-rhs));
  }
  //Display the string without a new line
  public void print()
  {
      System.out.print(scasol);
  }
  //Display the string with a new line
  public void println()
  {
      print();
      System.out.println();
  }
  
  }
  </code></pre>
  
  <p>The other class file that I am using (Lab7) is:</p>
  
  <pre><code>import java.util.ArrayList;
  
  public class Lab7 {
  
  public static void main(String args[])
  {
  
      for(int i = 0 ; i &lt; 10; ++i)
      {
          double x = CS2004.UI(-1, 1);
          System.out.println(x);
      }
  
      System.out.println();
  
      ScalesSolution s = new ScalesSolution("10101");
      s.println();
  
  }
  
  }
  </code></pre>