本文正在参加「Java主题月 - Java Debug笔记活动」,详情查看活动链接
如何为Spring Boot应用程序配置端口
我enum在Java中有基本和中间方向的说明:
public enum Direction {
NORTH,
NORTHEAST,
EAST,
SOUTHEAST,
SOUTH,
SOUTHWEST,
WEST,
NORTHWEST
}
如何编写一个for循环访问这些enum值的循环
高分回答:
.values()
您可以values()在枚举上调用该方法。
for (Direction dir : Direction.values()) {
// do what you want
}
该values()方法由编译器隐式声明。因此它未在Enum文档中列出。
高分回答:
可以通过调用该public static T[] values()类型的隐式方法来获取枚举类型的所有常量:
for (Direction d : Direction.values()) {
System.out.println(d);
}
文章翻译自 am2dgbqfb6mk75jcyanzabc67y-ac4c6men2g7xr2a-stackoverflow-com.translate.goog/questions/1…
作者建议: 参考我之前我的回答的嵌套枚举模板:如何从Java中的字符串值获取枚举值?
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
真心感谢帅逼靓女们能看到这里,如果这个文章写得还不错,觉得有点东西的话
求点赞👍 求关注❤️ 求分享👥 对8块腹肌的我来说真的 非常有用!!!
如果本篇博客有任何错误,请批评指教,不胜感激 !❤️❤️❤️❤️