Java - 从树状图中删除所有元素

方法 clear() 用于删除 Java 中映射结构中的任何元素。调用此方法后,列表 TreeMap 变为空.

package codeurjava.树状图;

导入 java.util.TreeMap;

public class TreeMapCopy {

public static void main(String a[]){
TreeMap hmap = new TreeMap();
//添加键值对等方
hmap.put(1, one”);
hmap.put(2, 二”);
hmap.put(3,三”);
hmap.put(4,四”);
hmap.put(5,五”);
hmap.put(6,六”);

System.out.println(之前的树状图:”+hmap);

System.out.println(空树状图”);
hmap.clear();

System.out.println(树状图后:”+hmap);
}
}
Runtime:

TreeMap before: {1=one, 2=two, 3=three, 4=four, 5=five, 6=six}
Empty TreeMap
TreeMap after: {}

Commentaires (12)

Connectez-vous pour commenter

Rejoignez la discussion et partagez vos connaissances avec la communauté

JD
Jean Dupont Il y a 2 heures

Excellent tutoriel !

👍 12 Répondre Signaler
CodeurJava ✓ Auteur • Il y a 1 heure

N'hésitez pas si vous avez des questions.