Cannot pass char[] to generic method


slow down

I wrote a utility class to solve the problem. It takes a generic array and returns all combinations (no duplicates).nCr

import java.util.ArrayList;


public class fooClass {

    public static void main(String[] args) {
        class Utils {
            public int factorial(int n) {
                int p = 1;
                int i = 1;
                while(i <= n) {
                    p *= i++;
                }
                return p;
            }
            public <T> ArrayList<T[]> combinations(T[] array, int r) {
                int n = array.length;
                int[] vec = new int[r];
                int i, j, k, m, o;
                for (i = 0; i < vec.length; i++) {
                    //int j = vec[i];
                    vec[i] = i;
                }
                ArrayList<T[]> result = new ArrayList<T[]>();
                int total = factorial(n) / (factorial(r) * factorial(n - r));
                for (i = 0; i < total; i++) {
                    T[] combination = (T[])new Object[r];
                    for (k = 0; k < r; k++) {
                        combination[k] = array[vec[k]];                    
                    }
                    result.add(combination);
                    j = r - 1;
                    if (vec[j] + 1 < n) {
                        vec[j]++;
                    } else {
                        o = j;
                        while (j-- >= 0) {
                            if (vec[j] + 1 < n - r - (j + 1)) {
                                vec[j]++;
                                m = j + 1;
                                while (m++ <= o) {
                                    vec[m] = vec[m-1] + 1;
                                }
                                break;
                            } //if end
                        } //while end
                    } //if-else end
                }

                return result;
            }
        };

        Utils utils = new Utils();
        String test = "abcde";
        ArrayList<char[]> combinations = utils.combinations(test.toCharArray(), 3);
    }

}

Now the problem is here, I call it "phone". When I pass, the compiler complains char[].

Method groups(T[], int) of type Utils does not work with parameters (char[], int)

What is the correct way to solve this problem?

anonymous

I think it's because char is a primitive type and T means object. If you use Character[]instead char[], it should work fine.

char[] chars = test.toCharArray();
Character[] arg = new Character[chars.length];
for (int i = 0; i < chars.length; i++)
    arg[i] = chars[i];
ArrayList<Character[]> combinations = utils.combinations(arg, 3);

This will copy all characters from the char array to the Character array and then pass it to the method.

Related


Cannot pass char[] to generic method

