如何在 BlueJ 中为 ArrayList 输入参数? [英] How do I enter parameters for an ArrayList in BlueJ?

查看:46
本文介绍了如何在 BlueJ 中为 ArrayList 输入参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 BlueJ 中,如果我编写了一个将数组作为参数的方法,那么当我想通过方法调用测试该方法时,我必须用花括号输入元素,因此:

In BlueJ, if I write a method that takes an array as a parameter, then when I want to test that method with a method call I have to enter the elements with curly braces, so:

{1,2,3}

如何调用 ArrayList 的方法?

这是我的代码:

import java.util.*;

public class Test2{

    public static int[] toArray(ArrayList<Integer>a){
        int len = a.size();
        int []b = new int[len];
        for(int i = 0; i<len; i++){
            b[i] = a.get(i);
        }
     return b;    

    }
}

现在想在BlueJ中测试,在下面的对话框中应该输入什么?

Now I want to test it in BlueJ, what should I type in the following dialog box?

推荐答案

您需要创建一个 ArrayList 的实例,以便在您调用它时传递给您的方法.在 BlueJ 主窗口中打开您的项目后,单击 Tools 菜单,然后单击Use Library Class...",然后从 Class 菜单中选择 java.util.ArrayList.同时从出现的列表中选择无参数构造函数,然后单击确定".

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 然后将显示另一个对话框,要求您输入实例的名称和 ArrayList 的类型参数.输入类型参数的名称和 Integer.

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.

单击确定"后,新的 ArrayList 实例将出现在 BlueJ 主窗口底部的对象工作台区域中.

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

当您右键单击新实例时,BlueJ 将显示可对其调用的方法菜单.多次选择 boolean add(Integer) 方法以向实例添加一些值.

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.

最后,当您右键单击您的测试类并调用 toArray 方法时,您可以输入 ArrayList 实例的名称以将其作为参数传递给您的方法.

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.

单击 Inspect 按钮查看从您的方法返回的 int 数组的内容,或单击 Get 按钮将其添加到对象工作台.

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.

这篇关于如何在 BlueJ 中为 ArrayList 输入参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