Soh Zheng Wei
Soh Zheng Wei

Reputation: 69

adding objects into array for java

I'm new to java and am given a task to construct a class "magazine" with arrays of "supplements".The supplements have a name and cost to it. What i'm trying to figure out is, how to add like say, 3 supplements into an array under the "magazine".

This is the code for "supplements"

public class Supplements {
 String Supplement;
 double cost;

public Supplements ()
{
    Supplement="abcdef";
    cost= 10;

}
public Supplements(String sp, double ct )
{
    Supplement = sp;
    cost=ct;
}

public String getSupplement()
{
    return Supplement;
}
public void setSupplement(String sp)
{
  Supplement=sp;
}
public void setcost(double ct)
{
    cost=ct;
}
public double getcost()
{
    return cost;
}
public String toString()
{
    return ("Supplements: "+ Supplement + "\n" + "Weekly cost: "+ cost);
}

And beginning of "magazine". It's suppose to have cost and array of supplements (s1="abc",10; ,s2="def",11; etc etc)

public class Magazine {
double cost;
Supplements[] supplist;

public Magazine ()
{

    cost=20;
   Supplements[] supplist= new Supplements[1];

I am so sorry if this sounds like gibberish, i have not much experience with coding

Upvotes: 0

Views: 2400

Answers (5)

BugsForBreakfast
BugsForBreakfast

Reputation: 815

I recommend you to use ArrayList instead of Array unless you need or must use Array for this task, if you must use Array then it should be something simple like this:

Supplements sup1 = new Supplements("sup1", 10.0);
Supplements sup2 = new Supplements("sup2", 20.0);
Supplements sup3 = new Supplements("sup3", 30.0);

then you just add to your magazines array but take in count that arrays in java have a fixed size, so lets say that if it has a fixed size of 1 then you can only add 1 supplement like so:

Supplements[] supList = new Supplements[1];
supList[0]=sup1;

Then you set this Array to your magazine attribute.

With ArrayList you don't have to worry about the size, it is dinamically increased.

ArrayList<Supplements> supList= new ArrayList<Supplements>();
supplist.add(new Supplements("sup1", 10));
supplist.add(new Supplements("sup2", 20));
supplist.add(new Supplements("sup3", 30));

And so on, feel free to comment if you have any question.

Upvotes: 0

Niravdas
Niravdas

Reputation: 476

first of all it is not valid Supplements[] supplist= new Supplements[1]; if you already declared Supplements[] supplist; it gives duplicate variable's error


now to add data in array

    Supplements[] supplist= new Supplements[3];
    supplist[0] = new Supplements("abc", 10);
    supplist[1] = new Supplements("def", 20);
    supplist[2] = new Supplements("hij", 30);

and in java Arrays have fix size, it's recommended to use ArrayList's Object instead of Array to support the dynamic size. for example

ArrayList<Supplements> supplist= new ArrayList<Supplements>();
supplist.add(new Supplements("abc", 10));
supplist.add(new Supplements("def", 20));
supplist.add(new Supplements("hij", 30));

you can add dynamic Supplements in your list.

Upvotes: 1

Eduardo
Eduardo

Reputation: 329

If you know a priori how many elements your array will have you can create it with:

Supplements[] arr = new Supplements[n];

where n is the number of elements it will have. Then, you can assign elements to it using the indexes:

arr[0] = new Supplements("abc", 10);
arr[1] = new Supplements("def", 11);

Note here that 0 and 1 are the indexes. You can access until n-1 because your array has size n and the indexes start at 0.

If you don't know how much elements you will have you should consider using data structures from Java Collections, like LinkedList and ArrayList.

Upvotes: 0

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

Arrays have fix size in Java. You have to use ArrayList instead to support the dynamic size.

Upvotes: 2

Shashank Gupta
Shashank Gupta

Reputation: 347

Do something like this

Supplements s1 = new Supplements("abc", 10);
Supplements s2 = new Supplements("def", 20);
Supplements s3 = new Supplements("hij", 30);

Supplements[] supplist= new Supplements[3];
supplist[0] = s1;
supplist[1] = s2;
supplist[2] = s3;

Upvotes: 0

Related Questions