开发者

Java ArrayList<Double> as Parameter

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:

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 outside of the method or inside 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.

public static double ScalesFitness(ArrayList<Double> weights){
    //code emitted for illustration purposes
}

If anybody could help me it would be greatly appreciated. If any more code is required, then please let me know.

Thank you so much.

Mick.

EDIT: The code for the class in question is as follows:

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<n;++i)
    {
        char si = s.charAt(i);
        if (si != '0' && si != '1') ok = false;
    }
    if (ok)
    {
        scasol = s;
    }
    else
    {
 开发者_Python百科       scasol = RandomBinaryString(n);
    }
}
private static String RandomBinaryString(int n)
{
    String s = new String();

    for(int i = 0; i > 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<Double> weights)
{   
    if (scasol.length() > weights.size()) return(-1);
    double lhs = 0.0,rhs = 0.0;

    double L = 0;
    double R = 0;

    for(int i = 0; i < 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();
}

}

The other class file that I am using (Lab7) is:

import java.util.ArrayList;

public class Lab7 {

public static void main(String args[])
{

    for(int i = 0 ; i < 10; ++i)
    {
        double x = CS2004.UI(-1, 1);
        System.out.println(x);
    }

    System.out.println();

    ScalesSolution s = new ScalesSolution("10101");
    s.println();

}

}


you can these

1) use varargs instead of list

public static double scalesFitness(Double...weights)

so you can call this method with :

scalesFitness(1.0, 2.0, 3.0, 4.0, 10.0);

2) create the list outside your method

ArrayList<Double> weights = new ArrayList<Double>();
weights.add(1.0); 
weights.add(2.0); 
weights.add(3.0); 
weights.add(4.0); 
weights.add(10.0); 

scalesFitness(weights);


Towards your initial posting, this would work:

scalesFitness (new ArrayList<Double> (Arrays.asList (new Double [] {1.0, 2.0, 4.0, 10.0})));

You may explicitly list the values in Array form, but

  • you have to use 1.0 instead of 1, to indicate doubles
  • you have to prefix it with new Double [] to make an Array, and an Array not just of doubles
  • Arrays.asList() creates a form of List, but not an ArrayList, but
  • fortunately, ArrayList accepts a Collection as initial parameter in its constructor.

So with nearly no boilerplate, you're done. :)

If you can rewrite scalesFitness that would be of course a bit more easy. List<Double> as parameter is already an improvement.


Should I create the ArrayList outside of the method or inside the method?

The ArrayList is a parameter for the method so it need to be created outside the method, before you invoke the method.


You need to import ArrayList in the file that includes your methods. This is probably solved but that's the issue I was encountering.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