Java ArrayList toArray()方法及示例

ArrayList類的toArray()方法 (ArrayList Class toArray() method)

Syntax:

句法:

    public Object[] toArray();
public T[] toArray(T[] elements);

  • toArray() method is available in java.util package.

    toArray()方法在java.util包中可用。

  • toArray() method is used to convert the given Arraylist to an array or in other words, this method is used to return an array that contains all the elements in this Arraylist.

    toArray()方法用于將給定的Arraylist轉換為數組,換句話說,該方法用于返回包含該Arraylist中所有元素的數組。

  • toArray(T[] elements) method is used to return an array of the runtime type is that of the given array T[], when this Arraylist fits in the given array then the same array will be returned else a new array is allocated is the type of the given array.

    toArray(T [] elements)方法用于返回運行時類型為給定數組T []的數組,當此Arraylist適合給定數組時,將返回同一數組,否則分配新數組給定數組的類型。

  • toArray() method does not throw an exception at the time of returning an array.

    toArray()方法在返回數組時不會引發異常。

  • toArray(T[] elements) method may throw an exception at the time of returning an array.

    toArray(T [] elements)方法可能會在返回數組時引發異常。

    • ArrayStoreException: This exception may throw when the dynamic type of the given array T[] is not a parent type of the dynamic type of element in this Arraylist.ArrayStoreException :如果給定數組T []的動態類型不是此Arraylist中元素的動態類型的父類型,則可能引發此異常。
    • NullPointerException: This exception may throw when the given array is null exists.NullPointerException :當給定數組為null時,可能引發此異常。
  • These are non-static methods, it is accessible with the class object and if we try to access these methods with the class name then we will get an error.

    這些是非靜態方法,可通過類對象進行訪問,如果嘗試使用類名稱訪問這些方法,則會收到錯誤消息。

Parameter(s):

參數:

  • In the first case, toArray(): It does not accept any parameter.

    在第一種情況下, toArray() :它不接受任何參數。

  • In the Second case, toArray(T[] elements):

    在第二種情況下, toArray(T [] elements)

    T[] elements – represents the array to store elements, when it is capable to store else it creates a new array according to its size of the same dynamic type.

    T [] elements –表示要存儲元素的數組,當它能夠存儲其他元素時,它將根據相同動態類型的大小創建一個新數組。

Return value:

返回值:

In the first case, The return type of the method is Object(), it returns an array of Object type that contains all the elements in this Arraylist.

在第一種情況下,該方法的返回類型為Object() ,它返回一個Object類型的數組,其中包含此Arraylist中的所有元素。

In the second case, The return type of the method is T[], it returns an array that contains all the elements of this array.

在第二種情況下,該方法的返回類型為T [] ,它返回一個包含該數組所有元素的數組。

Example:

例:

// Java program to demonstrate the example 
// of void toArray() method of ArrayList
import java.util.*;
public class ToArrayOfArrayList {
public static void main(String args[]) {
// Create an ArrayList with initial capacity
// to store elements
ArrayList < String > arr_l = new ArrayList < String > (10);
String str_l[] = new String[4];
// By using add() method is to add elements
// in the ArrayList
arr_l.add("C");
arr_l.add("C++");
arr_l.add("Java");
arr_l.add("DotNet");
// Display ArrayList 
System.out.println("ArrayList Elements :" + arr_l);
System.out.println();
// Display String Array
for (String s: str_l)
System.out.println("str_l :" + s);
// By using toArray() method is to convert the
// collection to Array
Object[] o = arr_l.toArray();
System.out.println();
// Display ArrayList
for (Object val: arr_l)
System.out.println("arr_l.toArray() : " + val);
// By using toArray(T[]) method is to coipies the
// collection to the given Array
str_l = arr_l.toArray(str_l);
System.out.println();
// Display str_l
for (String val1: str_l)
System.out.println("arr_l.toArray(str_l) : " + val1);
}
}

Output

輸出量

ArrayList Elements :[C, C++, Java, DotNet]str_l :null
str_l :null
str_l :null
str_l :nullarr_l.toArray() : C
arr_l.toArray() : C++
arr_l.toArray() : Java
arr_l.toArray() : DotNetarr_l.toArray(str_l) : C
arr_l.toArray(str_l) : C++
arr_l.toArray(str_l) : Java
arr_l.toArray(str_l) : DotNet

