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.mzph.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 "…