« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9702795 建立时间:2004年12月20日 |

| |
[java语言]List的toArray和ArraysasLIst使用 原创空间, 软件技术
邢红瑞 发表于 2005/9/23 18:00:31 |
最近确实忙晕了,发现sun的javadoc有时说的不明白,我用spring的jdcbtemplate读取字符串, List ls= getJdbcTemplate().query(sql, new Object[]{ccode},new RowMapper(){ public Object mapRow(ResultSet rs, int currentRowNum) throws java.sql.SQLException { return rs.getString("bkid"); } } );想得到一个String[], return (String[])ls.toArray();马上抛出ClassCastException,马上想到以前遇到过这个问题,Object [] a = ls.toArray(); for(int i=0;i<a.length;i++) System.out.println((String)a[i]); 如果要得到数据类型, return (String[])ls.toArray(new String[0]);如果使用 String [] a = new String[<total size>]; String [] l = (String []) list.toArray(a); 要注意如果这个数组a足够大,就会把数据全放进去,返回的数组也是指向这个数组,数组多余的空间存储的是null对象要是不够大,就申请一个跟参数同样类型的数组,把值放进去,然后返回。
asListpublic static <T> List<T> asList(T... a)
返回一个受指定数组支持的固定大小的列表。(对返回列表的更改会“直接写”到数组。)此方法同 Collection.toArray() 一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。返回的列表是可序列化的,并且实现了 RandomAccess。
此方法还提供了一个创建固定长度的列表的便捷方法,该列表被初始化为包含多个元素: List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
参数:
a - 支持列表的数组。
返回:
指定数组的列表视图。
说的更是不明不白,jdk 1.4对java.util.Arrays.asList的定义,函数参数是Object[]。所以,在1.4中asList()并不支持基本类型的数组作参数。jdk 1.5中,java.util.Arrays.asList的定义,函数参数是Varargs, 采用了泛型实现。同时由于autoboxing的支持,使得可以支持对象数组以及基本类型数组。支持泛型的转化,if (null != obj && obj.length > 0) { return (List<Long>) Arrays.asList((Long[])obj); }但是对于基本类型,数为基本类型的数组时,函数的行为发生了变异:它不是把这个数组转换为List,而是把这个数组整体作为返回List中的第一个元素,要取得转换后的结果,得首先get(0)才行。
例子import java.util.*; public class PrintArray{ public static void main (String [] args) { Integer[] integerArray = new Integer[] { new Integer(1), new Integer(2), new Integer(3) }; List list = Arrays.asList(integerArray); System.out.println(" Contents of List: " + list); /* using a primative array as an argument to Arrays.asList() method */ int [] primativeIntArray = {1,2,3}; List intList = Arrays.asList(primativeIntArray); Object[] objectArray = intList.toArray(); System.out.println(" Object Array Element:0 " + objectArray[0]); /* determining data type */ Object elementObject = intList.get(0); Class elementClass = elementObject.getClass(); System.out.println(" List Element: 0 " + intList.get(0)); System.out.println(" Data Type for Element: 0 " + elementClass.getSimpleName()); int [] temp = (int[]) intList.get(0); System.out.println(" hashcode of int [] array " + temp ); System.out.println(" temp int [] array element: 0" + temp[0]); /* trying to access the second element in the List */ elementObject = intList.get(1); // throws an ArrayIndexOutOfBoundsException elementClass = elementObject.getClass(); System.out.println(" List Element: 1 " + intList.get(1)); System.out.println(" Data Type for Element: 1 " + elementClass.getSimpleName()); System.out.println(" Displays List " + intList); }} |
|
回复:List的toArray使用 原创空间, 软件技术
yhard(游客)发表评论于2005/11/17 15:03:00 |
|
回复:List的toArray使用 原创空间, 软件技术
悄悄(游客)发表评论于2005/11/17 13:43:22 |
|
OII 原创空间, 软件技术
OII(游客)发表评论于2005/10/5 11:39:07 |
seen
签名:My Bloghttp://xiangtool.nease.net |
|
» 1 »
|