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.

gallickgunner
  • 472
  • 5
  • 20
pantalona
  • 1
  • 1
  • 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? – gallickgunner Mar 11 '16 at 14:39
  • Consider adding the language tag. The question have not yet got the right attention. – Tiny Mar 11 '16 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 :-) – pantalona Mar 11 '16 at 17:03
  • @pantalona - And what is `Num` ? – gallickgunner Mar 11 '16 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(); – pantalona Mar 11 '16 at 17:20
  • Oops sorry my bad didn't saw `Num` mentioned in your previous comment. As someone mentioned, your list holds object of type `Num` but double isn't a `Num` dats why it ain't working. Check this.. [link](http://stackoverflow.com/questions/18264933/how-to-add-string-arrays-to-array-list) – gallickgunner Mar 11 '16 at 17:39
  • @wandering-warrior yeah I get that in terms of doubles etc .... but I always thought that an ArrayList hold objects which themselves are arrays? My problem is that I can't change it to doubles as the ArrayList also needs to take an array of type float .... if that makes sense :-) – pantalona Mar 11 '16 at 17:41
  • @pantalona - then the answer given below is correct. You can't use .sqrt() on `arr` because `sqrt()` is only available for `NumList`, `NumDouble` and `NumFloat` i.e. all the classes that implement `Num` not for `NumDouble[]` – gallickgunner Mar 11 '16 at 18:07
  • @wandering-warrior thanks! also figured out that the constructors I had were for the ArrayList itself rather than for arrays within the ArrayList .... oh well, back to the drawing board! – pantalona Mar 14 '16 at 11:02

1 Answers1

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));
spectacularbob
  • 3,080
  • 2
  • 20
  • 41
  • 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(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! – pantalona Mar 11 '16 at 17:36