如何在Java中使用默认值初始化Vector? [重复]

问题描述 投票:-1回答:1

我有一个类,我想用默认值初始化一个Vector,就像我在以下代码中所做的那样:

public class CFach {
    static String name;
    static Vector<Integer> Noten = new Vector<Integer>();
    static double schnitt;

    public
    CFach() {}
    CFach(String nname){
        name = nname;
        Noten = {0,0,0,0,0};
    }

但是它不起作用。错误是“数组常量只能在初始化程序中使用”

此尝试也不起作用:

public class CFach {
    static String name;
    static Vector<Integer> Noten = new Vector<Integer>() {0,0,0,0,0};
    static double schnitt;

    public
    CFach() {}
    CFach(String nname){
        name = nname;
    }

我在做什么错?

提前感谢:)

java vector default
1个回答
0
投票

正如JB Nizet正确指定的那样,Vector已过时。但是,如果它是您的旧系统,则可以尝试以下操作:

int yourArray[] = new int[5];   
Vector<Integer> vector = new Vector<Integer>(Arrays.asList(yourArray));

否则,您应该使用

List<Integer> list = new ArrayList<>(Arrays.asList(yourArray));

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.