Click to See Complete Forum and Search --> : Different variable type for a Vector or array? Similar to Structure in C


Christopher
10-04-2001, 06:18 PM
Hello all,

I was wondering how I could define a multi-dimensional Vector or array that
has different variable types? Similar to a Structure in C?

And how I could access the elements via indexing?

For example:

1. I'd like to enter an items' ID (int), description (string), price
(float?) and taxable-state (boolean)

2. User's enter the info and its loaded in the Vector/Array. How is this
done?

3. After all items have been entered, how do loop to display the Vector or
array and their elements?

I understand that the size of an array cannot be changed so I would guess
that Vector would be the best solution, correct? (For the user to keep adding
the data)

thanks all,

Christopher

markn
10-05-2001, 08:31 AM
Use a Vector or Collection(ArrayList, etc.) and an Object

public class Product
{
private int id;
private String desc;
private float price;
private boolean stateTaxable;

public int getId()
{
return id;
}
void setId(int id)
{
this.id = id;
}

// etc - other getters/setters/constructors/methods
}

You can either iterate - productVector.iterator() - or

for (int i = 0; i < productVector.size(); i++){
Product tempProduct = (Product) productVector.elementAt(i);
System.out.println(tempProduct.getID());

}

And, oh yeah, this is all in books like "Beginning Java 2". :)
Mark

"Christopher" <info@asiavoices.com> wrote:
>
>Hello all,
>
>I was wondering how I could define a multi-dimensional Vector or array that
>has different variable types? Similar to a Structure in C?
>
>And how I could access the elements via indexing?
>
>For example:
>
>1. I'd like to enter an items' ID (int), description (string), price
>(float?) and taxable-state (boolean)
>
>2. User's enter the info and its loaded in the Vector/Array. How is this
>done?
>
>3. After all items have been entered, how do loop to display the Vector
or
>array and their elements?
>
>I understand that the size of an array cannot be changed so I would guess
>that Vector would be the best solution, correct? (For the user to keep adding
>the data)
>
>thanks all,
>
>Christopher
>

Paul Clapham
10-05-2001, 11:04 AM
Or alternatively, and better, create an Item object that contains those four
things as instance variables.

PC2

"markn" <mnuttall@nospam.com> wrote in message
news:3bbda80c$1@news.devx.com...
>
> Use a Vector or Collection(ArrayList, etc.) and an Object
>
> public class Product
> {
> private int id;
> private String desc;
> private float price;
> private boolean stateTaxable;
>
> public int getId()
> {
> return id;
> }
> void setId(int id)
> {
> this.id = id;
> }
>
> // etc - other getters/setters/constructors/methods
> }
>
> You can either iterate - productVector.iterator() - or
>
> for (int i = 0; i < productVector.size(); i++){
> Product tempProduct = (Product) productVector.elementAt(i);
> System.out.println(tempProduct.getID());
>
> }
>
> And, oh yeah, this is all in books like "Beginning Java 2". :)
> Mark
>
> "Christopher" <info@asiavoices.com> wrote:
> >
> >Hello all,
> >
> >I was wondering how I could define a multi-dimensional Vector or array
that
> >has different variable types? Similar to a Structure in C?
> >
> >And how I could access the elements via indexing?
> >
> >For example:
> >
> >1. I'd like to enter an items' ID (int), description (string), price
> >(float?) and taxable-state (boolean)
> >
> >2. User's enter the info and its loaded in the Vector/Array. How is this
> >done?
> >
> >3. After all items have been entered, how do loop to display the Vector
> or
> >array and their elements?
> >
> >I understand that the size of an array cannot be changed so I would guess
> >that Vector would be the best solution, correct? (For the user to keep
adding
> >the data)
> >
> >thanks all,
> >
> >Christopher
> >
>