slow down I wrote a utility class to solve the problem. It takes a generic array and returns all combinations (no duplicates).nCr import java.util.ArrayList; public class fooClass { public static void main(String[] args) { class Utils {

How to pass generic to generic method?

Agish A I have a generic class like below //Un modifiable source public AbstractJsonWebserviceConnector<T extends JsonRequest, U>{ public U getResponse(T request, Class<U> responseType) throws Exception { //Do Some stuff } } Another cl

How to pass generic to generic method?

Mobshere Mughal I want to call a method that accepts an T typeobject. In my custom method, I receive T type. How can I pass me T typeto the method already received T type? Can it be called by reflection? Here is my method: public IEnumerable<T> Method2<T>(stri

How to pass generic to generic method?

Agish A I have a generic class like below //Un modifiable source public AbstractJsonWebserviceConnector<T extends JsonRequest, U>{ public U getResponse(T request, Class<U> responseType) throws Exception { //Do Some stuff } } Another cl

How to pass generic to generic method?

Mobshere Mughal I want to call a method that accepts an T typeobject. In my custom method, I receive T type. How can I pass me T typeto the method already received T type? Can it be called by reflection? Here is my method: public IEnumerable<T> Method2<T>(stri

Pass generic parameter to method

User 11383582 I have a small question about generic programming in Java. Is there a way to check the type of the parameter passed to the method? I want to compare the type of the instance on which the method is called and the arguments passed to it. If they ar

Pass generic type to method

garden giant I want to write a generic function that passes a generic parameter to a function with multiple overloads. The C++ equivalent of this . Here is what I have tried: public void setUniform1<T>(int loc, T value) { GL.Uniform1(loc, value); } but th

Pass generic parameter to method

User 11383582 I have a small question about generic programming in Java. Is there a way to check the type of the parameter passed to the method? I want to compare the type of the instance on which the method is called and the arguments passed to it. If they ar

Pass generic type to method

garden giant I want to write a generic function that passes a generic parameter to a function with multiple overloads. The C++ equivalent of this . Here is what I have tried: public void setUniform1<T>(int loc, T value) { GL.Uniform1(loc, value); } but th

Pass generic type to method

garden giant I want to write a generic function that passes a generic parameter to a function with multiple overloads. The C++ equivalent of this . Here is what I have tried: public void setUniform1<T>(int loc, T value) { GL.Uniform1(loc, value); } but th

cannot pass char *** as parameter

Bisla It's easy to see the tasks to be done here. Read a list of files and pass it to another function. Why doesn't this work. When I try to store the filename in a local char** it works fine, but I can't send it back via a pointer. gives segmentation fault. i

cannot pass char *** as parameter

Bisla It's easy to see the tasks to be done here. Read a list of files and pass it to another function. Why doesn't this work. When I try to store the filename in a local char** it works fine, but I can't send it back via a pointer. gives segmentation fault. i

How to pass generic type to generic method?

size Why can't I call SomeGenericMethod<SomeGenericType<>>? class NotGeneric { } class Generic<T> { } class Program { static void Main(string[] args) { PrintType(typeof(NotGeneric)); PrintType(typeof(Generic<>)); PrintType<Not

How to pass a generic class as a generic parameter of a method

Jaroslaw K. I have a problem with passing a class as a generic parameter of a method, if I have a simple method: <T> T sendRequest(SomeRestApiRequest request, Class<T> responseClass) Parses the response to the specified form. I use them this way: ItemListJSON

How to pass a generic class as a generic parameter of a method

Jaroslaw K. I have a problem with passing a class as a generic parameter of a method, if I have a simple method: <T> T sendRequest(SomeRestApiRequest request, Class<T> responseClass) Parses the response to the specified form. I use them this way: ItemListJSON

How to pass generic type to generic method?

size Why can't I call SomeGenericMethod<SomeGenericType<>>? class NotGeneric { } class Generic<T> { } class Program { static void Main(string[] args) { PrintType(typeof(NotGeneric)); PrintType(typeof(Generic<>)); PrintType<Not

How to pass a generic class as a generic parameter of a method

Jaroslaw K. I have a problem with passing a class as a generic parameter of a method, if I have a simple method: <T> T sendRequest(SomeRestApiRequest request, Class<T> responseClass) Parses the response to the specified form. I use them this way: ItemListJSON

How to pass a generic class as a generic parameter of a method

Jaroslaw K. I have a problem with passing a class as a generic parameter of a method, if I have a simple method: <T> T sendRequest(SomeRestApiRequest request, Class<T> responseClass) Parses the response to the specified form. I use them this way: ItemListJSON

Pass generic type as generic method parameter in Java

Alien01 First, let me apologize for the poor title, but I don't know how to sum this up in one sentence. public class GenericFun { public class TypedStream<I extends OutputStream> { I input; public I getInput() { return input; } pu

How to pass generic type to generic method?

size Why can't I call SomeGenericMethod<SomeGenericType<>>? class NotGeneric { } class Generic<T> { } class Program { static void Main(string[] args) { PrintType(typeof(NotGeneric)); PrintType(typeof(Generic<>)); PrintType<Not

How to pass generic type to generic method?

size Why can't I call SomeGenericMethod<SomeGenericType<>>? class NotGeneric { } class Generic<T> { } class Program { static void Main(string[] args) { PrintType(typeof(NotGeneric)); PrintType(typeof(Generic<>)); PrintType<Not

How to pass a generic class as a generic parameter of a method

Jaroslaw K. I have a problem with passing a class as a generic parameter of a method, if I have a simple method: <T> T sendRequest(SomeRestApiRequest request, Class<T> responseClass) Parses the response to the specified form. I use them this way: ItemListJSON

How to pass a generic class as a generic parameter of a method

Jaroslaw K. I have a problem with passing a class as a generic parameter of a method, if I have a simple method: <T> T sendRequest(SomeRestApiRequest request, Class<T> responseClass) Parses the response to the specified form. I use them this way: ItemListJSON

How to pass generic type to generic method?

size Why can't I call SomeGenericMethod<SomeGenericType<>>? class NotGeneric { } class Generic<T> { } class Program { static void Main(string[] args) { PrintType(typeof(NotGeneric)); PrintType(typeof(Generic<>)); PrintType<Not

How to pass generic type to generic method?

size Why can't I call SomeGenericMethod<SomeGenericType<>>? class NotGeneric { } class Generic<T> { } class Program { static void Main(string[] args) { PrintType(typeof(NotGeneric)); PrintType(typeof(Generic<>)); PrintType<Not

Pass generic type to non-generic method

crash Z I was wondering how to mix calls of specific type methods when coming from generic methods? I know the example given is redundant. It is for illustration purposes only. The minimal synthetic context I imagine is as follows: This method BitConverter.Get

Pass generic type class to method

marry I want to convert List<Object[]>toList<ClassX> For this I created a method: public List mapObjectArrayListToClassList(List<Object[]> objectArrayList, Class genericType){ //some awesome logic return new ArrayList<genericType>(); } I want to call

Pass generic type class to method

marry I want to convert List<Object[]>toList<ClassX> For this I created a method: public List mapObjectArrayListToClassList(List<Object[]> objectArrayList, Class genericType){ //some awesome logic return new ArrayList<genericType>(); } I want to call

Pass generic type class to method

marry I want to convert List<Object[]>toList<ClassX> For this I created a method: public List mapObjectArrayListToClassList(List<Object[]> objectArrayList, Class genericType){ //some awesome logic return new ArrayList<genericType>(); } I want to call