简体   繁体   中英

Overloading constructor with a generic type in java

Here is the code:

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IDEA on 15/11/14.
 */
public class Matrix<T> {
    private final int nrow;
    private final int ncol;

    Matrix(List<List<T>> data) {
        nrow = data.size();
        ncol = data.get(0).size();
    }

    Matrix(List<T[]> data) {
        nrow = data.size();
        ncol = data.get(0).length;
    }
}

The IDE asks me to delete the first constructor, why?

The IDE asks me to rename or delete the first constructor, why?

It's because of the way generics work in Java. Due to type erasure , you're effectively declaring two constructors like this:

Matrix(List data)

That's what the JVM sees, with some added metadata about the type arguments involved.

This isn't just for constructors - you can't overload methods based on generic type arguments, either.

See the Java Generics FAQ entry on type erasure for the gruesome details.

It's odd that your constructor only needs the row and column count, but here's a version using static factory methods:

import java.util.ArrayList;
import java.util.List;

public class Matrix<T> {
    private final int nrow;
    private final int ncol;

    private Matrix(int nrow, int ncol) {
        this.nrow = nrow;
        this.ncol = ncol;
    }

    public static <T> Matrix<T> fromArrays(List<T[]> data) {
        return new Matrix<T>(data.size(), data.get(0).length);
    }

    public static <T> Matrix<T> fromLists(List<? extends List<T>> data) {
        return new Matrix<T>(data.size(), data.get(0).size());
    }
}

And a demo of using this:

public class Test {
    public static void main(String[] args) {
        // We're only checking the compilation for the moment :)
        List<List<String>> lists = null;
        List<String[]> arrays = null;

        Matrix<String> x = Matrix.fromArrays(arrays);
        Matrix<String> y = Matrix.fromLists(lists);
    }
}

Because both the constructors have same signatures (same number of parameters and same type). The generic types are not enough to make overloaded constructors/methods and that is because of type erasure.

Both the constructors are treated as Matrix(List a) due to type erasure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM