1

i don't know how to set a specific array (with values of lets say {1,2,3,4}) as an object property? (this is the only property)

i tried this in my class as a constructor (and i don't want to use initializing constructor)

public class Arrays {

    public int [] arr = {2,3,4,5};

    public Arrays (int[] arr ) {

        this.arr = arr; }

but what do i put in the brackets ?

Arrays a = new Arrays ();

thanks!

BB_KING
  • 115
  • 1
  • 12

4 Answers4

1

You can do it like this:

Arrays a = new Arrays(int[x]{1,2,3,4,5});

Hope i could help you.

sexyboy

sexyboy
  • 24
  • 2
0

You could do

MyArrays arrays = new MyArrays(new int[] {1,2,3,4,5});
Reimeus
  • 152,723
  • 12
  • 195
  • 261
0

You must make new instance in place where you call your constructor.

Arrays a = new Arrays (new int[]{1,2});
Kasper Ziemianek
  • 1,269
  • 8
  • 15
0

If you want the object to initialize its own array:

public class Arrays {
    public int[] arr = {1,2,3,4,5};
}

And use it like this:

Arrays a = new Arrays();

If you want the caller to initialize an array and pass it to the constructor:

public class Arrays {
    public int[] arr;
    public Arrays(int[] arr) {
        this.arr = arr;
    }
}

And use it like this:

int[] arr = {1,2,3,4,5};
Arrays a = new Arrays(arr);
Andrew Sun
  • 3,721
  • 6
  • 32
  • 44