0

I'm trying to create two different types of Arrays within one ArrayList. Set up constructors accordingly (I think), but when it comes to instantiating them I get an error message "arr cannot be resolved". I'm slowly but surely going round the bend. How do I get the ArrayList to accept a simple array with doubles? (It also has to accept other types so it's not just a question of changing the ArrayList itself).Here's the code for the constructors & main ArrayList:

class NumList implements Num
{
    private ArrayList<Num> n1;

    public NumList( NumDouble[] doubleArray ) 
    {           
       n1 = new ArrayList<Num>();
       for( NumDouble d : doubleArray ) 
            n1.add( d );
    }

    public NumList(NumFloat[] floatArray ) 
    {
       n1 = new ArrayList<Num>();
       for( NumFloat d : floatArray ) 
            n1.add( d );

    }
// methods of Num interface
}

And my test class looks like this -

import java.util.ArrayList;

public class Demo extends NumList {
    public Demo(NumDouble[] doubleArray) {  
        //suggested automatically to add super here
        super(doubleArray);

        double[] arr = {(1.1), (2.2), (3.3), (4.4)};
        ArrayList<Num> n1 = new ArrayList<Num>(arr);
    }

    public static void main (String [] args){

        arr.sqrt();

        System.out.println("The numbers sq are "+ arr [0]);
    }
}

The NumList class has just three methods including sort. I have tried wildcards as well as

It's probably something really easy ... any help appreciated.

9
  • 1
    What's Num used in Array list declaration at the top ? Is it a class? Where is the code for it? You are talking about NumList Interface, but looks like NumList is a class? Commented Mar 11, 2016 at 14:39
  • Consider adding the language tag. The question have not yet got the right attention. Commented Mar 11, 2016 at 15:33
  • @wandering-warrior NumList is inheriting from an Interface called Num which just has three methods (neg, sort and asString). Should have made that clearer I guess :-) Commented Mar 11, 2016 at 17:03
  • @pantalona - And what is Num ? Commented Mar 11, 2016 at 17:09
  • @wandering-warrior Num is the name of the interface which has three methods: public void neg(); public void sqrt(); public String asString(); Commented Mar 11, 2016 at 17:20

1 Answer 1

2

Your ArrayList holds object of type Num, but you are trying to insert plain ol' doubles into it

double[] arr = {(1.1), (2.2), (3.3), (4.4)};
ArrayList<Num> n1 = new ArrayList<Num>(arr);

double does not inherit from Num and so cannot be placed in an ArrayList<Num>. Also, no ArrayList constructor takes an array as a parameter, you have to convert your array to a collection with Arrays.asList(array). You would have to do something like this

NumDouble[] arr = {new NumDouble(1.1), new NumDouble(2.2), new NumDouble(3.3), new NumDouble(4.4)};
ArrayList<Num> n1 = new ArrayList<Num>(Arrays.asList(arr));
Sign up to request clarification or add additional context in comments.

1 Comment

that seems to work as long as I don't use n1 (otherwise I get a warning saying n1 is not used) - so : new ArrayList<Num>(Arrays.asList(arr)); gives me no warning and is ok? Just have to figure out calling the method sort on it (are.sqrt does not work). Thanks!

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.