1

Take for example:

public static String[] multipleOfSameSet(String var,int setLength){
    String[] out = new String[setLength];
    for(int i=0; i<setLength; i++){
        out[i] = var;
    }
    return out;
}

but I want this to work for int, double and about 10 other classes. I tried using a class in place of String and it gave me a world of errors. Here:

Is it even possible?? If yes, how do I do it?

Frankely Diaz
  • 886
  • 1
  • 9
  • 16
kbluue
  • 369
  • 1
  • 5
  • 20

2 Answers2

4

Yes, it's possible. One option is to try and find a class that is a superclass of all the classes you want to use, or an interface all your classes implement. In your case, the only candidate might be Object:

public static Object[] multipleOfSameSet(Object var, int setLength) {
    Object[] out = new Object[setLength];
    for(int i = 0; i < setLength; i++) {
        out[i] = var;
    }
    return out;
}

This will work, because all Java classes extend Object, either directly or indirectly. Primitive values get converted into objects automaticaly (ints become Integers, doubles become Doubles and so on).

The downside of this approach is that, well, you get an array of Objects back, and there's not much you can do with those. What you might want to consider instead is making your method accept some generic type T, and returning an ArrayList of T's:

public static <T> ArrayList<T> multipleOfSameSet(T object, int setLength) {
    ArrayList<T> out = new ArrayList<T>();
    for(int i = 0; i < setLength; i++) {
        out.add(object);
    }
    return out;
}

However, if you don't need to modify the list afterwards, I'd go with this:

public static <T> List<T> multipleOfSameSet(T object, int setLength) {
    return Collections.nCopies(setLength, object);
}
Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • This is bad solution. It forces you to use `projections`. – Laszlowaty Jul 20 '15 at 00:34
  • @Laszlowaty it works but why do you say it is bad solution and what are projections? – kbluue Jul 20 '15 at 00:42
  • It leads to class cast exceptions when you cast your Object to some concrete class that i'snt correct at runtime – Constantin Jul 20 '15 at 00:43
  • I'm now sure i've used proper word. Projections are changing type of an variable. For example: `double a;double b; int c = (int)a/b;` – Laszlowaty Jul 20 '15 at 00:47
  • @Laszlowaty Ah, you were thinking about *casts*. I've added a second example that gives you a typed ArrayList. – Wander Nauta Jul 20 '15 at 00:48
  • Yes, casts. Damn english ;) – Laszlowaty Jul 20 '15 at 00:57
  • @WanderNauta only problem is my output is totally different. is there no way i can cast it back to int or string if i want to. normal (int) casting donyt seem to be woorking – kbluue Jul 20 '15 at 01:26
  • @Lasz I've spent a while trying out both the generic (with Arraylist) method and the Object method and i noticed that my output can be read as Objects or Arraylist but then the loss all property of their intended class and i can not cast them with an error or the other. for example in the case of String, i can not use the String.concat() method on it but i can print the String as is. Without the individual class property of the output, my code is quite useless. What do I do? – kbluue Jul 20 '15 at 23:20
  • @kbluue [read this](http://stackoverflow.com/questions/5289393/casting-variables-in-java) It should help ;-) – Laszlowaty Jul 20 '15 at 23:26
  • I understand how casting work now but it still doesnt work with my code. Is it because mine is an ArraySet?? i include the [] to specify that I am casting to an array and it still doesn't work @Lasz – kbluue Jul 21 '15 at 00:12
  • @kbluue did you used generic types? Did you pass Class reference to your Array? I don't know java good enough. I think you should create new question for your problem. – Laszlowaty Jul 21 '15 at 02:04
  • @Las i figured that out but i have a new issue. I'm creating a method and i need a new instance of a Generic Type. I cant seem to understand the solution here as they both seem to require me create a new class and i dont think that is necessary. Please could you expand further. The answers are here ==> http://stackoverflow.com/questions/2434041/instantiating-generics-type-in-java ... http://stackoverflow.com/questions/1090458/instantiating-a-generic-class-in-java ... http://www.artima.com/weblogs/viewpost.jsp?thread=208860 (is there a way we could directly chat in here?) – kbluue Jul 23 '15 at 21:55
  • @kbluue well i have no if there is chat here. My java isn't good, really. I think other people will be more helpfull – Laszlowaty Jul 24 '15 at 01:21
0

You can use interfaces or defined class types..

public static MyInterface[] multipleOfSameSet(MyInterface var, int setLength) {
    MyInterface[] out = new MyInterface[setLength];
    for(int i = 0; i < setLength; i++) {
        out[i] = var;
    }
    return out;
}
Constantin
  • 1,506
  • 10
  • 16