本文介绍了如何在Java中创建向量数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想在Java中使用一个Vector of Integer数组。

So, I want an array of Vector of Integer in Java.

如果我把

Vector<Integer>[] matrix;
matrix = new Vector<Integer>[100];

我得到的不能是汇编错误

I get cannot the compilation error

我应该使用

    matrix = new Vector[100];

相反? (这会发出警告)

instead? (which gives a warning)

或者我应该不使用向量数组而是使用向量向量?

Or should I simply not use an array of vectors and use vector of vector instead?

注意:我不想要Vector<整数>,我想要一个Vector<整数> []创建一个整数矩阵而不使用Integer [] []。

推荐答案

Java简直没有没有任何方法可以创建参数化类型的数组,而无需获取或抑制警告。所以你能得到的最好的是:

Java simply doesn't have any means to create arrays of a parameterized type without getting or suppressing a warning. So the best you can get is this:

@SuppressWarnings("unchecked")
Vector<Integer>[] anArray = (Vector<Integer>[]) new Vector<Integer>[100];

如果完全避免使用数组,可以解决这个问题。即:

You can get around this problem if you avoid arrays entirely. I.e.:

Vector<Vector<Integer>> list = new Vector<Vector<Integer>>(100);

或使用集合类型:

List<List<Integer>> list = new ArrayList<List<Integer>>(100);

这篇关于如何在Java中创建向量数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 05:04