[轉載] 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)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540186.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540186.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540186.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[轉載] Java中Array(數組)轉List(集合類)的幾種方法

參考鏈接&#xff1a; Java中的數組類Array 1、循環。新建List類&#xff0c;循環填充。 2、利用Arrays類的靜態方法asList()。 Arrays.asList(T[])返回Arrays類的一個內部內List(T)&#xff0c;此類繼承自AbstractList&#xff0c;不可增刪。若想要一個可以增刪的List類&am…

Linux查看系統cpu個數、核心書、線程數

Linux查看系統cpu個數、核心書、線程數 現在cpu核心數、線程數越來越高&#xff0c;本文將帶你了解如何確定一臺服務器有多少個cpu、每個cpu有幾個核心、每個核心有幾個線程。 查看物理cpu個數 cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l 查看核…

[轉載] java中數組的反射的探究

參考鏈接&#xff1a; Java中的反射數組類reflect.Array 數組的反射有什么用呢&#xff1f;何時需要使用數組的反射呢&#xff1f;先來看下下面的代碼&#xff1a; Integer[] nums {1, 2, 3, 4}; Object[] objs nums; //這里能自動的將Integer[]轉成Object[] Object obj n…

防火墻iptables之常用腳本

防火墻iptables之常用腳本 轉自&#xff1a;http://zhujiangtao.blog.51cto.com/6387416/1286490 標簽&#xff1a;防火墻 主機 1。不允許別人ping我的主機&#xff0c;但是我可以ping別人的主機 #!/bin/bash iptables -F iptables -X iptables -Z modprobe ip_tables modprobe…

[轉載] java中50個關鍵字以及各自用法大全

參考鏈接&#xff1a; Java中的默認數組值 關鍵字和保留字的區別 正確識別java語言的關鍵字&#xff08;keyword&#xff09;和保留字&#xff08;reserved word&#xff09;是十分重要的。Java的關鍵字對java的編譯器有特殊的意義&#xff0c;他們用來表示一種數據類型&…

NFS 共享存儲

服務器客戶端yum -y install rpcbind nfs-utils 服務器 vim /etc/exports /data 192.168.10.0/24(rw,sync,no_root_squash) * ro # 只讀權限 * rw # 讀寫權限 * sync # 同步&#xff0c;數據更安全&#xff0c;速度慢 * async #異步&#xff0c;速度快&#xff0c;效率高&a…

[轉載] Java中的final變量、final方法和final類

參考鏈接&#xff1a; Java中的final數組 &#xff5c; Final arrays 1、final變量 final關鍵字可用于變量聲明&#xff0c;一旦該變量被設定&#xff0c;就不可以再改變該變量的值。通常&#xff0c;由final定義的變量為常量。例如&#xff0c;在類中定義PI值&#xff0c;可…

Linux基礎篇_01_計算機概論

學習資料&#xff1a;《鳥哥的Linux私房菜&#xff08;基礎篇&#xff09;》部分&#xff1a;Linux的規劃與安裝 時間&#xff1a;20130225 學習筆記&#xff1a;計算機定義&#xff1a;接受使用者輸入指令與數據&#xff0c; 經由中央處理器的數學與邏輯單元運算處理后&#x…

[轉載] java中的經典問題:傳值與傳引用

參考鏈接&#xff1a; 有關Java中數組分配的有趣事實 參數傳遞的秘密 知道方法參數如何傳遞嗎&#xff1f; 記得剛開始學編程那會兒&#xff0c;老師教導&#xff0c;所謂參數&#xff0c;有形式參數和實際參數之分&#xff0c;參數列表中寫的那些東西都叫形式參數&#x…

[3/21]Windows Server 2008時鐘方面的改進展示

在Windows Server 2008中的時鐘顯示和以往Windows Server 2003及以前的版本顯示有很大的差別。如果要顯示并進行簡單的時間修改可以在時鐘上雙擊&#xff0c;會出現如下圖所示的界面。在上圖中可以調整但無法進行真正的修改&#xff0c;徹底修改需要點擊&#xff02;更改日期和…

[轉載] 黑馬程序員_學習筆記8_C#基礎歸納之數組

參考鏈接&#xff1a; Java中的鋸齒數組Jagged array ---------------------- Windows Phone 7手機開發、.Net培訓、期待與您交流&#xff01; ---------------------- 什么是數組&#xff1f; 數組是一組數據結構&#xff0c;它可以包含同一類型的多個元素。C#用特殊記號還…

2Python全棧之路系列之MysQl基本數據類型

Python全棧之路系列之MySQL基本數據類型 MySQL中定義數據字段的類型對你數據庫的優化是非常重要的。 MySQL支持多種類型&#xff0c;大致可以分為三類&#xff1a; 數字類型 日期和時間類型 字符串類型 數字類型 類型大小用途BIT-二進制TINYINT1字節小整數值INT or INTEGER4字…

[轉載] JAVA筆記_(Day04,Day05)函數數組

參考鏈接&#xff1a; 了解Java中的數組IndexOutofbounds異常 文章目錄 函數定義練習誤區重載&#xff08;overload&#xff09;重載選擇題練習函數的內存調用問題 數組定義數組的內存圖解數組的常見問題應用求和最大值將數組轉成字符串查表法轉十六進制查表版&#xff08;十六…

VDI序曲二 RemotoAPP部署

首先&#xff0c;我們需要準備如下角色&#xff1a;沿用VDI序曲一的2臺物理服務器以及角色我們在物理服務器1的hyper-v上&#xff0c;我們利用之前我介紹的“服務器虛擬化之準備母盤VHD”的方法再創建如下虛擬機&#xff1a;WIN-RDAPP&#xff1b;WIN-RDWA&#xff1b;WIN-RDCB…

[轉載] Java ArrayList toArray(T[] a) 解惑

參考鏈接&#xff1a; Java中的Array vs ArrayList 先看一個小的代碼片段 ArrayList<Integer> arrayList new ArrayList<>(); Collections.addAll(arrayList, 11, 21, 31, 41, 51); Integer[] a new Integer[0]; Integer[] b new Integer[arrayList.size()]; …

CentOS7使用firewalld打開關閉防火墻與端口(轉載)

1、firewalld的基本使用 啟動&#xff1a; systemctl start firewalld 查看狀態&#xff1a; systemctl status firewalld 停止&#xff1a; systemctl disable firewalld 禁用&#xff1a; systemctl stop firewalld 2.systemctl是CentOS7的服務管理工具中主要的工具&#xff…

多任務管理類 MutilTaskManager

計算和計算所需的數據能被較為平均的非配到若干task的時候&#xff0c;下面的任務管理類可以提供在大數據大計算量的情況下非精確的控制task的執行數量來限制計算量和內存占用量.下面是代碼&#xff08;非線程安全版本&#xff09;&#xff1a; public class MutilTaskManager{…

[轉載] Scanner和bufferreader讀取控制臺字符的區別

參考鏈接&#xff1a; Java中Scanner和BufferReader類之間的區別 從開始學習Java就用了scanner&#xff0c;因為比較簡單每當遇到空格鍵或者換行鍵則讀取下一個字符&#xff0c;一般用法 while(input.hasNextInt()){ int n input.nextInt(); int t input.nextInt(); int c …

Node.js meitulu圖片批量下載爬蟲1.051

原有1.05版程序沒有斷點續傳模式&#xff0c;現在在最近程序基礎上改寫一版1.051. // // meitulu圖片批量下載爬蟲1.051 // 用最近的斷點續傳框架改寫原有1.05版程序 // 2017年11月21日 //// 內置https模塊 var httpsrequire("https");// 內置http模塊 var httprequi…