欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于Java中數(shù)組切片的幾種方法(獲取數(shù)組元素)

 更新時(shí)間:2023年05月01日 09:32:01   作者:Smile sea breeze  
這篇文章主要介紹了關(guān)于Java中數(shù)組切片的幾種方法(獲取數(shù)組元素),切片是數(shù)組的一個(gè)引用,因此切片是引用類(lèi)型,在進(jìn)行傳遞時(shí),遵守引用傳遞的機(jī)制,需要的朋友可以參考下

1.問(wèn)題

數(shù)組切片是一種獲取給定數(shù)組的子數(shù)組的方法。假設(shè),a[] 是一個(gè)數(shù)組。它有 8 個(gè)元素,索引從 a[0] 到 a[7]

int a[] = {8, 9, 4, 6, 0, 11, 45, 21}

現(xiàn)在,我們要找到從 a[3] 到 a[6] 的數(shù)組索引的一部分。其中 a[3] 是 startIndex,a[6] 是 endIndex。因此,我們得到以下切片

a[] = {6, 0, 11, 45}

有以下三種方法可以找到數(shù)組的切片:

  1. 通過(guò)復(fù)制元素
  2. 通過(guò)使用 copyOfRange() 方法
  3. 使用 Java 8 流

2.方法

1)通過(guò)復(fù)制元素

  • 首先,我們找到給定數(shù)組的開(kāi)始和結(jié)束索引。
  • 之后,我們創(chuàng)建一個(gè)大小為 (endIndex - startIndex) 的空數(shù)組(切片數(shù)組)。
  • 從給定的數(shù)組中,將元素(從 startIndex)復(fù)制到切片數(shù)組。最后,打印切片數(shù)組。
public class Test {
    public static void main(String[] args) {
        int[] array = {0,1,2,3,4,5,6,7,8,9};
        int startIndex = 3, endIndex = 8;
        int[] slicedArray = getSlice(array, startIndex, endIndex + 1);
        System.out.println("Slice of Array: "+ Arrays.toString(slicedArray));
    }
    public static int[] getSlice(int[] array, int startIndex, int endIndex)
    {
        int[] slicedArray = new int[endIndex - startIndex];
        //將數(shù)組元素從原始數(shù)組復(fù)制到新創(chuàng)建的切片數(shù)組
        for (int i = 0; i < slicedArray.length; i++)
        {
            slicedArray[i] = array[startIndex + i];
        }
        return slicedArray;
    }
}

結(jié)果如下:

Slice of Array: [3, 4, 5, 6, 7, 8]

2)通過(guò)使用 copyOfRange() 方法

  • copyOfRange() 方法屬于 Java Arrays 類(lèi)。
  • 它將數(shù)組的指定范圍復(fù)制到新創(chuàng)建的數(shù)組(切片數(shù)組)中,并從原始數(shù)組返回包含指定范圍的新創(chuàng)建的數(shù)組。
  • 創(chuàng)建數(shù)組切片需要 O(n) 時(shí)間,存儲(chǔ)元素需要 O(n) 空間,其中 n 是結(jié)果數(shù)組的元素?cái)?shù)。
public static int[] copyOfRange(int[] original, int from, int to) 

它拋出以下異常:

  • ArrayIndexOutOfBoundsException:如果 from 小于 0 或者 from 大于指定數(shù)組的長(zhǎng)度。
  • IllegalArgumentException:如果參數(shù) from 大于 to。
  • NullPointerException:如果給定的數(shù)組為空。
public class Test {
    public static void main(String[] args) {
        int[] array = {0,1,2,3,4,5,6,7,8,9};
        int startIndex = 3, endIndex = 8;
        int[] slicedArray = getSlice(array, startIndex, endIndex + 1);
        System.out.println("Slice of Array: "+ Arrays.toString(slicedArray));
    }
    public static int[] getSlice(int[] array, int startIndex, int endIndex)
    {
        int[] slicedArray = Arrays.copyOfRange(array, startIndex, endIndex);
        return slicedArray;
    }
}

結(jié)果如下:

Slice of Array: [3, 4, 5, 6, 7, 8]

3)使用 Java 8 流

通過(guò)使用以下步驟

  • 我們可以使用 Java 8 Stream 找到數(shù)組的切片。
  • 首先,找到 startIndex 和 endIndex 數(shù)組。
  • 使用 range() 方法將元素(在范圍內(nèi))轉(zhuǎn)換為原始流。
  • 使用 map() 方法映射指定數(shù)組中的指定元素。
  • 通過(guò)調(diào)用 toArray() 方法,將映射數(shù)組轉(zhuǎn)換為數(shù)組。 打印切片
public class Test {
    public static void main(String[] args) {
        int[] array = {0,1,2,3,4,5,6,7,8,9};
        int startIndex = 3, endIndex = 8;
        int[] slicedArray = getSlice(array, startIndex, endIndex + 1);
        System.out.println("Slice of Array: "+ Arrays.toString(slicedArray));
    }
    public static int[] getSlice(int[] array, int startIndex, int endIndex)
    {
        //獲取數(shù)組的切片并將其存儲(chǔ)在數(shù)組 slcarray[] 中
        // range() 方法將元素轉(zhuǎn)換為流
        // 使用 lambda 表達(dá)式獲取 int 流的元素
        // 使用 toArray() 方法將映射元素轉(zhuǎn)換為切片數(shù)組
        int[] slcarray = IntStream.range(startIndex, endIndex).map(i -> array[i]).toArray();
        return slcarray;
    }
}

結(jié)果如下:

Slice of Array: [3, 4, 5, 6, 7, 8]

到此這篇關(guān)于關(guān)于Java中數(shù)組切片的幾種方法(獲取數(shù)組元素)的文章就介紹到這了,更多相關(guān)Java 數(shù)組切片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論