英文:
Create array of object from an array of int
问题
我显然有点不理解。我有一个原始的`int`数组,我需要将其转换为我的自定义类型的数组。这是我的尝试。
```java
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).mapToObj(StupidInt::new).toArray(StupidInt[]::new);
}
正如你可能预期的那样,myInts
这行代码有个问题,问题是“无法将 StupidInt 转换为 int”。但我不确定要使用哪种组合的 map
、foreach
或其他中间和终端方法来将这个 int
数组转换为我的对象数组。正确的做法是什么?
<details>
<summary>英文:</summary>
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. 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". 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. What is the correct way to do this conversion?
</details>
# 答案1
**得分**: 7
你需要使用`mapToObj`来创建一个新的对象流,而使用`toArray`代替`collect`以获得结果数组。
```java
StupidInt[] myInts = IntStream.of(ints).mapToObj(StupidInt::new).toArray(StupidInt[]::new);
英文:
You need to use mapToObj
to create a new Stream of objects and toArray
instead of collect
to obtain the result as an array.
StupidInt[] myInts = IntStream.of(ints).mapToObj(StupidInt::new).toArray(StupidInt[]::new);
答案2
得分: 2
@hev1的答案简单明了,也是你想要实现的终端方法的最佳答案。
如果你需要不同的解决方案,我在这里提供两个不同的选项。第一个选项只是一个中间方法,用于创建原始数组;第二个选项是一种完全不同的问题解决方法,它不创建原始数组,而是创建一个反映原始类型的“列表”(Object类型),并且也适用于任何类型或对象:
解决方案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]);
}
解决方案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);
}
}
在这种情况下,要添加新的整数,你可以使用add方法,要获取一个整数,可以使用get(position)方法,或者使用find方法查找是否存在并获取位置。
或者你可以直接使用ArrayList类,或者如果你愿意,我可以分享一个能够实现你所需功能的列表结构,它拥有大多数你能想到的方法,实现了Comparable接口,可以排序,实现了Iterable接口,并且非常高效。
英文:
The answer from @hev1 is simple and the best answer for a terminal method for what you are trying to achieve.
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:
Solution 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:
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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论