Arrays类可以包含各种操作数组的方法,并且还包含静态工厂方法,允许将数组视为列表。Java 9向Arrays类添加了三个重要的方法:Arrays.equals(),Arrays.compare()和Arrays.mismatch()。
Arrays.equal() - 在Java 9中,Arrays.equals()方法添加了几个重载方法。新方法为提供的两个数组添加了fromIndex和toIndex参数。这些方法根据它们的相对索引位置检查两个数组的相等性。
Syntax
<strong>public static boolean equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)</strong>
在上述语法中,如果两个指定的int数组和指定的范围内的元素相等,则该方法返回true。第二个方法对于char数组也是一样的。
示例
import java.util.Arrays; public class CompareArrayTest { public static void arrayEqualsTest() { int[] existRows = {0, 1, 2, 3, 4, 5}; int[] newRows = {3, 4, 5, 1, 2, 0}; System.out.println(<strong>Arrays</strong>.<strong>equals</strong>(existRows, newRows)); System.out.println(<strong>Arrays</strong>.<strong>equals</strong>(existRows, 1, 3, newRows, 3, 5)); System.out.println(<strong>Arrays</strong>.<strong>equals</strong>(existRows, 3, 5, newRows, 0, 2)); } public static void main(String args[]) { CompareArrayTest.arrayEqualsTest(); } }
Output
false true true
Arrays.compare() − In Java 9, few parameters have added to the Arrays.compare() method. With fromIndex/toIndex parameters that are used for relative position comparison.
Syntax
<strong>public static int compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)</strong>
In the above syntax, the method compares two int arrays lexicographically over the specified ranges.
Example
import java.util.Arrays; public class LexicographicalArraysTest { public static void main(String args[]) { LexicographicalArraysTest.compareSliceArraysTest(); } public static void compareSliceArraysTest() { int[] tomMarks = {5, 6, 7, 8, 9, 10}; int[] daisyMarks = {5, 6, 7, 10, 9, 10}; int[] maryMarks = {5, 6, 7, 8}; System.out.println(<strong>Arrays.compare</strong>(tomMarks, 0, 3, daisyMarks, 0, 3)); System.out.println(<strong>Arrays.compare</strong>(tomMarks, 0, 4, maryMarks, 0, maryMarks.length)); System.out.println(<strong>Arrays.compare</strong>(daisyMarks, 0, 4, maryMarks, 0, maryMarks.length)); } }
Output
<strong>0 0 1</strong>
Arrays.mismatch() −In Java 9, there are other overloaded methods of the Arrays.mismatch() method that enables us to find and return the index of the first mismatch between two slices of arrays.
Syntax
<strong>public static int mismatch(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)</strong>
In the above syntax, the method finds and returns the relative index of the first mismatch between two int arrays over the specified range. It returns -1 if no mismatch has found. The index in the range of 0 (inclusive) up to the length (inclusive) of the smaller range.
Example
import java.util.Arrays; public class MismatchMethodTest { public static void main(String[] args) { MismatchMethodTest.mismatchArraysTest(); } public static void mismatchArraysTest() { int[] a = {1, 2, 3, 4, 5}; int[] b = {1, 2, 3, 4, 5}; int[] c = {1, 2, 4, 4, 5, 6}; System.out.println(<strong>Arrays.mismatch</strong>(a, b)); System.out.println(<strong>Arrays.mismatch</strong>(a, c)); System.out.println(<strong>Arrays.mismatch</strong>(a, 0, 2, c, 0, 2)); System.out.println(<strong>Arrays.mismatc</strong><strong>h</strong>(a, 0, 3, c, 0, 3)); System.out.println(<strong>Arrays.mismatch</strong>(a, 2, a.length, c, 2, 5)); } }
Output
<strong>-1 2 -1 2 0</strong>
以上是在Java 9中,Arrays类新增了哪些新方法?的详细内容。更多信息请关注PHP中文网其他相关文章!

JVM的工作原理是将Java代码转换为机器码并管理资源。1)类加载:加载.class文件到内存。2)运行时数据区:管理内存区域。3)执行引擎:解释或编译执行字节码。4)本地方法接口:通过JNI与操作系统交互。

JVM使Java实现跨平台运行。1)JVM加载、验证和执行字节码。2)JVM的工作包括类加载、字节码验证、解释执行和内存管理。3)JVM支持高级功能如动态类加载和反射。

Java应用可通过以下步骤在不同操作系统上运行:1)使用File或Paths类处理文件路径;2)通过System.getenv()设置和获取环境变量;3)利用Maven或Gradle管理依赖并测试。Java的跨平台能力依赖于JVM的抽象层,但仍需手动处理某些操作系统特定的功能。

Java在不同平台上需要进行特定配置和调优。1)调整JVM参数,如-Xms和-Xmx设置堆大小。2)选择合适的垃圾回收策略,如ParallelGC或G1GC。3)配置Native库以适应不同平台,这些措施能让Java应用在各种环境中发挥最佳性能。

Osgi,Apachecommonslang,JNA和JvMoptionsareeForhandlingForhandlingPlatform-specificchallengesinjava.1)osgimanagesdeppedendendencenciesandisolatescomponents.2)apachecommonslangprovidesitorityfunctions.3)

JVMmanagesgarbagecollectionacrossplatformseffectivelybyusingagenerationalapproachandadaptingtoOSandhardwaredifferences.ItemploysvariouscollectorslikeSerial,Parallel,CMS,andG1,eachsuitedfordifferentscenarios.Performancecanbetunedwithflagslike-XX:NewRa

Java代码可以在不同操作系统上无需修改即可运行,这是因为Java的“一次编写,到处运行”哲学,由Java虚拟机(JVM)实现。JVM作为编译后的Java字节码与操作系统之间的中介,将字节码翻译成特定机器指令,确保程序在任何安装了JVM的平台上都能独立运行。

Java程序的编译和执行通过字节码和JVM实现平台独立性。1)编写Java源码并编译成字节码。2)使用JVM在任何平台上执行字节码,确保代码的跨平台运行。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

禅工作室 13.0.1
功能强大的PHP集成开发环境