How to add an element at the end of an array?

前端 未结 6 1610
迷失自我
迷失自我 2021-01-02 10:33

I want to know how to add or append a new element to the end of an array. Is any simple way to add the element at the end? I know how to use a StringBuffer but I don\'t know

6条回答
  •  离开以前
    2021-01-02 10:59

    Arrays in Java have a fixed length that cannot be changed. So Java provides classes that allow you to maintain lists of variable length.

    Generally, there is the List interface, which represents a list of instances of the class T. The easiest and most widely used implementation is the ArrayList. Here is an example:

    List words = new ArrayList();
    words.add("Hello");
    words.add("World");
    words.add("!");
    

    List.add() simply appends an element to the list and you can get the size of a list using List.size().

提交回复
热议问题