idea提示那些事

在用idea编程过程中会产生许多worning提示,细细查看这些提示,会得到许多好处:
- 优化代码,解除潜在bug
- 学习相关语言语法
现在把遇到的warning作总结如下。

求Array大小时候建议用length

Replace .size with .length on arrays and strings less
This inspection reports array.size and string.size calls. While such calls are legitimate, they require an additional implicit conversion to SeqLike to be made. A common use case would be calling length on arrays and strings may provide significant advantages.
Before:
Array(1, 2, 3, 4).size
"this is a string".size
After:
Array(1, 2, 3, 4).length
"this is a string".length

在求Array数量的时候建议用length函数提升效率


s字符串中包含变量时不加括号

val ss = "not find"
println(s"The error is ${ss}.")变为
println(s"The error is $ss.")


函数推导式不用加大括号

case _:Exception => {
log.error(s"wrong instruction : $s")
return Map()
}

应该为
case _:Exception =>
log.error(s"wrong instruction : $s")
return Map()


Map取值

Replace with .(key) less... (⌘F1)
Removes unnecessary gets when getting a value from map given a key.
Before:
map.get(k).get
After:
map(k)

直接用key从map中取值


调用无参函数

Java accessor method called as empty-paren less... (⌘F1)
Methods that follow JavaBean naming contract for accessors are expected to have no side effects. The recommended convention is to use a parameterless method whenever there are no parameters and the method have no side effect. This convention supports the uniform access principle, which says that client code should not be affected by a decision to implement an attribute as a field or method. The problem is that Java does not implement the uniform access principle. To bridge that gap, Scala allows you to leave off the empty parentheses on an invocation of function that takes no arguments.
* Refer to Programming in Scala, 10.3 Defining parameterless methods

在scala中调用无参函数或者调用java的无参函数都不必加括号


<think>嗯,用户想解决IntelliJ IDEA的乱码问题。我之前在系统指令里看到了几个引用,需要参考一下。首先,用户的问题是关于IDEA提示乱码,可能涉及到项目代码、控制台输出或者运行时的乱码。根据引用[1],解决方案包括设置文件编码、调整编译器参数和Tomcat配置。引用[2]提到全局和项目编码设置为UTF-8,引用[3]则提到修改encodings.xml文件。 我需要把这些步骤整理清楚,分点说明。第一步应该是检查文件编码设置,确保全局和项目编码都是UTF-8。然后,控制台乱码可能需要设置额外的编译器参数,比如-encoding utf-8。对于Tomcat的情况,VM选项添加-Dfile.encoding=UTF-8也很重要。另外,安装目录下的vmoptions文件添加编码参数可能也是一个关键步骤,引用[1]和[3]都有提到。 还要注意用户可能遇到的问题是否涉及不同场景,比如main函数、Tomcat运行或者测试用例。每个情况对应的解决方法可能不同,需要分别说明。此外,字体问题也可能导致乱码,虽然引用里没提到,但有时候字体不支持某些字符也会显示乱码,所以加上检查字体设置的建议可能更全面。 最后,用户可能需要终极解决方案,比如修改安装目录的配置文件,确保所有地方都统一使用UTF-8。同时,提醒用户修改后重启IDEA,确保设置生效。这样分步骤、分场景的解答应该能帮助用户系统地解决乱码问题。</think>### IntelliJ IDEA 乱码问题解决方案 #### 一、基础编码设置 1. **全局与项目编码设置** - 打开 `File > Settings > Editor > File Encodings` - 将 `Global Encoding` 和 `Project Encoding` 均设置为 `UTF-8` - 确保 `Default encoding for properties files` 也为 `UTF-8`(需勾选 `Transparent native-to-ascii conversion`)[^2] 2. **配置文件强制编码** -IDEA安装目录中找到 `idea.exe.vmoptions`(64位系统为 `idea64.exe.vmoptions`) - 文件末尾添加 `-Dfile.encoding=UTF-8`[^1] #### 二、控制台乱码场景化解决 1. **Main方法控制台乱码** - 打开 `Settings > Build,Execution,Deployment > Compiler > Java Compiler` - 在 `Additional command line parameters` 中添加 `-encoding utf-8`[^1] 2. **Tomcat控制台乱码** - 打开 `Run/Debug Configurations` - 选择Tomcat配置,在 `Server > VM options` 添加 `-Dfile.encoding=UTF-8` 3. **测试用例控制台乱码** - 打开 `Help > Edit Custom VM Options` - 添加 `-Dfile.encoding=UTF-8` 并重启IDEA #### 三、终极解决方案 1. **修改安装目录配置文件** - 找到IDEA安装路径下的 `bin/idea.properties` - 添加或修改 `-Dfile.encoding=UTF-8` - 同步修改 `encodings.xml` 确保内容为: ```xml <?xml version="1.0" encoding="UTF-8"?> <project version="4"></project> ```[^3] 2. **系统环境检查** - Windows系统需检查区域语言设置是否支持Unicode - 确保操作系统与IDEA使用相同编码体系 #### 四、字体兼容性验证 - 打开 `Settings > Editor > Font` - 选择支持中文的字体(如 `Microsoft YaHei` 或 `SimSun`) ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值