写在前面的话
-“虽然我之前没做过xxx,不过用xxx应该很简单。”
-“没做过就别说很简单!”
一、运算符,基本类型和四则运算
1.1 负数的余数,如何优雅地避免数组下标越界
1.2 你确定你的if判断式里是'=='而不是=?
二、类,接口的设计
2.1 参数列表的设计
2.2 类的分类
2.3 用return进行逻辑控制
2.4 灵活地重写某GUI的Action类,用匿名内部类
三、文件,输入输出
3.1 (zz)文件编码,java读写文件时的编码
四、IDE相关
4.1 IDEA中某变量为灰色(Dark主题下)不等价于该变量没有用
------------------------------------------------
-1%3=?
有两种答案:-1和2
java中用的是第一种,为了避免出现-1,抛出ArrayIndexOut的Error,所以获取数组上一个元素应该这么写,
<T> elementBefore(int x) { return array[ (array.length + x - 1) % array.length ]; }避免了使用if语句判读是否越界,优雅。
3个以上同类型变量用数组传参,配合int参数指定数组中各个变量的功能
UI类的焦点控制类应该和UI类放在同一目录下,而不是和调用该UI的类放在同一目录
尤其是actionListenner中实现多种逻辑时,新增一个跳出逻辑只需要加入if (...) {return;}即可不执行之后的代码,优雅。
需求描述:有一个new JButton(new ThisAction(arg1,arg2)),现在需要仅在当前类中修改ThisAction的事件监听逻辑。
new JButton( new ImagePreviewAction(DeviceCurveDialog.this,previewContext) { @Override public void actionPerformed(ActionEvent event) { if (isNameEmpty()) { return; } super.actionPerformed(event); } } )
File f = new File("E:\\eclipse\\workspace\\box\\Data\\2003-3-7-100.inc"); InputStreamReader read = new InputStreamReader (new FileInputStream(f),"UTF-8"); BufferedReader reader=new BufferedReader(read); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } File f = new File("E:\\eclipse\\workspace\\box\\Data\\2003-3-7-100.inc"); InputStreamReader read = new InputStreamReader (new FileInputStream(f),"UTF-8"); BufferedReader reader=new BufferedReader(read);String line; while ((line = reader.readLine()) != null) {System.out.println(line); }
File file = new File("c:\a.test"); Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); writer.write("中文测试"); writer.close();
一般这个时候你用crtl+鼠标左键点击该变量发现没有导航。不要以为这个变量在程序里就没用到了。有一种情况是,该变量初始化后没被调用。但是在初始化的时候,该变量已经和某些逻辑绑定了。
比如说:
private JButton defaultNameButton; ... defaultNameButton = button(cc.xy(3, 1), createDefaultNameAction());