Java List. Clear() method: removes all elements from the list
•
Java
clear()
Example
public static void main(String[] args){ List<String>list = new ArrayList<String>(); list.add("保护环境"); //向列表中添加数据 list.add("爱护地球"); //向列表中添加数据 list.add("从我做起"); //向列表中添加数据 System.out.println("使用clear方法前,List集合中数据"); for(int i=0;i<list.size();i++){ //通过循环输出列表中的内容 System.out.println(i+":"+list.get(i)); } System.out.println("使用clear方法后,List集合中数据"); list.clear(); //移除所有元素 if(list.size()<=0){ System.out.println("list对象为空!"); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码