1

I am newbie to java. But I used c++ a lot. I got a class written in c++. Now I need to rewrite the class in java. I got confused about the generic types.

My c++ class is something like this:

//Form, Cmpt are of class type. nCmpt is a integer

template <class Form, class Cmpt, int nCmpt>
class VectorSpace
{
public:
   //component array
   Cmpt v[nCmpt];
   //constructors
   VectorSpace();
   VectorSpace(const VectorSpace<Form,Cmpt,nCmpt>& m_v);
   .....
   .....
};

I tried to rewrite it in java, I got problems:

public class VectorSpace<Form, Cmpt, nCmpt>
{
    public Cmpt v[];
    public VectorSpace()
    {
        v = new Cmpt[nCmpt];
    }
    .....
    .....
}

I got errors:

**Can't create a generic array of Cmpt.

nCmpt cannot be resolved to a variable.**

Can anyone help this out? How can I use template in java to achieve the same functionality as it was in c++? (I don't want to define an integer variable inside class and use it as an argument in the constructor of my java version).

Thank you.

Jerry
  • 323
  • 1
  • 2
  • 10
  • 2
    Java generic parameters can only be types. – user253751 May 22 '15 at 00:07
  • Use `Object v[]`. It's not generic, and you need casting in places. – ZhongYu May 22 '15 at 00:10
  • 4
    This sounds like a flippant answer, but as someone who was once in your shoes (long ago :) ), my recommendation would be to try to forget C++ templates altogether and just learn Java as if they're a new thing. Obviously the motivations behind the two are very similar, but the implementations _and the patterns they imply_ are different enough that it's probably easier to learn them as separate constructs, than it is to try to "translate" templates to generics. – yshavit May 22 '15 at 00:12

2 Answers2

2

C++ templates are very different from Java generics, Check this out. Java Doesn't allow the construction generic arrays because of type erasure . You can create the array as array of objects and cast to Cmpt[] afterwards

// we can't pass values as type-parameter in Java 
public VectorSpace(int nCmpt){ 
   v = (Cmpt[])new Object[nCmpt];
}

This code however is dodgy and you better avoid arrays with generics and use a List instead. Prefer Lists To Arrays

private List<Cmpt> v;

public VectorSpace()
{
    v = new ArrayList<Cmpt>();
}

I don't recommend you learning Java or any language by direct translation. Pick a book or an online tutorial and teach yourself the language.

Community
  • 1
  • 1
Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
0

C++ templates don't directly translate to Java generics:

  • nCmpt should be just a member variable.
  • Java arrays and generics don't mix very well. Most of the time, you are better off using ArrayList.
  • As https://docs.oracle.com/javase/tutorial/java/generics/types.html states:

    By convention, type parameter names are single, uppercase letters. This stands in sharp contrast to the variable naming conventions that you already know about, and with good reason: Without this convention, it would be difficult to tell the difference between a type variable and an ordinary class or interface name.

With all that, your class becomes:
class VectorSpace<F, C> {

    private final List<C> v;
    private final int nCmpt;

    public VectorSpace(int nCmpt) {
        this.nCmpt = nCmpt;
        this.v = new ArrayList<>(nCmpt);
    }
}

VectorSpace<String, Integer> vc = new VectorSpace<>(3);
Misha
  • 27,433
  • 6
  • 62
  • 78