Mariell
Mariell

Reputation: 21

adding array objects to a list

i'm new to java and I am having problems adding my array objects to my list. I have already checked the topics in this forum but can't find something similar to my problem.

I have already made a class called Item that will save information about an item to be sold, and it has 4 instance variables id, desc, cost, quantity. And then I have a Sales class which has my main method. Basically what I want to do is build an array of objects from my Item class and hard code my data(which I have already created, i'm not sure if I did it right though).

I have created an ArrayList and what I want to do now is add the 5 objects from the array that I created into the list.

This is my Item class

public class Item {


    private String itemID;
    private String desc;
    private double cost;
    private int quantity;

    public Item() {
        itemID="";
        desc="";
        cost=0;
        quantity=0;
    }

    public Item(String id, String d, double c, int q) {
        itemID = id;
        desc = d;
        cost = c;
        quantity = q;

    }

    /**
     * @return the itemID
     */
    public String getItemID() {
        return itemID;
    }

    /**
     * @param itemID the itemID to set
     */
    public void setItemID(String itemID) {
        this.itemID = itemID;
    }

    /**
     * @return the desc
     */
    public String getDesc() {
        return desc;
    }

    /**
     * @param desc the desc to set
     */
    public void setDesc(String desc) {
        this.desc = desc;
    }

    /**
     * @return the cost
     */
    public double getCost() {
        return cost;
    }

    /**
     * @param cost the cost to set
     */
    public void setCost(double cost) {
        this.cost = cost;
    }

    /**
     * @return the quantity
     */
    public int getQuantity() {
        return quantity;
    }

    /**
     * @param quantity the quantity to set
     */
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Item [itemID=" + itemID + ", desc=" + desc + ", cost=" + cost
                + ", quantity=" + quantity + "]";
    }



}

And my Sales class:

import java.util.*;

public class Sales {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int i;

        Item[] items = new Item[5];


        for(i = 0; i < items.length; i++)
        {
            items[i]= new Item(); // create array 
        }

        //hard-coded values of id, desc, cost, qty
        items[0].setItemID("PN250");
        items[1].setItemID("ER100");
        items[2].setItemID("PP150");
        items[3].setItemID("MK200");
        items[4].setItemID("PN300");

        items[0].setDesc("Pen");
        items[1].setDesc("Eraser");
        items[2].setDesc("Paper");
        items[3].setDesc("Marker");
        items[4].setDesc("Pen");

        items[0].setCost(1.50);
        items[1].setCost(1.25);
        items[2].setCost(3.75);
        items[3].setCost(2.50);
        items[4].setCost(2.25);

        items[0].setQuantity(0);
        items[1].setQuantity(175);
        items[2].setQuantity(310);
        items[3].setQuantity(75);
        items[4].setQuantity(450);

        double total = 0;
        for(Item d : items)
        {

            System.out.print(d.getItemID());
            System.out.print("\t" + d.getDesc());
            System.out.print("\t" + d.getCost());
            System.out.println("\t" + d.getQuantity());
        }

        List<Item> obj;
        obj = new ArrayList<Item>();

    }
}

Upvotes: 1

Views: 12128

Answers (3)

molyss
molyss

Reputation: 256

instead of creating an array at the beginning of your function, create an arrayList and add objects to it using the put method.

Also, you should think of making your objects immutables and pass their attributes to their constructor instead of calling the setter for each attribute

Upvotes: 1

Cratylus
Cratylus

Reputation: 54074

You could add the array into your list with the following:

obj.addAll(Arrays.asList(items));

Upvotes: 4

Reimeus
Reimeus

Reputation: 159754

You can use:

for (i = 0; i < items.length; i++)
{
    obj.add(items[i]);
}

but why not add the items as soon as they are created & populated?

Upvotes: 1

Related Questions