#Error: Incompatible types
18 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @light bramble! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
is Item a generic?
oh you're trying to assign an array into the ArrayBag directly
you need to construct the ArrayBag
is there a way to assign the array without constructing arraybag
here's what im given:
public static final class ArrayBag<T> {
private T[] contents;
private static final int MAX = 25;
private int numberOfEntries;
@SuppressWarnings("unchecked")
public ArrayBag() {
T[] tempBag = (T[])new Object[MAX];
contents = tempBag;
numberOfEntries = 0;
}
public ArrayBag(int desiredCapacity) {
@SuppressWarnings("unchecked")
// Unchecked cast
T[] tempBag = (T[])new Object[desiredCapacity];
contents = tempBag;
numberOfEntries = 0;
} // end constructor
public boolean isEmpty() {
return getCurrentSize() == 0;
}
public boolean add(T anEntry) {
if (anEntry == null) {
return false;
}
if (numberOfEntries < contents.length) {
contents[numberOfEntries] = anEntry;
numberOfEntries++;
return true;
}
else {
return false;
}
}
}
public static class Item{
private String name;
private double price;
private boolean onSale;
public Item(String n, double p, boolean sale) {
name = n;
price = p;
onSale = sale;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public boolean getOnSale() {
return onSale;
}
but I can't access any of the code directly
no
because ArrayBag is not an array
you can't get an instance of any object without constructing it
that's how objects are created
dumb question, but how would i construct it inside this method:
public ArrayBag<Item> addSaleItems(Item[] items){
because thats the only place where i can put my code..
new ArrayBag(items.length) or just new ArrayBag(), up to you
those are the 2 constructors available
ah no way it doesn't have extensions
so just the former
Got it, thanks