Delupara
Delupara

Reputation: 359

How can I fix a type mismatch error of ArrayList type? Possible Multidimensional Array?

So I've been trying to code static, methods in my class to practice what I've learned.My understanding of generic types and ArrayLists are still null Which is why I ask here since troubleshooting has been horrendous (I'm currently reading Java Core 9th edition first book down to chapter 6).

The method is supposed to take in any number of double arrays (wich creates a multidimensional array), and check each individual one to see if they either contain less or more than three double elements, Then do things with those arrays etc... If an array contains less than two double elements, show an error and exit. If there's more than 3, create an Arraylist of that array and delete any element that are exceeding.

My issue comes when I try to create an ArrayList of the array "d" in the for each loop of my multidimensional array "DDs". I took every suggestion from this question but none worked, I've tried a few other suggestions from other websites but again none functioned. Here is a snippet of my code.

public static final void Calcs(double[]... DDs)
{
    for(double[] d : DDs)
    {
        if(d.length < 3)
        {
            System.out.println("Variable Error 1 :: Array Distributed exceeds 3 types");
            System.exit(0);
        }else if(d.length > 3)
        {
            ArrayList<Double> tmp = new ArrayList<>(Arrays.asList(d));
            //Type mismatch: cannot convert from ArrayList<double[]> to ArrayList<Double>
        }
    }

}

My Hypothesis is that the array d is encased in by another array. but I am not sure and rather ask someone who does:

What I think x should look like:

d = [x]

what I think it might look like:

d = [[x]]

Any troubleshooting ideas? I am very new to generic types and that error doesn't tell me much. Thanks a lot for the feedback.

Upvotes: 2

Views: 727

Answers (2)

John Hascall
John Hascall

Reputation: 9416

This compiles, but I am not certain if it is what you seek:

public static final void Calcs ( double[]... DDs ) {
    for ( double[] d : DDs ) {
        if ( d.length < 3 ) {
            System.out.println("Less than 3");
            System.exit(0);
        }
        ArrayList<double[]> tmp = new ArrayList<>(Arrays.asList(d));
    }
}

Maybe this, then:

public static final void Calcs ( double[]... DDs ) {
    for (double[] d : DDs ) {
        if (d.length < 3) {
            System.out.println("Less than 3");
            System.exit(0);
        }
        ArrayList<Double> tmp = new ArrayList<>(d.length);
        for ( double od : d ) tmp.add(od);
    }
}

Upvotes: 0

samczsun
samczsun

Reputation: 1024

You have two options here. You can either treat your 2D array (because varargs is practically an array) as a List of double[], or a List of List<Double>

The first way would be

List<double[]> list = new ArrayList<>(Arrays.asList(DDs)); //Arrays.asList() returns a fixed size list so we'll put it into a flexible list

The second way would be

List<List<Double>> list = new ArrayList<>();
for (double[] doubles : DDs) {
    List<Double> listDoubles = new ArrayList<>(); //Cannot use Arrays.asList() because that will return List<double[]> with a single element
    for (double innerDouble : doubles) {
        listDoubles.add(innerDouble);
    }
    list.add(listDoubles);
}

Upvotes: 1

Related Questions