简体   繁体   English

从 int 数组创建 object 数组

[英]Create array of object from an array of int

I'm not understanding something, obviously.很明显,我不明白一些事情。 I've got a primitive array of int s, and I need to convert that into an array of my own type.我有一个原始的int数组,我需要将它转换成我自己类型的数组。 Here's what I've tried.这是我尝试过的。

public class StupidInt {
   @JsonProperty("ID")
   private int id;

   public StupidInt(int id) { this.id = id; }

   public int getId() { return this.id; }
}

public static void main(String []args){
   int[] ints = {1,2,4,67};
   StupidInt[] myInts = IntStream.of(ints).map(StupidInt::new).collect(Collectors.toList());
}

As you might expect, myInts line has a problem and that problem is "cannot convert StupidInt to int".如您所料,myInts 行存在问题,该问题是“无法将 StupidInt 转换为 int”。 But I'm not sure what combination of map or foreach or whatever intermediate and terminal methods to use to convert that array of ints to an array of my object.但我不确定 map 或 foreach 或任何中间和终端方法的组合用于将该整数数组转换为我的 object 数组。 What is the correct way to do this conversion?进行这种转换的正确方法是什么?

You need to use mapToObj to create a new Stream of objects and toArray instead of collect to obtain the result as an array.您需要使用mapToObj创建一个新的 Stream 对象和toArray而不是collect以将结果作为数组获取。

StupidInt[] myInts = IntStream.of(ints).mapToObj(StupidInt::new).toArray(StupidInt[]::new);

The answer from @hev1 is simple and the best answer for a terminal method for what you are trying to achieve. @hev1 的答案很简单,是您尝试实现的终端方法的最佳答案。

I provide here two different options if you need a different solution.如果您需要不同的解决方案,我在这里提供两种不同的选择。 The first is just a for intermediate method to create the primitive array;第一个只是创建原始数组的中间方法; and the second option is a wholesome different approach to the problem and does not create a primitive array but a "list" of the type Object that reflect the primitive type and will also work with any type or object:第二个选项是解决问题的一种有益健康的不同方法,它不会创建原始数组,而是创建类型为 Object 的“列表”,它反映原始类型并且也适用于任何类型或 object:

Solution 1:解决方案1:

int[] ints = {1,2,4,67};
StupidInt[] myInts = new StupidInt[ints.length];
for (int i = 0; i < myInts.length; i++) {
    myInts[i] = new StupidInt(ints[i]);
}

Solution 2:解决方案2:

public class StupidIntList {

    private ArrayList<Integer> ids;

    public StupidIntList() { 
        this.ids = new ArrayList<Integer>();
    }

    public void add(int id) {
        this.ids.add(id);
    }

    public int get(int pos) { 
        return this.ids.get(pos); 
    }

    public boolean findId(int i_d) {
        for (Integer id : ids) {
            if(id == i_d)
                return true;
        }
        return false;
    }

    public String toString() {
        String res = "[";
        for (Integer id : ids) {
            res += id+",";
        }
        return res.substring(0, res.lastIndexOf(","))+"]";
    }

    public static void main(String []args){
        int[] ints = {1,2,4,67};
        StupidIntList myInts = new StupidIntList();
        for (int i = 0; i < ints.length; i++) {
            myInts.add(ints[i]);
        }
        System.out.println(myInts);
    }
}

In this case to add a new int you use the add method and to get one int you will use the get(position) method or the find method to look if it exists and get the position.在这种情况下,要添加一个新的 int,您使用 add 方法并获取一个 int,您将使用 get(position) 方法或 find 方法查看它是否存在并获取 position。

Or you could simply use the ArrayList class or if you want I can share with you a list structure that allows what you need to accomplish, has most methods you can think of, implements comparable, can be ordered, implements iterable and is very efficient.或者您可以简单地使用 ArrayList class 或者如果您愿意,我可以与您分享一个列表结构,它允许您完成您需要完成的工作,拥有您能想到的大多数方法,实现可比性,可以订购,实现可迭代并且非常高效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM