跳至内容
深圳市捷比信实业有限公司 · 产品选型与采购咨询联系业务人员
超小封装电容捷比信 · 专业器件主题站

Java 8中有趣而酷的技巧

实现注释大多数开发人员认为注释将永远不会在程序中执行,并用于帮助理解代码。但是,可以执行以下命令:public& class& nbsp; Main& nbsp; {& nbsp;& nbsp;& nbsp; public& nbsp; static& nbsp; void& nbsp; main( String []& nbsp; args)& nbsp; {& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp; ///& nbsp; u000d& nbsp; System.out.println(“ wmyskxz  is& nbsp; awesome!”);& nbsp;& nbsp;& nbsp;& nbsp;}}程序输出:wmyskxz ; nbsp;很棒!说明Java允许使用不带编码的Unicode字符。 Java编译器将此处的Unicode字符u000d解析为新行,因此可以理解,在Unicode字符之后出现的语句将在下一行执行。双括号初始化集合在Java中,集合对象(例如Set / List / Map)没有一种在生命周期内初始化值的简单方法(Java 11支持这种类型的操作)。开发人员要么将值显式传递到集合中,要么为常量集合创建一个静态块。使用双括号初始化,您可以在声明过程中以更少的精力和时间来初始化集合。例如:Set& lt; String& gt;& nbsp; set& nbsp; =& nbsp; new& nbsp; HashSet& lt; String& gt;()& nbsp; {{{& nbsp;& nbsp;& nbsp;& nbsp; add(“ wmyskxz”);& nbsp;& nbsp;& nbsp;& nbsp; add(“ is”);& nbsp;& nbsp; & nbsp;& nbsp; add(“ awesome”);& nbsp;& nbsp;& nbsp;& nbsp; add(“!”);}& nbsp;};系统。 out.println(set);程序输出:[awesome,!,& nbsp; wmyskxz,& nbsp; is]“在Java 11中,您可以使用Set.of(“ wmyskxz”,“ is”,“ awesome” ;,“!”),这里仅讨论Java8。另外,对于ArrayList集合类型仍然有一种简单的初始化方法:Arrays.asList()获取数组的索引以插入number元素。有一个很酷的技巧可以找到可以将请求的元素插入数组的位置:int []& nbsp; arr& nbsp; =& nbsp; new& nbsp; int []  {& 1,& nbsp; 3,& nbsp; 4,& nbsp;},//& nbsp; 2& nbsp; has& to& nbsp; to& nbsp; to& nbsp; to& nbsp;成为insertint& nbsp; =& nbsp; Arrays.binarySearch(arr,& nbsp; 2); System.out.print(“ Element& nbsp; to& nbsp; to& nbsp; be& nbsp;插入位置:””。程序输出:Element可以被插入:1 Description Arrays.binarySearch()是由JDK本身实现的二进制搜索方法。限制是必须对目标数组进行排序(可以使用Arrays.sort()进行排序)。确定数字是偶数还是奇数通常我们将使用num%2 == 0(请记住使用偶数判断而不是奇数判断,因为负数会导致错误..)来判断数字是奇数还是偶数。尽管以下技术并不比上述方法好多少,但在考虑大量数字时效率更高:System.out.println((num& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; quot; 1)& nbsp; == " 0& nbsp;"""""""""""""""""""""""""" ODD")示例:int& nbsp; =& nbsp; 2; System.out.println((num& nbsp;& amp; amp; nbsp; 1)& nbsp; ==& nbsp; 0& ????????????????????????????????? out.println((#& amp;& amp; amp; amp; amp; amp; amp; quot; 1)& nbsp; ==& nbsp; 0& nbsp;"“偶数”& nbsp:& nbsp ::& nbsp ;“ ODD”); // 输出  ODD快速乘以或除以2在二进制中,乘以2表示将所有位向左移动并除以2表示向右移动。 n& nbsp; =  n& nbsp;& lt;& lt;& nbsp; 1& nbsp;& nbsp;& nbsp; //      n& n& n ; nbsp; with& nbsp; 2& nbsp; n& nbsp; =& nbsp; n& nbsp;& gt;& gt;& nbsp; 1;& nbsp;  & nbsp; //& nbsp;除法  n& nbsp; by& nbsp; 2& nbsp;确定素数Java在BigInteger类中具有内置的isProbablePrime()方法。如果此BigInteger可能是质数(可以肯定),则返回true,如果它肯定是复数,则返回false。 BigInteger.valueOf(1235).isProbablePrime(1) 更多..欢迎使用Java-https://www.geeksforgeeks.org/interesting-and-cool-tricks-in-java/?中的参考资料添加有趣和酷技巧? ref = leftbar-rightbar用于竞争性编程的Java技巧(针对Java 8)-https://www.geeksforgeeks.org/java-tricks-competitive-programming-java-8/?ref = rp我特别注意