Java学习笔记#03 - IntelliJ IDEA Live Templates

申屠乐池
2023-12-01

IntelliJ IDEA中提供了一组叫做实时模板(live templates)的东西,类似于快捷键,可以实时自动生成自动代码模板,大大提高写代码的速度,其中最常用的当然是psvm和sout了。类似快捷键,你还可以自定义和修改实时模板,非常灵活。

这里列出IntelliJ IDEA默认的实时模板,简要介绍并给出用例。


  • 声明主方法:
    main / psvm →
public static void main(String[] args) {
		
}

  • 打印字符串:
    sout →
System.out.println();

  • 打印格式化字符串:
    souf →
System.out.printf("");

  • 打印当前类和方法名:
    soutm →
System.out.println("helloworld.twoSum");

  • 打印当前方法参数名和值:soutp
public static int[] twoSum(int[] nums, int target) {
//soutp →
System.out.println("nums = " + Arrays.toString(nums) + ", target = " + target);

  • 打印值:soutv
int[] ints = new int[5];
//soutv →
System.out.println("ints = " + ints);

  • 方法引用打印:
    soutc →
System.out::println

  • 打印错误:
    serr →
System.err.printf();

  • 方法引用打印错误:
    serrc →
System.err::println

  • for循环:
    fori →
for (int i = 0; i < ; i++) {
		
	}

  • 增强for循环:
    foreach →
for (:
	 ) {
		
	}

  • 增强for循环:iter
int[] ints = new int[5];
//iter →
for (int anInt : ints) {
		
	}

  • 增强for循环:I(大写i)
int[] ints = new int[5];
//输入ints并选中,ctrl+alt+j或ctrl+alt+t,选择I →
for (int anInt : ints) {
		
	}

  • 迭代数组元素:itar
int[] ints = new int[5];
//itar →
for (int i = 0; i < ints.length; i++) {
		int anInt = ints[i];
		
	}

  • 倒序迭代数组元素:ritar
int[] ints = new int[5];
//ritar →
for (int i = ints.length - 1; i >= 0; i--) {
		int anInt = ints[i];
		
	}

  • 迭代集合元素:itco
ArrayList<Integer> integers = new ArrayList<>();
//itco →
for (Iterator<Integer> iterator = integers.iterator(); iterator.hasNext(); ) {
		Integer next =  iterator.next();
		
	}

  • 迭代列表元素:itli
ArrayList<Integer> integers = new ArrayList<>();
//itli →
for (int i = 0; i < integers.size(); i++) {
		Integer integer =  integers.get(i);
		
	}

  • 迭代迭代器元素:itit
ArrayList<Integer> integers = new ArrayList<>();
Iterator<Integer> iterator = integers.iterator();
//itit →
while (iterator.hasNext()) {
Integer next =  iterator.next();
		
	}

  • 迭代枚举对象:iten
Vector<String> dayNames = new Vector<>();
dayNames.add("Sunday");
dayNames.add("Monday");
dayNames.add("Tuesday");
Enumeration<String> days = dayNames.elements();
//iten →
while (days.hasMoreElements()) {
String s =  days.nextElement();
	
}

  • 迭代StringTokenizer对象:ittok
String str = "a,b,c,d";
//ittok →
for (StringTokenizer stringTokenizer = new StringTokenizer(str); stringTokenizer.hasMoreTokens(); ) {
String s = stringTokenizer.nextToken();
		
}

  • null条件判断:ifn
String s = "";
//ifn →
if (s == null) {
		
	}

  • not null条件判断:inn
String s = "";
//inn →
if (s != null) {
		
	}

  • 用instanceof检查对象并向下转型:inst
int[] ints = new int[5];
//inst →
if (ints instanceof Object) {
Object o = (Object) ints;
	
}

  • 取数组最后一个值:lst
int[] ints = new int[5];
//lst →
ints[ints.length - 1]

  • 给变量设置一个更小的值:mn
int i = 5;
//mn →
i = Math.min(i, );

  • 给变量设置一个更小的值:mx
int i = 5;
//mx →
i = Math.max(i, );

  • 私有静态最终(类/方法):
    prsf →
private static final

  • 公共静态最终(类/方法):
    psf →
public static final

  • 公共静态最终整型(类/方法):
    psfi →
public static final int

  • 公共静态最终字符串(类/方法):
    psfs →
public static final String

  • 字符串:
    St →
String

  • 将collection对象的元素储存到数组中:toar
ArrayList<Integer> integers = new ArrayList<>();
//toar →
integers.toArray(new Object[integers.size()])

  • 插入单例模式的getinstance方法:geti
public class MySingleton {

    private static MySingleton instance = new MySingleton();
    private MySingleton(){}

    //geti →
	public static MySingleton getInstance() {
        return instance;
    }
}

  • 延迟初始化:lazy
public class MySingleton {

    private static MySingleton instance;
    private MySingleton(){}

    public static MySingleton getInstance() {
        //lazy →
		if (instance == null) {
            instance = new MySingleton();
        }
        return instance;
    }
}

  • 抛出异常:
    thr →
throw new

  • 读锁:RL
ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
//输入//read data并选中,ctrl+alt+j或ctrl+alt+t,选择RL →
reentrantReadWriteLock.readLock().lock();
try {
	//read data
} finally {
	reentrantReadWriteLock.readLock().unlock();
}

  • 写锁:WL
ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
//输入// write data并选中,ctrl+alt+j或ctrl+alt+t,选择WL →
reentrantReadWriteLock.writeLock().lock();
try {
	//write data
} finally {
	reentrantReadWriteLock.writeLock().unlock();
}

  • for循环:数字或变量或数组.fori
    例s.length().fori →
for (int i = 0; i < s.length(); i++) {
		
	}

  • 逆序for循环:数字或变量或数组.forr
    例100.forr →
for (int i = 100; i > 0; i--) {
		
	}

  • 增强for循环:数组变量.for/iter
    例strings.for →
for (String string : strings) {
		
	}

  • 声明变量:new 变量类型.var
    例new String .var →
String s1 = new String;

  • null条件判断:变量.null
    例s.null →
if (s == null) {
		
	}

  • not null条件判断:变量.nn
    例flag.nn →
if (flag != null) {
		
	}

  • 非:布尔类型变量.not
    例flag.not →
!flag

  • if判断:布尔类型变量.if
    例flag.if →
if (flag) {
		
	}

  • 返回:返回值.return
    例s.return →
return s;
 类似资料: