Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

java - How do I enter parameters for an ArrayList in 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}

How do I do a method call for an ArrayList?

Here is my code:

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;    

    }
}

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

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 Call Library Class dialog

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.

BlueJ Create Object dialog

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

BlueJ Object Bench

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.

Add Integers to the 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.

BlueJ Method Call dialog

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

BlueJ Method Results 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...