如何在Java中定义公共向量?

时间:2016-02-08 20:55:13

标签: java vector

我试图将矢量矢量从一个类传递到另一个类。对于常规参数和数组,我使用了这样的东西:

public static double[] x= new double[100];

但是,这不适用于矢量或矢量矢量:

public Vector<Vector<Integer>> vec2 = new Vector<Vector<Integer>>();
public Vector<Integer> vec = new Vector<Integer>();

我们如何在其他类中检索向量向量?

1 个答案:

答案 0 :(得分:0)

请考虑以下代码来解决您的实际问题,也许它可以帮助您更好地理解:

    //These are the Integers your vector will contain
    Integer i1 = new Integer(1);
    Integer i2 = new Integer(2);
    Integer i3 = new Integer(3);
    Integer i4 = new Integer(4);

    //This is the vector you're going to insert them into (You could create more than one)
    Vector <Integer> v1 = new Vector<>();

    v1.add(i1);
    v1.add(i2);
    v1.add(i3);
    v1.add(i4);

    //This is the vector of vectors you're going to use to contain your previous vector
    Vector <Vector<Integer>> vectorOfVectors= new Vector<>();

    vectorOfVectors.add(v1);

    //To test out your code
    System.out.println(v1.toString());
    System.out.println(vectorOfVectors.toString());

但请考虑不使用以下帖子中建议的Vector类:Why is Java Vector class considered obsolete or deprecated? 有更好的选项,例如Collection Interface's ArrayList

Java教程提供了有关如何使用集合界面的完整指南。我建议您仔细研究它,因为它对您可能需要的任何类型的集合都非常有用。 Here's the link