如何把java中的arrayListjava数组转化为listoracle中的数组

java list与数组之间的转换详细解析
字体:[ ] 类型:转载 时间:
以下是对java中list与数组之间的转换进行了详细的分析介绍,需要的朋友可以过来参考下
1 数组转换为List调用Arrays类的静态方法asList。
asListpublic static &T& List&T& asList(T... a)Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess. This method also provides a convenient way to create a fixed-size list initialized to contain several elements:
List&String& stooges = Arrays.asList("Larry", "Moe", "Curly");Parameters:a - the array by which the list will be backedReturns:a list view of the specified array
用法:API中提供了一种使用的方法。更为常用的示例代码: 代码如下:String[] arr = new String[] {"str1", "str2"};List&String& list = Arrays.asList(arr);2 List转换为数组这里的List以ArrayList为例,ArrayList的API提供了两种可供使用的函数。
toArraypublic Object[] toArray()Returns an array containing all of the elements in this list in proper sequence (from first to last element). The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
Specified by:toArray in interface Collection&E&Specified by:toArray in interface List&E&Overrides:toArray in class AbstractCollection&E&Returns:an array containing all of the elements in this list in proper sequenceSee Also:Arrays.asList(Object[])
--------------------------------------------------------------------------------toArraypublic &T& T[] toArray(T[] a)Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list. If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
Specified by:toArray in interface Collection&E&Specified by:toArray in interface List&E&Overrides:toArray in class AbstractCollection&E&Parameters:a - the array into which the elements of the list are to be stored, otherwise, a new array of the same runtime type is allocated for this purpose.Returns:an array containing the elements of the listThrows:ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this listNullPointerException - if the specified array is null
用法:示例代码: 代码如下:List&String& list = new ArrayList&String&();list.add("str1");list.add("str2");int size = list.size();String[] arr = (String[])list.toArray(new String[size]);//使用了第二种接口,返回值和参数均为结果
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具Java中List转换为数组,数组转List
今天写代码遇到一个奇怪的问题,具体代码不贴出了,写一个简化的版本。如下:
ArrayList&String& list=new ArrayList&String&();
String strings[]=(String [])list.toArray();
这样写代码个人觉得应该没什么问题,编译也没有问题。可是具体运行的时候报异常,如下:Exception in thread &main& java.lang.ClassCastException: [Ljava.lang.O
但是这么写是没有问题的:
ArrayList&String& list=new ArrayList&String&();
String strings[]=new String[list.size()];
for(int i=0,j=list.size();i&j;i++){
strings[i]=list.get(i);
对于这个现象我们可以这么解释:中允许向上和向下转型,但是这个转型是否成功是根据Java中这个对象的类型来实现的。Java虚拟机中保存了每个对象的类型。而数组也是一个对象。数组的类型是[Ljava.lang.Object。把[Ljava.lang.Object转换成[Ljava.lang.String是显然不可能的事情,因为这里是一个向下转型,而虚拟机只保存了这是一个Object的数组,不能保证数组中的元素是String的,所以这个转型不能成功。数组里面的元素只是元素的引用,不是存储的具体元素,所以数组中元素的类型还是保存在Java虚拟机中的。
根据上面的解释,我们可以把这个问题归纳到下面这个模型:
Object objs[]=new Object[10];
String strs[]=(String[])
这样子和刚才上面编译错误是一样的。如果我们修改一下这个代码,如下:
String strs[]=new String[10];
Object objs[]=
这样子就可以编译通过了。所以这个问题我们可以归结为一个Java转型规则的问题。下面谈一下Java数组对范型的支持问题。
JDK5中已经有了对范型的支持,这样可以保证在集合和Map中的数据类型的安全,可是,List的toArray方法返回的竟然是Object []让人很迷惑。个人感觉应该可以根据范型,直接返回相应的T []。仔细看了一下JDK的发现List转化为array有两个方法:
public Object[] toArray();
这个方法把List中的全部元素返回一个相同大小的数组,数组中的所有元素都为Object类型。
public &T& T[] toArray(T[] a);
这个方法把List中的全部元素返回一个相同大小的数组,数组中的所有元素都为T类型。
List如此设计是因为Java编译器不允许我们new范型数组。也就是说你不能这么定义一个数组:
T arr=new T[size];
但是你却可以用T[]来表示数组,而且可以把数组强制转化为T[]。比如List中的public &T& T[] toArray(T[] a)是这么实现的:
public &T& T[] toArray(T[] a) {
if (a.length & size)
a = (T[])java.lang.reflect.Array.
newInstance(a.getClass().getComponentType(), size);
System.arraycopy(elementData, 0, a, 0, size);
if (a.length & size)
从上面代码中可以看到,因为你不知道这个数组的类型,你必须通过反射机制创建这个数组(a.getClass().getComponentType()方法是取得一个数组元素的类型)。
最终,List转换为Array可以这样处理:
ArrayList&String& list=new ArrayList&String&();
String[] strings = new String[list.size()];
list.toArray(strings);
反过来,如果要将数组转成List怎么办呢?如下:
String[] s = {&a&,&b&,&c&};
List list = java.util.Arrays.asList(s);
摘自& xxf880324本文分析了Stack Overflow上最热门的的一个问题的答案,提问者获得了很多声望点,使得他得到了在Stack Overflow上做很多事情的权限。这跟我没什么关系,我们还是先看看这个问题吧。
这个问题是&在Java中怎样把数组转换为ArrayList?&
Element[] array = {new Element(1),new Element(2),new Element(3)};
1.最流行也是被最多人接受的答案
最普遍也是被最多人接受的答案如下:
ArrayList&Element& arrayList = new ArrayList&Element&(Arrays.asList(array));
首先,我们来看下ArrayList的构造方法的文档。
ArrayList(Collection & ? extends E & c) : 构造一个包含特定容器的元素的列表,并且根据容器迭代器的顺序返回。
所以构造方法所做的事情如下:
1.将容器c转换为一个数组
2.将数组拷贝到ArrayList中称为&elementData&的数组中
ArrayList的构造方法的源码如下:
public ArrayList(Collection&? extends E& c) {
elementData = c.toArray();
size = elementData.
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
2.另外一个流行的答案
另外一个流行的答案是:
List&Element& list = Arrays.asList(array);
这不是最好的,因为asList()返回的列表的大小是固定的。事实上,返回的列表不是java.util.ArrayList,而是定义在java.util.Arrays中一个私有静态类。我们知道ArrayList的实现本质上是一个数组,而asList()返回的列表是由原始数组支持的固定大小的列表。这种情况下,如果添加或删除列表中的元素,程序会抛出异常UnsupportedOperationException。
list.add(new Element(4));
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
at collection.ConvertArray.main(ConvertArray.java:22)
3.又一个解决方案
这个解决方案由Otto提供
Element[] array = {new Element(1), new Element(2)};
List&element& list = new ArrayList&element&(array.length);
Collections.addAll(list, array);
4.该问题的表明的东西
这个问题不难,但却很有趣。每个Java程序员都知道ArrayList,但却很容易犯下这样的错误。我想这就是这个问题很火的原因吧。如果是一个特定领域的Java库的相似的问题,就远不会这样火热了。
这个问题有好多答案提供了相同的解决方案,对于StackOverflow的其他问题也是这样。我猜想当人们想要回答一个问题时,是不会管别人说了什么的。
参考资料:
阅读(...) 评论()ArrayList&String& list=new ArrayList&String&();String strings[]=(String [])list.toArray();
这样写代码个人觉得应该没什么问题,编译也没有问题。可是具体运行的时候报异常,如下:Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.O但是这么写是没有问题的:ArrayList&String& list=new ArrayList&String&();String strings[]=new String[list.size()];for(int i=0,j=list.size();i&j;i++){strings[i]=list.get(i);}对于这个现象我们可以这么解释:Java中允许向上和向下转型,但是这个转型是否成功是根据Java虚拟机中这个对象的类型来实现的。Java虚拟机中保存了每个对象的类型。而数组也是一个对象。数组的类型是[Ljava.lang.Object。把[Ljava.lang.Object转换成[Ljava.lang.String是显然不可能的事情,因为这里是一个向下转型,而虚拟机只保存了这是一个Object的数组,不能保证数组中的元素是String的,所以这个转型不能成功。数组里面的元素只是元素的引用,不是存储的具体元素,所以数组中元素的类型还是保存在Java虚拟机中的。
根据上面的解释,我们可以把这个问题归纳到下面这个模型:Object objs[]=new Object[10];String strs[]=(String[])
这样子和刚才上面编译错误是一样的。如果我们修改一下这个代码,如下:String strs[]=new String[10];Object objs[]=
这样子就可以编译通过了。所以这个问题我们可以归结为一个Java转型规则的问题。下面谈一下Java数组对范型的支持问题。
JDK5中已经有了对范型的支持,这样可以保证在集合和Map中的数据类型的安全,可是,List的toArray方法返回的竟然是Object []让人很迷惑。个人感觉应该可以根据范型,直接返回相应的T []。仔细看了一下JDK的发现List转化为array有两个方法:
public Object[] toArray();这个方法把List中的全部元素返回一个相同大小的数组,数组中的所有元素都为Object类型。
public &T& T[] toArray(T[] a);这个方法把List中的全部元素返回一个相同大小的数组,数组中的所有元素都为T类型。
List如此设计是因为Java编译器不允许我们new范型数组。也就是说你不能这么定义一个数组:T arr=new T[size];
但是你却可以用T[]来表示数组,而且可以把数组强制转化为T[]。比如List中的public &T& T[] toArray(T[] a)是这么实现的:public &T& T[] toArray(T[] a) {if (a.length & size)a = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);System.arraycopy(elementData, 0, a, 0, size);if (a.length & size)a[size] =}
从上面代码中可以看到,因为你不知道这个数组的类型,你必须通过反射机制创建这个数组(a.getClass().getComponentType()方法是取得一个数组元素的类型)。
最终,List转换为Array可以这样处理:
ArrayList&String& list=new ArrayList&String&();
String[] strings = new String[list.size()];
list.toArray(strings);
反过来,如果要将数组转成List怎么办呢?如下:
String[] s = {"a","b","c"};List list = java.util.Arrays.asList(s);
阅读(...) 评论()}

我要回帖

更多关于 java数组转化为json 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信