I have a class that i have created of type StoreItem. It contains:

String, int, double, int

What I want to do is for the user to be able to add more items during runtime. I have tried to convert it to an ArrayList but I get errors telling me that it is a type mismatch so I tried Vectors. But if I do that won't have I have to convert the Vector{i] back into StoreItem before I can manipulate it? Is there anyway for me to be able to just have an array of StoreItems that I can dynamically add to?

thanks,

Daniela

Recommended Answers

All 11 Replies

please post your code

I don't see the multidimensional part. This looks like you're talking about an array of Item objects, where an Item has, let me guess, String name, int stockNumber, double price, int qty?

Whatever it is, an Item can be put in an ArrayList<Item>, which will expand as you need. Or have I misunderstood?

What do u want? To add new item dyanamically at runtime to your class StoreItem or create Objects of StoreItem dyanamically at runtime and add to an ArrayList?
Please point the specific problem area.

I don't see the multidimensional part. This looks like you're talking about an array of Item objects, where an Item has, let me guess, String name, int stockNumber, double price, int qty?

Whatever it is, an Item can be put in an ArrayList<Item>, which will expand as you need. Or have I misunderstood?

To create Objects of storeItem dynamically at runtime and add to an ArrayList.

I didn't have the <StoreItem> - or I had it in the wrong place because now it works. But it gets this error:

CashRegister8.java:36: warning: [unchecked] unchecked conversion
found : java.util.ArrayList
required: java.util.ArrayList<CheaperStoreItem>
private static ArrayList<CheaperStoreItem> itemArray = new ArrayList();

^

is that ignorable?

how do I access the individual parts?

CashRegister8.java:72: array required, but java.util.ArrayList<CheaperStoreItem>
found
int tempInt = itemArray.getQuantity();
^

thanks

warning: [unchecked] unchecked conversion

private static ArrayList<CheaperStoreItem> itemArray = new ArrayList();
vs 
private static ArrayList<CheaperStoreItem> itemArray = new ArrayList<CheaperStoreItem>();

int tempInt = itemArray.getQuantity();

What is the definition of itemArray?
Is it an array?
Or is it an ArrayList<CheaperStoreItem> object?
The variable ending with the [] makes the compiler look for an array definition.

There are methods to use to get elements of an ArrayList. Read the API doc to see what one you need to use.

It's

private static ArrayList<CheaperStoreItem> itemArray = new ArrayList<CheaperStoreItem>();


I understand that what I want to implement is this:

earray = a.toArray(E[]) //The array parameter should be of the E class. Returns values in that array (or a larger array is allocated if necessary).


but

this code:

CheaperStoreItem[] temp = new CheaperStoreItem[itemArray.size()];
temp = itemArray.toArray(temp);
int tempInt = temp.getNumber();

seems to defeat the purpose of having it in the arrayList in the first place if I have to convert it back to a fixed Array if I want to access the items.

this code:

CheaperStoreItem[] temp = new CheaperStoreItem[itemArray.size()];
    temp = itemArray.toArray(temp);
    int tempInt = temp[i].getNumber();

seems to defeat the purpose of having it in the arrayList in the first place if I have to convert it back to a fixed Array if I want to access the items.

It not only seems to, it actually does defeat the purpose. ArrayList has a get() method that might be what you're looking for, if you're trying to access an item in the ArrayList by its posiiton in the list.

CheaperStoreItem is:

String name, int quantity, double price, int SKU


So my ArrayList itemArray is an array of CheaperStoreItems and I want to access the quantity of the first item. When I used a static array

itemArray.getQuantity() worked

but it doesn't work when it is an ArrayList. If I try that it tells me the methods are either not there or incompatible types. I read the documentation but all the docs and examples I could find were ArrayLists of a single item, not an arrayList of a multiple items. There has to be a way to say "I want element 2 of item 1 in this array".

but it doesn't work when it is an ArrayList.

Right - itemArray is not an array. It's an ArrayList - a totally different thing, which has some similar properties. Arrays, like Strings, are objects which get a little special treatment in the java compiler. They can be initialized with curly brace syntax:

int[] fiveInts = {3,4,1,2,5,};

and you can get at their members with square brackets:

int thisIntEqualsSeven =  fiveInts[0]+fiveInts[1];
fiveInts[4] = thisIntEqualsSeven;

and so forth.

ArrayLists don't get that special treatment - specifically, you can't address their members with the square bracket notation. Look up the ArrayList.get() method and give it a try.

I don't know anything about the internal structure of CheaperStoreItem, so I don't know whether it has a getQuantity() method, or if there's anything else causing you a problem, but I promise you that this is causing you a problem.

Why you are struggling with array of arry
why cant u implement it using Class like these

public class className{

String strName;
int something;
double something;
}

and use this class object in a loop .

Yes, you need to forget about arrays. Don't even try to convert to or from arrays. Eliminate all the [] from your code.
ArrayLists are different and, for your app, better. Just add your objects the ArrayList and get them with myArrayList.get(index). Loop thru them with
for (StoreItem si : myArrayList) { do stuff with each si }.
The StoreItem class (and subclasses) are also NOT an array. Access their data members with accessor methods (get/put - you need to implement these, but they are trivial). So to access the quantity for item 99 use
myArrayList.get(99).getQuantity();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.