-2

So I was going to add 5 random numbers to an arraylist(PolyArr). I am only a beginner in Java and do not know the syntax well. Can you please tell me how to correctly format my last line?

'package ga1;
import java.util.*;
import java.lang.Math;
public class GA1 {
    static int k=5;
    public static void main(String[] args) {
        double a;
        List<Double[]> PolyArr= new ArrayList<>(k);//Creating the arraylist
        for (int i=0; i<k; i++){
            a = Math.random() * 50;
            //PolyArr.add(new Double() {a});
        }
    }
}'
kazanaki
  • 7,988
  • 8
  • 52
  • 79

3 Answers3

0

you are trying to create an array of size 5 with 5 random ? use this :

    List<Double> polyArr= new ArrayList<>(k);//Creating the arraylist
    for (int i=0; i<k; i++){
        double a = Math.random() * 50; // random
        polyArr.add(a);
    }

Note : don't use upper case for attributes in java, only for classes name and static fields

By doing new Double[]{ a } you were creating an array of doulbes, of size 1, with 1 random inside

D. Peter
  • 487
  • 3
  • 12
0

You need to create the array first and add to it and then you can add the array ot the list. But do you really need the array? cant you just add the double directly to the list?

       import java.util.*;
        import java.lang.Math;
        public class GA1 {
            static int k=5;
            public static void main(String[] args) {
                double a;
                List<Double[]> PolyArr= new ArrayList<>(k);//Creating the arraylist
                Double[] randNums = new Double[k]; //create the double array first based on k
                for (int i=0; i<k; i++){
                    randNums[i] = Math.random() * 50;    // add to array               
                }
               PolyArr.add(randNums); // then add to the list
            }
}
Polynomial Proton
  • 5,020
  • 20
  • 37
0

PolyArr.add(new Double() {a});

The thing is you cannot create subclasses from final class. That is what you tried to do in above line. If you tried this in an IDE you may notice:

An anonymous class cannot subclass the final class Double

I donot know what is the purpose of this.. May be you are tackling with. Anyway, it is good for you to understand what's happening, you can do like this also:

double a[] = new double[k];
List<Double> PolyArr= new ArrayList<>(k);//Creating the arraylist
for (int i=0; i<k; i++){
    a[i] = Math.random() * 50;
    PolyArr.add(new Double(a[i]));
}

for(double i : PolyArr){
    System.out.println(i);
}

You can also try like this:

double a;
List<Double[]> PolyArr= new ArrayList<>(k);//Creating the arraylist
for (int i=0; i<k; i++){
    a = Math.random() * 50;

    Double he[] = {a};
    PolyArr.add(he);
}

for(Double[] i : PolyArr){
    for(Double y : i)
        System.out.println(y);
}

This may not be that you are looking for. however, try every answers.

Read these: final class, List, ArrayList Of Arrays

Blasanka
  • 21,001
  • 12
  • 102
  • 104