将列表转换为数组. java.lang.ArrayStoreException [英] Convert list to array. java.lang.ArrayStoreException

查看:70
本文介绍了将列表转换为数组. java.lang.ArrayStoreException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个列表:

List<Integer[]> myList = new ArrayList<Integer[]>();

它包含一个单行条目,但可能包含多个条目:

It contains a sigle entry, but might contain multiple entries:

myList = [[2,null,1,null,null,3,6,1,1]]

我需要将此列表转换为数组Integer[][],但是由于空值而导致转换失败:

I need to convert this list into the array Integer[][], but the conversion fails due to nulls:

Integer[] myArr = myList.toArray(new Integer[myList.size()]);

如何解决此问题?

编辑#1

我需要得到:

myArr = [2,null,1,null,null,3,6,1,1]

推荐答案

尝试一下(假设您实际上已经在评论中谈到了List<Integer[]>):

Try this (assuming you have actually the List<Integer[]> you talked about in your comment):

List<Integer[]> myList = new ArrayList<Integer[]>();
myList.add(new Integer[] {2,null,1,null,null,3,6,1,1} );

Integer[][] myArr = myList.toArray(new Integer[myList.size()][]);

如果将数组列表转换为数组,则会得到一个二维数组,因此您的参数也应该是一个.

If you convert a list of arrays to an array, you'll get a 2 dimensional array and thus your parameter should be one too.

这篇关于将列表转换为数组. java.lang.ArrayStoreException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