2

I have a class arrayFun with the variable

  int[] _array;

I have a method setArray:

        public void setArray(int [] array) 
        {
         _array = array;
        }
  1. Is my set method implementation correct ?

2).How can I use this method in other class with main ?

I've tried some ridiculous options like:

       arrayFun A = new arrayFun(some_constructor_values);
       A.setArray(1,2,3,4,5);

That option of course doesn't work...

3
  • 3
    If you're still learning maybe it would be good to follow official code conventions Commented Nov 30, 2011 at 10:07
  • 1
    ` A.setArray(new int[] {1,2,3,4,5});` Commented Nov 30, 2011 at 10:07
  • They're a good idea learning or not. ;) Commented Nov 30, 2011 at 10:07

6 Answers 6

2

Try

A.setArray(new int[]{1,2,3,4,5});
Sign up to request clarification or add additional context in comments.

Comments

2

Another way to solve this declare the argument as a "varargs" argument as follows:

public void setArray(int ... array) {
     _array = array;
}

and then this will work:

A.setArray(1, 2, 3, 4, 5);

You can do the same with a constructor argument.


While I have your attention, it is important that you learn the Java naming conventions, and learn to follow them strictly.

  • A class name should always start with an uppercase letter
  • A variable name should always start with a lowercase letter ... unless it is a static final constant.
  • Using an underscore as a prefix generally frowned on.

For more information, read the Java Style Guidelines.

So your example class should look like this:

public class ArrayFun {

    private int[] array;

    public void setArray(int ... array) {
       this.array = array;
    }
}

and should be used like this:

ArrayFun a = new ArrayFun();
a.setArray(1, 2, 3, 4, 5);

1 Comment

Nice. It's the first time I've heard of Java varargs. I'm familiar with the use from perl and javascript, but it seems a bit "dirty" for Java.
0

You can use this instead

public void setArray(int... array) { _array = array; }

// later
ArrayFun a = new ArrayFun(some_constructor_values);
a.setArray(1,2,3,4,5);

Unless you take a copy of the array, you will be using the same array in the caller and callee.

Comments

0

What you're asking to do doesn't really make sense. Also, why use a function to "set the array", why not just set the array directly:

_array = newArray

You can also set an array's values like this:

int[] array = {1,2,3,4,5};

Comments

0

Your method's signature is :

public void setArray(int[] array)

So it accepts only one argument that is of type array of integers.

But in your method call, you are calling it as:

A.setArray(1,2,3,4,5);

In this you are passing 5 arguments to the method. So it does not match any method with 5 arguments. Thats why it does not work.

You should pass one array of integers.

You can do it in various ways :

int myArr[] = {1,2,3,4,5};
A.setArray(myArr);

or

A.setArray(new int[]new int[]{1,2,3,4,5});

Comments

0

Setting array the way you did is fine. But what you are setting from A.setArray(1,2,3,4,5); will throw you error saying "Method setArray(int,int,int,int,int) is not found".

You can do something like int[] ar = { 1, 2 }; a.setArray(ar);

Comments

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.