diff --git a/java/FuckArrays b/java/FuckArrays new file mode 100644 index 0000000000000000000000000000000000000000..77c2813ec19fc7680a52230af877f14b024a8d59 --- /dev/null +++ b/java/FuckArrays @@ -0,0 +1,24 @@ +import java.util.Arrays; +import java.util.List; + +/** + * Arrays.asList() 教你重新做人 + * + * @author shuzheng + * @date 2019/5/27 + */ +public class FuckArrays { + + public static void main(String[] args) { + int[] datas = new int[]{1, 2, 3, 4, 5}; + List list = Arrays.asList(datas); + + // 输出:1 + System.out.println(list.size()); + + list.add(6); + + // 输出:上一步抛出 Exception in thread "main" java.lang.UnsupportedOperationException + System.out.println(list.size()); + } +} diff --git a/java/Split b/java/Split new file mode 100644 index 0000000000000000000000000000000000000000..b2c051986c7c2e387356f735ca6023490ad0f3c7 --- /dev/null +++ b/java/Split @@ -0,0 +1,28 @@ +/** + * 字符串split使用场景比较频繁,此处有一个极易掉坑的操作 + * + * @author shuzheng + * @date 2019/5/27 + */ +public class Split { + + public static void main(String[] args) { + String param1 = "1,2"; + String param2 = "1,2,"; + String param3 = ",1,2,"; + + System.out.println(param1.split(",").length); + System.out.println(param2.split(",").length); + System.out.println(param3.split(",").length); + } + + /* + * 输出结果: + * 2 + * 2 + * 3 + * + * 惊喜不惊喜?结尾的空字符串被丢弃,可使用重载方法 public String [] split (String regex, int limit) 中 limit 参数控制模式应用的次数,-1 则不会丢弃结尾空字符。 + */ + +}