如果我有这段代码:
import javax.swing.*
import java.awt.image.*
def xMap = [
BufferedImage.TYPE_3BYTE_BGR : "3 byte BGR",
BufferedImage.TYPE_4BYTE_ABGR : "4 byte ABGR",
]
IDE将生成此错误:
illegal colon after argument expression; solution: a complex label expression before a colon must be parenthesized at
是否有另一种解决方案,而不是写:
def type_3byte_bgr = BufferedImage.TYPE_3BYTE_BGR
所有常数?
答案 0 :(得分:25)
映射文字要求其键是有效标识符或在括号中。这应该有效:
def xMap = [
(BufferedImage.TYPE_3BYTE_BGR) : "3 byte BGR",
(BufferedImage.TYPE_4BYTE_ABGR) : "4 byte ABGR",]
答案 1 :(得分:2)
错误消息告诉您如何解决它:括号标记表达式。
import java.awt.image.BufferedImage
def xMap = [
(BufferedImage.TYPE_3BYTE_BGR) : "3 byte BGR",
(BufferedImage.TYPE_4BYTE_ABGR) : "4 byte ABGR",
]
println xMap[BufferedImage.TYPE_3BYTE_BGR]