首页 > 解决方案 > 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();

标签: javaarrays

解决方案


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`);
}

推荐阅读