标签:
右键->New->JavaProject 创建项目Java项目右键->New ->Class 创建类右键Run as ->JavaApplication单击项目右键->Properties->JavaCompiler->5或6运行环境的JRE编译的设置
项目右键->Build path->ConfigureBuild path->Libraries->选中要修改的JRE->Edit->Alternate JRE ->Install JRE->需要制定JRE的home目录然后单击安装->Finish->选择自己的JRE即可| 代码 | 说明 |
|---|---|
| Alt+/ | 内容提示 |
| Ctrl+Alt+↓ | 快速的复制当前行 |
| Alt+↑或↓ | 快速一定当前行 |
| Ctrl+Shift+F | 格式化 |
| Ctrl+/ | 单行注释,再次使用取消注释 |
| Ctrl+shift+/ | 多行注释,取消注释Ctrl+shift+\ |
| Ctrl+shift+O | 导包语句,该快捷键一次性可以导入多个包 |
| Ctrl+1 | 快速修复错误,但是该快捷键只能修复小错误 |
Window->preferences->搜索keys->找到要修改的快捷键->解除绑定-> 绑定|
1
|
import java.lang.*|类名 |
|
1
2
3
4
5
6
|
public static void getArea(){ double r = Math.ceil(Math.random() * 10);// 1. 求一个随机的半径 double area = 0.0; // 2. 计算面积 area = Math.PI * r * r; System.out.println("半径是" + r + "的圆形的面积是:" + area);// 3. 打印面积} |
|
1
|
import static java.lang.*|静态成员 |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
import static java.lang.Math.PI;import static java.lang.System.out;import static java.lang.Math.random;import static java.lang.Math.ceil;public class Demo4 { public static void getArea(){ // 求一个任意半径的圆形的面积 double r = ceil(random()*10); double area = 0.0; area = PI*r*r; out.println("半径是"+r+"的圆形的面积是:"+area); }} |
|
1
|
import static java.lang.Math.*; |
|
1
2
3
4
|
修饰符 返回值类型 方法名(参数类型… 变量名 ) 异常的声明{ // 函数体 return;} |
|
1
2
3
4
5
6
7
|
public static long getSum(int... is){ long sum = 0; for (int i = 0; i < is.length; i++) { sum +=is[i]; } return sum;} |
|
1
|
static <T> List<T> asList(T... a) |
| 包装类 | 基本数据类型 |
|---|---|
| Byte | byte |
| Short | short |
| Integer | int |
| Long | long |
| Boolean | boolean |
| Character | char |
| Float | float |
| Double | double |
|
1
2
3
4
|
public static void getField() { System.out.println(Integer.MIN_VALUE); // -2147483648 System.out.println(Integer.MAX_VALUE); // 2147483647} |
|
1
|
long date = 12345678910; // 报错超出long的最大值 |
|
1
2
3
4
5
6
7
8
9
|
public static void getMethod() { // 创建类对象 Integer in1 = new Integer(123); int i1 = in1.intValue(); System.out.println(i1+1); // 转换为字符串 String str1 = in1.toString(); System.out.println(str1+1);} |
|
1
2
3
4
5
6
7
8
|
public static void getMethod2() { // 创建类对象 Integer in1 = new Integer(123); int i1 = in1; // 自动拆箱 System.out.println(i1+1); // 转换为字符串 System.out.println(in1+""+1);} |
|
1
|
list.add(1); //自动装箱 |
|
1
2
3
|
for( 条件的初始化语句; 条件表达式; 循环的增量 ){ // 循环体 break或continue} |
|
1
2
3
4
5
|
public static void printArray(int [] a){ for(int temp:a){ System.out.println(temp); }} |
|
1
2
3
4
5
|
public static void printList(List<String> list){ for (String string : list) { System.out.println(string); }} |
|
1
2
3
4
5
6
7
8
9
10
11
|
public static void printMap(Map<Integer,String> map){ // 想将其转换为实现了Iterable接口的Set类 Set<Map.Entry<Integer, String>> set = map.entrySet(); // 遍历set集合 for(Map.Entry<Integer, String> entry:set){ // 获取entry对象的key和value值 Integer key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"="+value+","); }} |
|
1
2
3
4
5
6
7
|
public static void printList(List<String> list){ for (String string : list) { list.add("eeee"); // 运行错误 System.out.println(string); } System.out.println("遍历中: "+list); } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("aaaa"); list.add("bbbb"); list.add("cccc"); list.add("dddd"); Iterator<String> it = list.iterator(); while(it.hasNext()){ list.add("yyyy"); String str = it.next(); System.out.println(str); } } |
|
1
2
3
|
修饰符 enum 枚举类名{ // 定义枚举的一组值,多个值之间使用逗号进行分隔} |
|
1
2
3
|
修饰符 enum 枚举类名{ // 定义枚举的一组值,多个值之间使用逗号进行分隔} |
D:\>javap GenderCompiled from "Gender.java"|
1
2
3
4
5
6
7
|
public final class Gender extends java.lang.Enum{ public static final Gender MALE; public static final Gender FEMALE; public static Gender[] values(); public static Gender valueOf(java.lang.String); static {};} |
|
1
2
3
4
5
6
7
8
9
|
public enum TrafficLight { // 定义枚举值 RED,GREEN,YELLOW; // 定义成员属性 public String info = "交通灯信息"; public static void main(String[] args) { System.out.println(TrafficLight.GREEN.info); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public enum TrafficLight { // 定义枚举值 RED("红灯"),GREEN("绿灯"),YELLOW("黄灯"); // 定义成员属性 public String info = "交通灯信息"; // 提供构造函数进行属性的初始化 private TrafficLight(String info){ this.info = info; } public static void main(String[] args) { System.out.println(TrafficLight.GREEN.info); System.out.println(TrafficLight.RED.info); System.out.println(TrafficLight.YELLOW.info); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public enum TrafficLight { // 定义枚举值 RED("红灯") { @Override public void showMessage() { System.out.println("红灯停!"); } }, GREEN("绿灯") { @Override public void showMessage() { System.out.println("绿灯行!"); } }, YELLOW("黄灯") { @Override public void showMessage() { 内部枚举每一项都是一个类,所以可以内部匿名类实现抽象方法 System.out.println("你自己看着办!"); } }; // 定义成员属性 public String info = "交通灯信息"; // 提供构造函数进行属性的初始化 private TrafficLight(String info) { this.info = info; } // 提供一个表明灯的信息的方法 public abstract void showMessage(); 定义抽象方法} |
| 常用方法 | 方法的描述 |
|---|---|
| String name() | 获取枚举值的名称 |
| int ordinal() | 返回枚举值定义的序号 |
| valueOf(Class<T> enumType, String name) | 通过反射查找指定的类中的指定名的枚举值 |
| values() | 该方法在API中不可见但是可用 |
|
1
2
3
4
5
6
|
public static void printList(List list){ for(Object temp:list){ String str = (String) temp; System.out.println(temp); }} |
|
1
2
3
4
5
6
7
|
public static void main(String[] args) { List list = new ArrayList(); list.add("aaa"); list.add(123); list.add("bbb"); printList(list); } |
标签:
原文地址:http://www.cnblogs.com/lindongdong/p/4861866.html