Java EnumMap size()
size() 获取EnumMap的元素个数。此方法返回一个整数。
1 语法
public int size()
2 参数
无
3 返回值
返回EnumMap的元素个数。
4 EnumMap size()示例1
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java EnumMap.size的例子
*/
import java.util.*;
public class Demo {
//创建枚举
public enum Tutorial {
Java, CSS, Python, Android, Javascript
};
public static void main(String[] args) {
EnumMap<Tutorial, String> map =
new EnumMap<Tutorial, String> (Tutorial.class);
map.put(Tutorial.Java, "1");
map.put(Tutorial.CSS, "2");
map.put(Tutorial.Python, "3");
map.put(Tutorial.Android, "4");
//输出Map
System.out.println("Map: "+map);
//输出Map的元素个数
System.out.println("Number of mappings: "+map.size());
//添加元素
map.put(Tutorial.Javascript, "5");
//输出Map
System.out.println("Map: "+map);
//输出Map的元素个数
System.out.println("Number of mappings: "+map.size());
}
}
输出结果为:
Map: {Java=1, CSS=2, Python=3, Android=4}
Number of mappings: 4
Map: {Java=1, CSS=2, Python=3, Android=4, Javascript=5}
Number of mappings: 5
5 EnumMap size()示例2
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java EnumMap.size的例子
*/
import java.util.*;
public class Demo {
//创建枚举
public enum Days {
Monday,Tuesday, Wednesday, Thursday
};
public static void main(String[] args) {
EnumMap<Days, String> map =
new EnumMap<Days, String> (Days.class);
map.put(Days.Monday, "1");
map.put(Days.Tuesday, "2");
map.put(Days.Wednesday, "3");
//输出Map
System.out.println("Map: "+map);
//输出Map的元素个数
System.out.println("Number of mappings: "+map.size());
//添加元素
map.put(Days.Thursday, "4");
//输出Map
System.out.println("Map: "+map);
//输出Map的元素个数
System.out.println("Number of mappings: "+map.size());
}
}
输出结果为:
Map: {Monday=1, Tuesday=2, Wednesday=3}
Number of mappings: 3
Map: {Monday=1, Tuesday=2, Wednesday=3, Thursday=4}
Number of mappings: 4
热门文章
优秀文章