what is the point of heterogenous arrays?

后端 未结 8 1147
忘掉有多难
忘掉有多难 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}, ...};
    
    0 讨论(0)
  • 2020-12-16 14:48

    Eterogenous lists are very useful. For instance, to make the game of snake, I can have a list of blocks like this: [[x, y, 'down'], [x1, y1, 'down']] instead of a class for the blocks, and I can access faster to every element.

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