日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

[转载] Java:获取数组中的子数组的多种方法

發布時間:2025/3/11 java 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载] Java:获取数组中的子数组的多种方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考鏈接: Java中的數組Array

我的個人博客:zhang0peter的個人博客?

?

Java:從一個數組中創建子數組?

使用Arrays.copyOfRange函數?

Arrays.copyOfRange支持:boolean[], byte[] ,char[],double[],float[],int[],long[]以及泛型的 T[] 使用示例如下:?

import java.util.Arrays;

?

public class hello {

? ? public static void main(String[] args) {

? ? ? ? int[] src = new int[]{1, 2, 3, 4, 5};

? ? ? ? int newArray[] = Arrays.copyOfRange(src, 0, 2);

? ? ? ? for (int i : newArray) {

? ? ? ? ? ? System.out.println(i);

? ? ? ? }

? ? }

}

?

?

官方文檔如下:?

copyOfRange

public static <T> T[] copyOfRange(T[] original,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int from,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int to)

Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.

The resulting array is of exactly the same class as the original array.

?

Parameters:

? ? original - the array from which a range is to be copied

from - the initial index of the range to be copied, inclusive

to - the final index of the range to be copied, exclusive. (This index may lie outside the array.)

Returns:

? ? a new array containing the specified range from the original array, truncated or padded with nulls to obtain the required length

Throws:

? ? ArrayIndexOutOfBoundsException - if from < 0 or from > original.length()

? ? IllegalArgumentException - if from > to

? ? NullPointerException - if original is null

Since:

? ? 1.6

?

使用subList?

對于List來說,可以使用subList獲取子列表 注意:subList返回的是原列表的一個視圖,它所有的操作最終都會作用在原列表上 示例如下:?

import java.util.ArrayList;

?

public class hello {

? ? public static void main(String[] args) {

? ? ? ? // create an empty array list

? ? ? ? ArrayList<String> color_list = new ArrayList<String>();

?

? ? ? ? // use add() method to add values in the list

? ? ? ? color_list.add("White");

? ? ? ? color_list.add("Black");

? ? ? ? color_list.add("Red");

? ? ? ? System.out.println("List of the colors :" + color_list);

?

? ? ? ? //Return portion of the list : fromindex(inclusive)->1,? toindex(exclusive)->3

? ? ? ? ArrayList<String> new_color_list1 = new ArrayList<String>(color_list.subList(1, 3));

? ? ? ? System.out.println("Portion of the list: " + new_color_list1);

? ? }

}

?

?

官方文檔如下:?

public List<E> subList(int fromIndex,

? ? ? ? ? ? ? ? ? ? ? ?int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations.

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

?

? ? ? list.subList(from, to).clear();

?

Similar idioms may be constructed for indexOf(Object) and lastIndexOf(Object), and all of the algorithms in the Collections class can be applied to a subList.

The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

?

Specified by:

? ? subList in interface List<E>

Overrides:

? ? subList in class AbstractList<E>

Parameters:

? ? fromIndex - low endpoint (inclusive) of the subList

? ? toIndex - high endpoint (exclusive) of the subList

Returns:

? ? a view of the specified range within this list

Throws:

? ? IndexOutOfBoundsException - if an endpoint index value is out of range (fromIndex < 0 || toIndex > size)

? ? IllegalArgumentException - if the endpoint indices are out of order (fromIndex > toIndex)

總結

以上是生活随笔為你收集整理的[转载] Java:获取数组中的子数组的多种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。