翻譯自: https://www.includehelp.com/java/arraylist-toarray-method-with-example.aspx

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

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

相關文章

小練習:新聞網站、SNS網站圖片播放器jQuery版

新聞網站和sns網站常見的圖片瀏覽器。直接看效果吧&#xff0c;效果預覽&#xff1a;http://u.vps168.com.cn/cos2004/photoView/&#xff0c; ie7、ie8、ff預覽正常&#xff0c;ie6和chrome預覽有一點小問題&#xff0c;研究中 作者&#xff1a;綠色花園 出處&#xff1a;htt…

SQL SERVER的鎖機制(四)——概述(各種事務隔離級別發生的影響)

SQL SERVER的鎖機制&#xff08;一&#xff09;——概述&#xff08;鎖的種類與范圍&#xff09; SQL SERVER的鎖機制&#xff08;二&#xff09;——概述&#xff08;鎖的兼容性與可以鎖定的資源&#xff09; 本文上接SQL SERVER的鎖機制&#xff08;三&#xff09;——概述&a…

如何測定拼色染液中,各染料在不同染色時間時,染液中殘留染料量及織物上吸附上染的染料量?

如何測定拼色染液中,各染料在不同染色時間時,染液中殘留染料量及織物上吸附上染的染料量? 標準答案: 通常依據各染料對特定波長光的吸光度具有加和性;吸光度值符合朗伯比爾定律,吸光度與濃度成正比;吸光度值分光光度計可以讀出;通過聯立方程,可以求出拼色各染料在染液中…

oracle云數據庫 免費的嗎,使用免費的Oracle云服務-創建ATP數據庫

前面我們講了在Oracle的云主機安裝數據庫&#xff0c;雖然現在安裝數據庫已經很簡單了&#xff0c;但是真正要在生產環境使用&#xff0c;還是需要進行很多配置和優化的&#xff0c;這通常需要專業的人員。而ATP(自治事務性數據庫服務)正是Oracle云提供的非常強大的數據庫云服務…

golang判斷結構體為空_如何在Golang中檢查結構是否為空?

golang判斷結構體為空The size of an empty structure is zero in Golang. Here, empty structure means, there is no field in the structure. 在Golang中&#xff0c; 空結構的大小為零。 在此&#xff0c; 空結構表示該結構中沒有字段。 Eg: 例如&#xff1a; Type struc…

智能車復工日記【5】:起跑線的識別與車庫入庫

博主聯系方式: QQ:1540984562 QQ交流群:892023501 群里會有往屆的smarters和電賽選手,群里也會不時分享一些有用的資料,有問題可以在群里多問問。 系列文章 【智能車Code review】—曲率計算、最小二乘法擬合 【智能車Code review】——坡道圖像與控制處理 【智能車Code re…

文本框只允許輸入數字.net/javascript

<input type"text" name"test" onKeyUp"test1.value(this.valuethis.value.replace(/\D/g,).substring(0,6)).substring(0,3)" > <br /><input name"test1" type"text" > 用Up和Down有區別 只能輸入數字、…

jquery全選,jquery全不選,jquery反選

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns"http://www.w3.org/1999/xhtml"> <head> <title>全選&#xff0c;不…

obj[]與obj._Ruby中帶有示例的Array.rassoc(obj)方法

obj[]與obj.Array.rassoc(obj)方法 (Array.rassoc(obj) Method) In this article, we will study about Array.rassoc(obj) method. You all must be thinking the method must be doing something which is related to the insertion of a certain element. It is not as simp…

oracle平均活動會話數,Oracle11g r2的活動會話數問題解決

如果你在Oracle11g r2的活動會話數的實際操作中&#xff0c;你對Oracle11g r2的活動會話數有不解之處時&#xff0c;你可以通過以下的文章對其的實際應用與功能有所了解&#xff0c;以下是文章的具體介紹&#xff0c;望你瀏覽完以下的內容會有所收獲。在Oracle11g r2的活動會話…

請分析比較下列四種染料在相同濃度和相同溫度的水染液中的聚集度大小?

