📜  Java中的Java .util.Arrays.copyOfRange()

📅  最后修改于: 2022-05-13 01:55:15.037000             🧑  作者: Mango

Java中的Java .util.Arrays.copyOfRange()

此方法在原始数组的指定范围内创建元素的副本。
句法 :

public static int[] copyOfRange(int[] original_array, int from_index, int to_index) 
  • original_array :要从中复制的数组
  • from_index :要复制的范围的起始索引
  • to_end :要复制的范围的结束索引

使用此方法时要记住的要点:

  1. 初始索引 ie(from_index) 必须在 0 到 original_array.length 之间
  2. 最终索引 ie(to_index) 可以大于或等于原始数组的长度。如果它大于原始数组,则将 0 分配给索引大于或等于 original.length – from 的副本。
  3. 返回数组的长度为 will be to-from
  4. 它是 Arrays 类的一种方法

例外

  • ArrayIndexOutOfBoundsException :如果初始索引 ie(from_index)
    超出原始数组的范围
  • IllegalArgumentException :如果 form_index>to_index 则抛出此异常
  • NullPointerException :如果原始数组为空,则抛出此异常

变化:

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
// 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 IllegalArgumentException
        // int[] copy2 = Arrays.copyOfRange(arr, 5, 3);
 
        // It throws ArrayIndexOutOfBoundsException
        // int[] copy2 = Arrays.copyOfRange(arr, 10, arr.length + 5);
    }
}


Java
// 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;
        }
 
        // Override 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();
 
        // Illustration 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() + "  ");
        }
    }
}


输出:

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

// 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;
        }
 
        // Override 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();
 
        // Illustration 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