日韩性视频-久久久蜜桃-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:获取数组中的子数组的多种方法的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 久久久久久久久久久久久久 | 新av在线 | 成人av免费网址 | 国产黄色在线网站 | 污网站免费看 | 国产成人高清视频 | 波多野结衣一区在线 | 日韩免费av片 | 最新国产在线视频 | 欧美激情图区 | 成 年 人 黄 色 大 片大 全 | 日日夜夜免费精品视频 | 狠狠干超碰 | 天天综合色网 | 中文字幕精品一区二区精品 | 黄片毛片av | 欧美亚洲精品一区二区 | 有机z中国电影免费观看 | 国产精品88av | 99久久久无码国产精品6 | 黄色资源在线观看 | 一区二区久久 | 亚洲国产精品福利 | 男人在线视频 | 亚洲三级图片 | 国产精品视频无码 | 总裁憋尿呻吟双腿大开憋尿 | 成人国产一区二区三区精品麻豆 | 黑丝一区 | 精品字幕 | 精品在线观看一区二区 | 欧美自拍偷拍第一页 | 中文在线观看免费 | 免费拍拍拍网站 | 森泽佳奈作品在线观看 | 国产高清一区二区 | 咪咪成人网| 国产精品二区三区 | 91人人澡人人爽人人精品 | 婷婷超碰 | 成人免费黄色大片 | 91精品国产综合久久久久 | 青青国产在线视频 | 精品国产成人 | 国产又黄又大又粗视频 | 欧美日韩一区不卡 | 亚洲女人被黑人巨大进入 | 高清在线一区 | 久久久久无码精品国产sm果冻 | 日本美女a级片 | 国产一二三四在线 | 秋霞欧洲| av2018| 国产精品久久亚洲7777 | 成人精品 | 男人天堂新地址 | 天天爽天天搞 | 精品无码人妻一区二区免费蜜桃 | 伊人91视频 | jzjzjz欧美丰满少妇 | 丝袜av在线播放 | 久久久久成人片免费观看蜜芽 | 91av在线免费观看 | 成人人人人人欧美片做爰 | 国产精品无码影院 | 狂野欧美| 国产精品无码久久久久成人app | 2019天天操| 国产精品欧美精品 | 国产精品成人69xxx免费视频 | 日韩中文在线一区 | 少妇高清精品毛片在线视频 | 一区二区在线视频免费观看 | 亚洲三级色 | 亚洲激情偷拍 | 久久不卡免费视频 | 美女张开腿流出白浆 | 日韩欧美理论 | 玖玖网| 免费视频污| 国产成人黄色 | 中文亚洲字幕 | 一区二区三区四区国产 | 五月伊人婷婷 | 国产美女被遭强高潮免费网站 | 性xxxx视频 | 国产69精品久久久 | 污污网站免费 | 日本xxxxxⅹxxxx69 | 欧美性黄色 | 一区二区三区日韩视频 | 国产香蕉视频在线播放 | 毛片小视频 | 久久综合资源 | 国产精品三区四区 | 精品五月天 | 日韩欧美视频一区二区三区 | 91亚洲在线 | 亚洲在线视频免费观看 |