what is the point of heterogenous arrays?

后端 未结 8 1173
忘掉有多难
忘掉有多难 2020-12-16 13:43

I know that more-dynamic-than-Java languages, like Python and Ruby, often allow you to place objects of mixed types in arrays, like so:

[\"hello\", 120, [\"w         


        
8条回答
  •  眼角桃花
    2020-12-16 14:45

    There is nothing to stop you having a heterogeneous array in Java. It is considered poor programming style and using proper POJOs will be faster/more efficient than heterogeneous arrays in Java or any other language as the types of the "fields" are statically known and primitives can be used.

    In Java you can

    Object[][] array = {{"John Smith", 000}, {"Smith John", 001}, ...};
    

提交回复
热议问题