how pass array to constructor java?

How pass array to Constructor with one parameter integer I want to save all the value of array in a1

when use this code :

//Constructor,,int a1

welcome(int…a)

    {
        for(int b:a)
        {
             a1=b;
        }

    }

OUTPUT display like this if write 1 & 2 as value of array the output will be :

2

2

that is mean print just last value of array

BUT want output like this :

1

2


public static void main(String[] args) 
{
    int size;

    Scanner input=new Scanner(System.in);
    size=input.nextInt();
    int []f= new int[size];

   jk e=new jk();

   e.fill(f);
}

public class jk {

private welcome a;
private int b;

public void fill(int f[])  
{
    Scanner input=new Scanner(System.in);
    for (int i=0;i<f.length;i++)
    {
       b=input.nextInt();
       a=new welcome(b);
    }


    for (int j=0;j<f.length;j++)
    {
       a.print();
    }
}


}

public class welcome {

private  int  a1;

welcome(int...a)
{
    for(int b:a)
    {
         a1=b;

    }

}

public void print()
{
    System.out.println(a1);
}
}
Jon Skeet
people
quotationmark

This loop:

for(int b:a)
{
     a1=b;
}

just repeatedly assigns the value of b to a1 - so yes, when the loop has finished, a1 will have the value of the last value in a. If you want to print out the values, you need to put the output in the loop:

for (int b : a) {
    System.out.println(b);
}

You haven't shown where your current output is - but if you're just repeatedly printing a1 then it will keep printing the same value.

EDIT: Now that we've seen your code, you just need to change a1 to be an array:

public class Welcome { // Names changed to be more conventional
    private final int[] values;

    public Welcome(int... values) {
        this.values = values;
    }

    public void printValues() {
        for (int value : values) {
            System.out.println(value);
        }
    }
}

Then in your fill method to need to populate the array:

public void fill(int[] f) {
    Scanner input=new Scanner(System.in);
    for (int i = 0; i < f.length; i++) {
       f[i] = input.nextInt();
    }
    a = new Welcome(f);
    a.printValues();
}

Previously you were creating one instance of your class on each iteration of the loop. You should look very carefully over your original code - ideally working through it in a debugger. It's important to understand why the original code didn't work just as much as how to fix it.

people

See more on this question at Stackoverflow