简体   繁体   English

Java ArrayList <Double> 作为参数

[英]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. 我被要求创建一个包含值1,2,3,4和10的ArrayList。虽然我通常在创建具有所述值的ArrayList时遇到任何问题,但这次我遇到了麻烦。 Should I create the ArrayList outside of the method or inside the method? 我应该在方法之外还是在方法内部创建ArrayList? Whichever way I have attempted it, I have been presented with numerous error messages. 无论我尝试过哪种方式,我都会收到许多错误消息。 How do I add values to this ArrayList parameter? 如何向此ArrayList参数添加值? I have attempted to add values to it when calling it from the main method, but this still doesn't work. 从main方法调用它时,我试图向它添加值,但这仍然不起作用。 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
    {
        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: 我正在使用的另一个类文件(Lab7)是:

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 1)使用varargs而不是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 2)在方法之外创建列表

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 您可以在Array表单中明确列出值,但是

  • you have to use 1.0 instead of 1, to indicate doubles 你必须用1.0而不是1来表示双打
  • you have to prefix it with new Double [] to make an Array, and an Array not just of doubles 你必须在它前面加上一个new Double []来制作一个数组,而一个数组不仅仅是双精度数
  • Arrays.asList() creates a form of List, but not an ArrayList, but Arrays.asList()创建List的形式,但不是ArrayList
  • fortunately, ArrayList accepts a Collection as initial parameter in its constructor. 幸运的是,ArrayList在其构造函数中接受Collection作为初始参数。

So with nearly no boilerplate, you're done. 所以几乎没有样板,你就完成了。 :) :)

If you can rewrite scalesFitness that would be of course a bit more easy. 如果你可以重写scalesFitness ,那当然会更容易一些。 List<Double> as parameter is already an improvement. List<Double>作为参数已经是一个改进。

You need to import ArrayList in the file that includes your methods. 您需要在包含方法的文件中导入ArrayList。 This is probably solved but that's the issue I was encountering. 这可能已经解决了,但那是我遇到的问题。

Should I create the ArrayList outside of the method or inside the method? 我应该在方法之外还是在方法内部创建ArrayList?

The ArrayList is a parameter for the method so it need to be created outside the method, before you invoke the method. ArrayList是方法的参数,因此需要在调用方法之前在方法外部创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM