Home  >  Q&A  >  body text

请问各位java自带类的一些问题

List<List<String>> results = new ArrayList<>();

在java中经常会看到这种写法,定义的类型和new后边的类型不一样,list后还嵌套着list,这个到底是什么意思?

阿神阿神2744 days ago497

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-04-18 10:49:36

    Isn’t this just that every element of List is still a List. I didn’t write generics later because they can be omitted.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:49:36

    After Java 7 or 8, generics can be omitted within the <> when instantiating.
    As for list<list>, it should be similar to a two-dimensional array.
    I wonder if this has been used. Map<string, list<string>> is often used in one-to-many relationships.

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:49:36

    Both of the two people above answered the question incorrectly. . .
    The main question is: 1. Why is the front type List, and the new type at the end is ArrayList? 2. What does it mean that list is nested in list.
    To be honest, this question is something you can come into contact with in the first week of learning object-oriented.
    The declared type of results is List, but its actual type is ArrayList. ArrayList is the implementation class of List, which means a subclass. The specific type of results cannot be determined during program compilation. Instead, during runtime, the type is dynamically bound based on the actual type of results. This is called polymorphism.
    List nested within List means that the elements in this set are still sets one by one.

    reply
    0
  • Cancelreply