Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将字符串数组追加到Integer类型的arraylist_Java_Arrays_Arraylist - Fatal编程技术网

在Java中将字符串数组追加到Integer类型的arraylist

在Java中将字符串数组追加到Integer类型的arraylist,java,arrays,arraylist,Java,Arrays,Arraylist,我有一个名为节点的数组列表。我想将数组[]中的元素分配给ArrayList中的元素。这样arraylist中的第一个元素将数组中的第一个元素作为其属性。。等等然而,数组中只有6个元素,因此对于第7个元素,它也是数组[]中的第一个元素。数组的类型为整数 public static String[] Interest = new String[] {"I1","I2","I3","I4","I5","I6"}; public static void main(String[] args){

我有一个名为
节点的
数组列表
。我想将数组[]中的元素分配给ArrayList中的元素。这样arraylist中的第一个元素将数组中的第一个元素作为其属性。。等等然而,数组中只有6个元素,因此对于第7个元素,它也是数组[]中的第一个元素。数组的类型为整数

public static String[] Interest = new String[] {"I1","I2","I3","I4","I5","I6"};

public static void main(String[] args){
    System.out.println("Enter number of nodes");
    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();
    for(int i=0;i<=n;i++) {
        nodes.add(i);
    }

    System.out.println(nodes);
}

听起来您需要一个带有
映射的
节点
类来存储首选项

class Node {

    Integer id;
    Map<String, Integer> interestPreferences;

    public Node(Integer id) {
        this.id = id;
        this.interestPreferences = new HashMap<String, Integer>();
    }

    void setPreference(String key, Integer value) {
        interestPreferences.put(key, value);
    }

    Interest getPreference(String key) {
        return interestPreferences.get(key);
    }
}
类节点{
整数id;
地图兴趣参考;
公共节点(整数id){
this.id=id;
this.interestPreferences=new HashMap();
}
void setPreference(字符串键,整数值){
interestPreferences.put(键,值);
}
兴趣获取首选项(字符串键){
returninterestreferences.get(key);
}
}
然后像这样使用它:

public static void setInterest(List<String> array){

    String[] Interest = new String[]{"I1","I2","I3","I4","I5","I6"};

    for(int k=0;k<array.size();k++){
        array.get(k);
        for(int j=0;j<Interest.length;j++){

        }
    }
}
public static List<Node> nodes = new ArrayList<Node>();
public static String[] Interest = new String[] {"I1","I2","I3","I4","I5","I6"};

public static void main(String[] args){
    System.out.println("Enter number of nodes");
    Scanner sc = new Scanner(System.in);

    int n = sc.nextInt();
    for(int i=0;i<=n;i++) {
        Node node = new Node(i);
        node.setPreference("I1", 10);
        node.setPreference("I2", 8);
        //....
        node.setPreference("I6", 2);
        nodes.add(node);
    }

    System.out.println(nodes);
}
publicstaticlist节点=newarraylist();
公共静态字符串[]兴趣=新字符串[]{“I1”、“I2”、“I3”、“I4”、“I5”、“I6”};
公共静态void main(字符串[]args){
System.out.println(“输入节点数”);
扫描仪sc=新的扫描仪(System.in);
int n=sc.nextInt();

对于(int i=0;我可以发布代码,说明将数组转换为列表的方法不适用于您吗?请提供“节点”声明还有,请澄清预期的情况和实际发生的情况。我已经编辑了问题。请检查。谢谢。所以你有一个节点列表,每个节点都有一个I1的值,一个I2的值,一个I3的值,一个I4的值,一个I5的值,一个I6的值。对吗?你所说的
1st元素的兴趣配置文件I1=10,I2=8…I6=2是什么意思我只是希望当我创建一个类型列表时,我的其他方法也能起作用。谢谢你的回答。