Java ArrayList <Double> as parameter

I am currently working on a laboratory and would like to know how to deal with the following problem, which I spent at least two hours on:

I am invited to create an ArrayList containing the values ​​1, 2, 3, 4, and 10. Although I usually have no problem creating an ArrayList with the specified values, this time I am having problems. Should I create an ArrayList method outside of a method or inside a method? No matter how I try to do this, I have been presented with numerous error messages. How to add values ​​to this ArrayList parameter? I tried adding values ​​to it by calling it from the main method, but this still doesn't work. Here is the method.

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

If anyone can help me, it will be very appreciated. If any other code is required, then please let me know.

Thank you very 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(); } } 

Another class file that I use (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(); } } 

I hope this will be helpful.

Thanks,

Mick.

+4
source share
3 answers

you can use these

1) use varargs instead of a list

 public static double scalesFitness(Double...weights) 

so that you can call this method with:

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

2) create a 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); 
+2
source

By your initial publication, this will work:

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

You can explicitly specify values ​​in an Array form, but

  • you need to use 1.0 instead of 1 to indicate doubling
  • you need to attach it to new Double [] to create an array, and the array doesn't just double
  • Arrays.asList () creates a List form, but not an ArrayList, but
  • Fortunately, ArrayList accepts the collection as an initial parameter in its constructor.

So, with almost no template, everything is ready. :)

If you can rewrite scalesFitness , that would, of course, be a little easier. List<Double> as a parameter is already improving.

+1
source

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

ArrayList is a parameter for a method, so you need to create it outside the method before you call the method.

0
source

Source: https://habr.com/ru/post/1343568/


All Articles