【发布时间】:2021-01-21 10:58:54
【问题描述】:
java代码中的sn-p在这里:
public class MatrixUsingVectors {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
Vector<Vector<Integer> > vec= new Vector<Vector<Integer> >();
for(int i=0;i<3;i++)
{
Vector<Integer> op= new Vector<Integer>(3);
for(int j=0;j<3;j++)
{
op.add(sc.nextInt());
}
vec.add(op);
}
System.out.println(vec);
int [][] ar= new int[vec.size()][3];
vec.copyInto(ar);// this line throws ArrayStoreException
for(int[] c: ar)
{
System.out.println(c);
}
}
}
我想将向量vec 的元素存储在一个名为ar 的二维数组中。
我需要帮助来处理ArrayStoreException 并希望将vec 的元素存储到ar 中。
请帮忙。
【问题讨论】:
-
我猜:该方法假定目标数组具有相同的类型。但 Integer 和 int 不 是同一类型。您可以手动迭代向量,并将 Integer 对象转换为 int 值。但想知道:谁让你首先使用 Vector?
-
为什么不到处使用 List/ArrayList 数据结构中有数据,然后将其转换为数组有什么意义?
-
copyInto throws ArrayStoreException “如果此向量的组件不是可以存储在指定数组中的运行时类型”也许你可以多说一些关于你试图解决的问题然后有人可以就 Vector of Vectors 是否合适提出意见
标签: java vector arraystoreexception