目錄
Syntax
範例
Output
Syntaxx
public static int compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)
登入後複製
" >
public static int compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)
登入後複製
In Java 9, there are other overloaded methods of the
#index 
首頁 Java java教程 在Java 9中,Arrays類別新增了哪些新方法?

在Java 9中,Arrays類別新增了哪些新方法?

Aug 20, 2023 pm 06:33 PM

在Java 9中,Arrays类新增了哪些新方法?

Arrays類別可以包含各種操作陣列的方法,並且還包含靜態工廠方法,允許將陣列視為清單。 Java 9為Arrays類別加入了三個重要的方法:Arrays.equals()Arrays.compare()Arrays.mismatch()

Arrays.equal() - 在Java 9中,Arrays.equals()方法加入了幾個重載方法。新方法為提供的兩個陣列新增了fromIndextoIndex參數。這些方法根據它們的相對索引位置檢查兩個陣列的相等性。

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.

Syntaxx

#In the above syntax, the method compares two int arrays

lexicographically over the specified ranges.

Example

<strong>public static int compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)</strong>
登入後複製

Output

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));
   }
}
登入後複製

Example

<strong>0
0
1</strong>
登入後複製

Output

<strong>public static int mismatch(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)</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

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));
   }
}
登入後複製
In the above syntax, the method finds and returns the

relative 

##index 

#index 

#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.###ler range.### ###Example###
<strong>-1
2
-1
2
0</strong>
登入後複製
###Output###rrreee###

以上是在Java 9中,Arrays類別新增了哪些新方法?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致應用無法運行?如何排查和解決? 公司安全軟件導致應用無法運行?如何排查和解決? Apr 19, 2025 pm 04:51 PM

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

如何使用MapStruct簡化系統對接中的字段映射問題? 如何使用MapStruct簡化系統對接中的字段映射問題? Apr 19, 2025 pm 06:21 PM

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

如何優雅地獲取實體類變量名構建數據庫查詢條件? 如何優雅地獲取實體類變量名構建數據庫查詢條件? Apr 19, 2025 pm 11:42 PM

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

如何將姓名轉換為數字以實現排序並保持群組中的一致性? 如何將姓名轉換為數字以實現排序並保持群組中的一致性? Apr 19, 2025 pm 11:30 PM

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

IntelliJ IDEA是如何在不輸出日誌的情況下識別Spring Boot項目的端口號的? IntelliJ IDEA是如何在不輸出日誌的情況下識別Spring Boot項目的端口號的? Apr 19, 2025 pm 11:45 PM

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對像如何安全地轉換為數組? Java對像如何安全地轉換為數組? Apr 19, 2025 pm 11:33 PM

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU數據庫設計:如何兼顧用戶自定義屬性和無屬性商品? 電商平台SKU和SPU數據庫設計:如何兼顧用戶自定義屬性和無屬性商品? Apr 19, 2025 pm 11:27 PM

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名構建查詢條件? 使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名構建查詢條件? Apr 19, 2025 pm 09:51 PM

在使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名以構建查詢條件,是一個常見的難題。本文將針...

See all articles