當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.util.Arrays.copyOfRange()用法及代碼示例


此方法在原始數組的指定範圍內創建元素的副本。

用法:

public static int[] copyOfRange(int[] original_array, int from_index, int to_index)
  • original_array:要複製的數組
  • from_index:要複製範圍的起始索引
  • to_index要複製範圍的結束索引

使用此方法時要記住的重要事項:


  1. 初始索引(即(from_index)必須在0到original_array.length之內
  2. 最終索引,即(to_index)可以大於或等於原始數組的長度。如果它大於原始數組,則將0分配給索引大於或等於original.length-from的副本。
  3. 返回數組的長度為to-from
  4. 它是Arrays類的一種方法

異常

  • ArrayIndexOutOfBoundsException:如果初始索引即(from_index)
    超出原始數組的範圍
  • IllegalArgumentException:如果form_index> to_index拋出此錯誤
  • NullPointerException :如果原始數組為null,則拋出此錯誤

變化:

copyOfRange(boolean[] original, int from, int to)
copyOfRange(byte[] original, int from, int to)
copyOfRange(char[] original, int from, int to)
copyOfRange(double[] original, int from, int to)
copyOfRange(float[] original, int from, int to)
copyOfRange(int[] original, int from, int to)
copyOfRange(long[] original, int from, int to)
copyOfRange(short[] original, int from, int to)
copyOfRange(T[] original, int from, int to)
copyOfRange(U[] original, int from, int to, Class newType)

讓我們通過一個例子來理解這一點:

// Java program to illustrate  
// copyOfRange method 
import java.util.Arrays; 
class GFG { 
public static void main(String args[]) 
    { 
        int arr[] = { 12, 13, 14, 15, 16, 17, 18 }; 
  
        // to index is within the range 
        int[] copy = Arrays.copyOfRange(arr, 2, 6); 
        for (int i : copy) 
            System.out.print(i + "  "); 
  
        System.out.println(); 
        // to index is out of range 
        // It assigns Zero to all the index out of range 
        int[] copy1 = Arrays.copyOfRange(arr, 4, arr.length + 3); 
  
        for (int i : copy1) 
            System.out.print(i + "  "); 
  
        // It throws IIlegalArgumentException 
        // int[] copy2 = Arrays.copyOfRange(arr, 5, 3); 
  
        // It throws ArrayIndexOutOfBoundsException 
        // int[] copy2 = Arrays.copyOfRange(arr, 10, arr.length + 5); 
    } 
}

輸出:

14  15  16  17  
16  17  18  0  0  0  

讓我們看一下copyOfRange(T [] original,int from,int to)和copyOfRange(U [] original,int from,int to,Class newType)的示例

// Java program to illustrate  
// copyOfRange method 
import java.util.Arrays; 
class GFG { 
    // User defined class 
    static class Employee { 
        int Eid; 
        String Ename; 
  
        // constructor 
    public Employee(int Eid, String Ename) 
        { 
            this.Eid = Eid; 
            this.Ename = Ename; 
        } 
  
        // Overide toString() 
    public String toString() 
        { 
            return Eid + "   " + Ename; 
        } 
    } 
  
    public static void
    main(String args[]) 
    { 
        Employee[] e = { new Employee(10, "geek1"), 
                         new Employee(20, "geek2"), 
                         new Employee(30, "geek3"), 
                         new Employee(40, "geek4"), 
                         new Employee(50, "geek5") }; 
  
        // Illustration of copyOfRange(T[] original, int from, int to) 
        // Working with user defined class 
        Employee[] getCopy_Employees = Arrays.copyOfRange(e, 3, 5); 
        for (Employee e1 : getCopy_Employees) 
            System.out.print(e1.toString() + "  "); 
  
        System.out.println(); 
  
        // IIlustration of copyOfRange(U[] original, int from, int to, Class newType) 
        // In this we store the user defined class Employee into Object class 
        // It Throws ArrayStoreException when we try to copy it in a class 
        // That is not correct 
        Object getcopy[] = Arrays.copyOfRange(e, 1, 3, Object[].class); 
  
        // This throws an error 
        // Number getcopy[] = Arrays.copyOfRange(e, 1, 3, Number[].class); 
  
        for (Object e1 : getcopy) { 
            System.out.print(e1.toString() + "  "); 
        } 
    } 
}

輸出:

40   geek4  50   geek5  
20   geek2  30   geek3  

copyOfvs copyOfRange

copyOf()開始從原始數組的第0個索引開始複製,並複製指定數量的元素
雖然copyOfRange()可以將元素範圍複製到原始數組中。

參考:
docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#copyOfRange-int:A-int-int-



相關用法


注:本文由純淨天空篩選整理自 Java.util.Arrays.copyOfRange() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。