2021BTAJ 面试真题详解,16 条代码规范建议,快看看自己做到没
前言
再过一周,马上将迎来新的一年,希望大家在 2021 年“牛气冲天,牛年大吉”,一起迎接春暖花开之时。
金三银四马上就到了,很多粉丝朋友私信希望我出一篇面试专题或者分享面试相关的笔记来学习,小编还是相当宠粉的,这不今天就给大家安排上了?(都是干货,错过就是亏。)
下面的面试笔记都是精心整理好免费分享给大家的,希望新朋友和老朋友不要吝啬你的赞和转发。
}
//初始化 list,往 list 中添加元素反例:int[] arr = new int[]{1,2,3,4};List<Integer> list = new ArrayList<>();for (int i : arr){list.add(i);}
//初始化 list,往 list 中添加元素正例:int[] arr = new int[]{1,2,3,4};//指定集合 list 的容量大小 List<Integer> list = new ArrayList<>(arr.length);for (int i : arr){list.add(i);}
//在循环中拼接字符串反例 String str = "";for (int i = 0; i < 10; i++){//在循环中字符串拼接 Java 不会对其进行优化 str += i;}
//在循环中拼接字符串正例 String str1 = "Love";String str2 = "Courage";String strConcat = str1 + str2; //Java 编译器会对该普通模式的字符串拼接进行优化 StringBuilder sb = new StringBuilder();for (int i = 0; i < 10; i++){//在循环中,Java 编译器无法进行优化,所以要手动使用 StringBuildersb.append(i);}
//频繁调用 Collection.contains() 反例 List<Object> list = new ArrayList<>();for (int i = 0; i <= Integer.MAX_VALUE; i++){//时间复杂度为 O(n)if (list.contains(i))System.out.println("list contains "+ i);}
//频繁调用 Collection.contains() 正例 List<Object> list = new ArrayList<>();Set<Object> set = new HashSet<>();for (int i = 0; i <= Integer.MAX_VALUE; i++){//时间复杂度为 O(1)if (set.contains(i)){System.out.println("list contains "+ i);}}
//赋值静态成员变量反例 private static Map<String, Integer> map = new HashMap<String, Integer>(){{map.put("Leo",1);map.put("Family-loving",2);map.put("Cold on the out side passionate on the inside",3);}};private static List<String> list = new ArrayList<>(){{list.add("Sagittarius");list.add("Charming");list.add("Perfectionist");}};
//赋值静态成员变量正例 private static Map<String, Integer> map = new HashMap<String, Integer>();static {map.put("Leo",1);map.put("Family-loving",2);map.put("Cold on the out side passionate on the inside",3);}
private static List<String> list = new ArrayList<>();static {list.add("Sagittarius");list.add("Charming");list.add("Perfectionist");}
public class PasswordUtils {//工具类构造函数反例 private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.class);
public static final String DEFAULT_CRYPT_ALGO = "PBEWithMD5AndDES";
public static String encryptPassword(String aPassword) throws IOException {return new PasswordUtils(aPassword).encrypt();}
public class PasswordUtils {//工具类构造函数正例 private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.class);
//定义私有构造函数来屏蔽这个隐式公有构造函数 private PasswordUtils(){}
public static final String DEFAULT_CRYPT_ALGO = "PBEWithMD5AndDES";
public static String encryptPassword(String aPassword) throws IOException {return new PasswordUtils(aPassword).encrypt();}
//多余异常反例 private static String fileReader(String fileName)throws IOException{
}
//多余异常正例 private static String fileReader(String fileName)throws IOException{
}
//把其它对象或类型转化为字符串反例:int num = 520;// "" + valueString strLove = "" + num;
//把其它对象或类型转化为字符串正例:int num = 520;// String.valueOf() 效率更高 String strLove = String.valueOf(num);
// BigDecimal 反例
BigDecimal bigDecimal = new BigDecimal(0.11D);
// BigDecimal 正例 BigDecimal bigDecimal1 = bigDecimal.valueOf(0.11D);
//返回 null 反例 public static Result[] getResults() {return null;}
public static List<Result> getResultList() {return null;}
public static Map<String, Result> getResultMap() {return null;}
//返回空数组和空集正例 public static Result[] getResults() {return new Result[0];}
public static List<Result> getResultList() {return Collections.emptyList();}
public static Map<String, Result> getResultMap() {return Collections.emptyMap();}
评论