Using Java generic classes in Matlab

后端 未结 3 1662
慢半拍i
慢半拍i 2021-01-05 07:24

Is it possible to construct a parameterized class in Matlab? For example in Java I could say ArrayList myList = new ArrayList(). I h

相关标签:
3条回答
  • 2021-01-05 08:01

    Kurt is right, however a workaround would be to define your own java class that's not parameterized. public class MyList extends ArrayList<String> { }. Then in matlab you could write myList = MyList() and you would get almost all of the same method signatures as ArrayList<String>.

    0 讨论(0)
  • 2021-01-05 08:02

    You can't instantiate a parametrized Java class in Matlab. This is because Matlab is an interpreted language. So, in your example, when you try

    myList = java.util.ArrayList<String>()
    

    This code is immediately interpreted and run by Matlab (and the Java code compiled). But because Java has Type Erasure all type information for myList is immediately lost. This means in the context of Matlab syntax type parameters make no sense -- so they are syntactically invalid.

    0 讨论(0)
  • 2021-01-05 08:16

    I am not a specialist in Matlab but I understand something in java.

    1. Generics are supported since java 5
    2. Generics are compile time feature. They are also called "erasures".

    It means that

    1. Check which java is installed on your system and used by matlab. Probably it is configured to use java 1.4?
    2. Can you write you code using other IDE (not matlab), compile it their and then use in Matlab? Probably it will fix your problem.

    The following link could probably help you: http://www.mathworks.com/support/solutions/en/data/1-1812J/

    0 讨论(0)
提交回复
热议问题