4

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
    {
        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();

}

}
3
  • 1
    Define "Doesn't work"? Perhaps some code and error messages? The snippit above is fine. Commented Mar 14, 2011 at 1:40
  • Hi Brian. If I attempt to add the following outside of the method: ArrayList<Double> weights = {1, 2, 3, 4, 10}; then I am presented with the following error message: Type mismatch: cannot convert from int[] to ArrayList<Double>. I am then prompted to change the type of array to int[] which is not what I am looking to do. If I add the same statement within the method, I am prompted to rename 'weights' and, once again, to change the type of array to int[]. I hope this helps? Commented Mar 14, 2011 at 1:45
  • the answers below from others should now help. When you say {1, 2, 3, 4, 10} it's an implicit array of int (int[]), which you can't give to ArrayList<Double>. If you don't need the features of the ArrayList, you could also just use double[] myWeights = { 1.0, 2.0, 3.0, 4.0, 10.0 } and have your method take double[] weights Commented Mar 14, 2011 at 3:18

4 Answers 4

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

new ArrayList<Double>(Arrays.asList(1.0, 2.0, ...)) works too. Though ideally the method wouldn't require an ArrayList but rather a List so you could just use Arrays.asList(1.0, 2.0, ...).
I agree with the use of List : scalesFitness(List<Double> weights)
1

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.

Comments

0

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.

2 Comments

Thank you, but whenever I try this, I am prompted to change the type of 'weights' to 'int[]' which I am not looking to do...may I ask how you would suggest to do this?
@Mick - we'd need code. That would indicate you're not understanding what you're creating and passing.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.