How do you add elements to a Java list?

To add elements to a Java list, you can use the add() method. Here is an example code:

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

// 添加元素
list.add("元素1");
list.add("元素2");
list.add("元素3");

In the code above, three elements are added to the list using the add() method. After adding the elements, the size of the list will increase automatically.

广告
Closing in 10 seconds
bannerAds