0

I have a Very basic question. How do I need to type parrameters for ArrayList to use the code in BlueJ? I have a method as below.

public void requiresVaccination(int year, ArrayList<Cat>list)

I have created some cat objects cat1, cat2 and cat3.

I thought, I need to type 2012 for theyearand [cat1, cat2, cat3] for the ArrayList but obiusly I am wrong. Could anyone tell me what I need to do, please?

3 Answers 3

1

To invoke the method, pass the year together with a new ArrayList object.

Using double brace initialization, the code for the second argument becomes:

new ArrayList<Cat>() {{ add(cat1); add(cat2); add(cat3); }};

To call the method, use:

requiresVaccination(2012, new ArrayList<Cat>() {{ add(cat1); add(cat2); add(cat3); }});

I think this is the easiest way.

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

Comments

0

You have to give the method a list of Cat Objects, you can't do what you are suggesting.

1 Comment

Hmm, okay then. Will change it.
0

I think it is possible to use a signature like this

public void requiresVaccination(int year, Cat... list)

therefore you can call your methode something like this

 requiresVaccination(2010, cat1, cat2, cat3);

and inside function it is possible to behave with list like an array of Cat (Cat[])

1 Comment

Sorry I am confused still so if I want to test my code. I need to create method call is that right? Then how do I call my method? and how can I use a debugger in BlueJ.

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.