java - Copy different types of an array in JAVA
问题描述
I want to copy the content stored in a array of one type to another array of different type. On this particular case, i want to copy the content stored in items to itemsPacked.
I have this:
Item[] items = {new Item(2,2,2,"ITEM3","ITEM3")};
ItemPacked[] itemsPacked = new ItemPacked[items.length];
for(int i=0;i<items.length;i++){
itemsPacked[i] = (ItemPacked) items[i];
}
When i try to cast i get: inconvertyble types;cannot cast'packing.Item' to 'packing.ItemPacked'
Item class:
public class Item extends Box implements IItem {
private final String reference;
private String description;
public Item(int lenght, int height, int depth, String reference, String description) {
super(lenght, height, depth);
this.reference = reference;
this.setDescription(description);
}
public String getReference() {
return this.reference;
}
public String getDescription() {
return this.description;
}
public final void setDescription(String description) {
this.description = description;
}
public int hashCode() {
int hash = 7;
return hash;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj == null) {
return false;
} else if (this.getClass() != obj.getClass()) {
return false;
} else {
Item other = (Item)obj;
return Objects.equals(this.reference, other.reference);
}
}
public String toString() {
return super.toString() + "\nItem{reference=" + this.reference + ", description=" + this.description + '}';
}
}
ItemPacked.class
public class ItemPacked implements IItemPacked {
private final IItem item;
private IPosition position;
private Color color;
public ItemPacked(IItem item, IPosition position, Color color) {
this.item = item;
this.setPosition(position);
this.setColor(color);
}
public IItem getItem() {
return this.item;
}
public IPosition getPosition() {
return this.position;
}
public final void setPosition(IPosition position) {
this.position = position;
}
public Color getColor() {
return this.color;
}
public final void setColor(Color color) {
this.color = color;
}
public int hashCode() {
int hash = 5;
return hash;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj == null) {
return false;
} else if (this.getClass() != obj.getClass()) {
return false;
} else {
ItemPacked other = (ItemPacked)obj;
return Objects.equals(this.item, other.item);
}
}
public String toString() {
return "ItemPacked{item=" + this.item + ", position=" + this.position + ", color=" + this.color + '}';
}
}
Is there any another way to do it?
I've already tried this too:
ItemPacked itemsPacked[] = items.clone();
解决方案
How do you expect it to auto convert Item
to ItemPacked
?
try using the following:
for(int i=0;i<items.length;i++){
itemsPacked[i] = new ItemPacked(items[i], `somePosition`, `someColor`);
}
推荐阅读
- elasticsearch - logstash -> Elasticsearch:更新非规范化数据
- mybatis - Mybatis foreach之HashMap
- c++ - map::lower_bound() 和 map::upper_bound 如何在 C++ 中工作?
- javascript - Javascript:循环到任何带有偶数/奇数描述的数字。为什么它只记录用户输入?
- python - 快速创建具有序列号的列表
- javascript - Kotlin XML 解析
- sbt - 如何运行 sbt,评估表达式并离开?
- android - 如何使用 XmlPullParser Android 读取没有 StartTag 或 Endtag 的 XML 标记
- html - 电子邮件不在 Outlook 中居中
- javascript - ReactDOM.render() 与导出默认类?