![](/img/trans.png)
[英]Sorting ArrayList of ArrayList of double and string by the double in Java
[英]Java ArrayList<Double> as Parameter
我目前正在实验室工作,想知道如何处理我至少花了两个小时的以下问题:
我被要求创建一个包含值1,2,3,4和10的ArrayList。虽然我通常在创建具有所述值的ArrayList时遇到任何问题,但这次我遇到了麻烦。 我应该在方法之外还是在方法内部创建ArrayList? 无论我尝试过哪种方式,我都会收到许多错误消息。 如何向此ArrayList参数添加值? 从main方法调用它时,我试图向它添加值,但这仍然不起作用。 这是有问题的方法。
public static double ScalesFitness(ArrayList<Double> weights){
//code emitted for illustration purposes
}
如果有人可以帮助我,我将不胜感激。 如果需要更多代码,请告诉我。
非常感谢。
米克。
编辑:有问题的类的代码如下:
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();
}
}
我正在使用的另一个类文件(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();
}
}
你可以这些
1)使用varargs而不是list
public static double scalesFitness(Double...weights)
所以你可以调用这个方法:
scalesFitness(1.0, 2.0, 3.0, 4.0, 10.0);
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);
对于您的初始发布,这将有效:
scalesFitness (new ArrayList<Double> (Arrays.asList (new Double [] {1.0, 2.0, 4.0, 10.0})));
您可以在Array表单中明确列出值,但是
new Double []
来制作一个数组,而一个数组不仅仅是双精度数 所以几乎没有样板,你就完成了。 :)
如果你可以重写scalesFitness
,那当然会更容易一些。 List<Double>
作为参数已经是一个改进。
您需要在包含方法的文件中导入ArrayList。 这可能已经解决了,但那是我遇到的问题。
我应该在方法之外还是在方法内部创建ArrayList?
ArrayList是方法的参数,因此需要在调用方法之前在方法外部创建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.