0

i have this class

public class customer {

    int id;
    int status ;
    customer(int value)
    {

        id = value;
        status = 0;

    }
}

i want to create an array consist of 100 objects of customer. how can i pass argument to constractor function of customer class ?

public class barbershop {
    Queue WaitSeat ;
    Queue WaitRoom ;
    static barber [] bb ;
    static customer [] cc;
    barbershop(){
        WaitSeat = new PriorityQueue(4);
        WaitRoom = new PriorityQueue(13);
        bb = new barber[3];
        cc = new customer [100]{1}; // problem !

    }

}

5 Answers 5

4

Create objects within a loop and place them in the array

You need to create each object individually, then place it in the array. These are two seperate operations (although they can be within the same line). A loop will make this far more pleasent.

cc = new customer[100];
for(int i=0;i<cc.length;i++){
    cc[i]=new customer(1);
}

Its the same for objects with no-argument constructors

Although the compiler did not complain I do not believe bb = new barber[3]; does what you think it does. It creates an array large enough to fit 3 references to a Barber object. But it doesn't create those objects, only the the array. At this point bb contains {null, null, null} so you would need to use a similar loop to fill bb with Barber objects.

Java naming convention

It is also convention to use UpperCamelCase for class names. So customer should be Customer and barber should be Barber. Equally variable names should be lowerCamelCase so WaitSeat should be waitSeat and WaitRoom should be waitRoom.

Sign up to request clarification or add additional context in comments.

Comments

1

You confuse array initialization with object initialization.

cc is an array of Customer (note, capitalized here).

In order to access the constructor of Customer and get an instance you would need to populate your array with instances.

To simplify your issue you can do:

  cc = new Customer[100];
  Arrays.fill(cc, new Customer(1));

This will fill your 100-sized array with 100 elements referencing one instance of Customer whose id will be 1.

Word of caution, the instance is shared across 100 elements.

In turn, if you modify one element you "modify the whole array", as shown below.

Self-contained example

public class Main {

    public static void main(String[] args) {
        // initializing array
        Customer[] cc = new Customer[100];
        // filling array
        Arrays.fill(cc, new Customer(1));
        // printing array
        System.out.println(Arrays.toString(cc));
        // changing any array element
        cc[0].setId(2);
        // printing array again
        System.out.println(Arrays.toString(cc));
    }

    static class Customer {
        int id;
        int status;

        Customer(int value) {
            id = value;
            status = 0;
        }
        // invoking this method on any object of the array will update all references
        public void setId(int value) {
            id = value;
        }

        @Override
        public String toString() {
            return String.format("Customer %d", id);
        }
    }
}

Output

[Customer 1, Customer 1, Customer 1, Customer 1, Customer 1, Customer 1, Customer 1, Customer 1, etc...]

[Customer 2, Customer 2, Customer 2, Customer 2, Customer 2, Customer 2, Customer 2, Customer 2, etc...]

2 Comments

This is an interesting alternative. Would it be fair to say that this fills the array with references to the same object. Which may well be what the OP wants, but could be dangerous for mutable objects
@RichardTingle thanks for your comment - I actually realized the wording in my answer was very ambiguous: it's not 100 instances but of course 100 references to 1 instance.
0
cc = new customer [100]

This will create a ref cc holding the 100 Customer refs.

for(int i=0;i>cc.length;i++){
    cc[i]=new customer(1);
}

Comments

0

First, classes should always be written in CamelCase.
Second, I don't think that's possible. You coudl use a for loop like this:

cc = new Customer[100];
for(int i = 0; i < 100; i++) {
    cc[i] = new Customer(i);
}

Comments

0
public class barbershop {
    Queue WaitSeat ;
    Queue WaitRoom ;
    static barber [] bb ;
    static customer [] cc;
    barbershop(){
        WaitSeat = new PriorityQueue(4);
        WaitRoom = new PriorityQueue(13);
        bb = new barber[3];

        for(i=0;i<100;i++)
        {
          cc[i] = new customer (i); 
        }

    }

}

3 Comments

I would say that braceless loops are very dangerous beasts; prone to later bugs when the code is modified
Ok.Added that too.Thanks.
Much nicer. One final thing; you've left out the cc = new customer[100] line, and assuming you stick with an array size of 100 your loop should be for(i=0;i<100;i++) because java arrays are zero indexed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.