How do I enter parameters for an ArrayList in BlueJ?

a 夏天 提交于 2019-12-01 05:32:59

You need to create an instance of ArrayList to pass to your method when you call it. With your project open in the main BlueJ window, click on the Tools menu, then on "Use Library Class...", then select java.util.ArrayList from the Class menu. Also select the no-argument constructor from the list that appears, then click Ok.

BlueJ will then display another dialog asking you for a name for the instance and for a type parameter for the ArrayList. Enter a name and Integer for the type parameter.

After you click Ok, the new ArrayList instance will appear in the object bench area at the bottom of the main BlueJ window.

When you right click on the new instance, BlueJ will display a menu of methods that can be called on it. Select the boolean add(Integer) method a few times to add some values to the instance.

Finally, when you right click on your test class and call the toArray method, you can enter the name of the ArrayList instance to pass it as the argument to your method.

The results of the method call are displayed in a dialog.

Click the Inspect button to view the contents of the int array returned from your method, or click the Get button to add it to the object bench.

Arrays.asList("1", "2", "3");

Will return a List and not a ArrayList.

Your methods' parameters should always be the interface and not the implementation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!