請分析比較下列四種染料在相同濃度和相同溫度的水染液中的聚集度大小? 標準答案: 染料聚集度從大到小的順序為:3〉1〉4〉2 比較1-4染料結構差異,1、2共軛體系較短,3、4共軛體系較長,1、3磺酸基在端部,2、4磺酸基在中間; 染料3共軛體系長,范德華力大,而水溶性磺酸基團…

分割范圍Codeforces Round #181 (Div. 2)

每日一貼,今天的內容關鍵字為分割范圍 標題鏈接 A &#xff0c;B雖然是水題&#xff0c;但要是沒想清晰就易容掉進某個坑里。。 C&#xff1a;預處理出有所可能的和&#xff0c;然后個每個每和分給a b兩個數 D&#xff1a;競賽的時候標題沒看懂&#xff0c;題意就是說在一個n*n…

VC++動態鏈接庫深入淺出(轉)

1.概論  先來闡述一下DLL(Dynamic Linkable Library)的概念&#xff0c;你可以簡單的把DLL看成一種倉庫&#xff0c;它提供給你一些可以直接拿來用的變量、函數或類。在倉庫的發展史上經歷了“無庫&#xff0d;靜態鏈接庫&#xff0d;動態鏈接庫”的時代。 靜態鏈接庫與動態鏈…

智能車復工日記【6】:有bug的模糊PID記錄

目錄 系列文章DYY Warning算法參考鏈接:代碼.c文件.h文件調用方法希望有大佬能幫我指正錯誤之處。以后假如有機會的話再搞這個模糊吧,頭疼!!!系列文章 【智能車Code review】—曲率計算、最小二乘法擬合 【智能車Code review】——坡道圖像與控制處理 【智能車Code review…

車刀 matlab,可轉位車刀的結構

內容介紹原文檔由會員 違規屏蔽12 發布摘 要隨著有限元技術的快速發展&#xff0c;結構優化在航空航天、建筑、汽車、工程機械等領域的結構設計方面應用越來越廣泛。在刀具行業中&#xff0c;新型刀具材料不斷涌現&#xff0c;刀具的結構也在不斷發展&#xff0c;目前針對刀具的…

DtCms.Model.Advertising.cs

using System; namespace DtCms.Model {/// <summary>/// 廣告位實體類Advertising/// </summary>[Serializable]public class Advertising{public Advertising(){ }#region Modelprivate int _id;private string _title;private int _adtype;private string _adre…

分析染色溫度對染色熱力學性能和染色動力學性能的影響?實際生產時,染色溫度選擇應考慮哪些因素?舉例說明?并闡明實現低溫染色的原理。

分析染色溫度對染色熱力學性能和染色動力學性能的影響?實際生產時,染色溫度選擇應考慮哪些因素?舉例說明?并闡明實現低溫染色的原理。 答:染料的結構包括物理結構和化學結構,溫度高低會影響染料結構,影響染料在染浴中的狀態,染料的溶解、聚集、電離等性能,從而影響染…

c++stl和std_std :: replace()函數以及C ++ STL中的示例

cstl和stdC STL std :: replace()函數 (C STL std::replace() function) replace() function is a library function of algorithm header, it is used to replace an old value with a new value in the given range of a container, it accepts iterators pointing to the s…

《Two Dozen Short Lessons in Haskell》(二十)分數

《Two Dozen Short Lessons in Haskell》&#xff08;Copyright © 1995, 1996, 1997 by Rex Page&#xff0c;有人翻譯為Haskell二十四學時教程&#xff0c;該書如果不用于贏利&#xff0c;可以任意發布&#xff0c;但需要保留他們的copyright&#xff09;這本書是學習 Ha…

oracle 查詢不能重復,oracle – 如何防止在選擇查詢中選擇重復行?

我被賦予了從Oracle數據庫中選擇關鍵數據的任務,但我注意到我的select正在返回重復的行.我不需要它們用于我的報告但我不希望它們刪除它們.有人可以幫助只獲取我需要的數據.我嘗試了以下代碼,但這沒有用.SELECT distinct bbp.SUBCAR "Treadwell",bbp.BATCH_ID "…