Creating an array constructor and then creating an object in a main method
Hi,
I am new to programming and Java and get easily confused so any help is much appreciated.
I have created a constructor as follows:
private int [] myArray;
public MinInt (int [] setArray)
{
myArray = setArray;
}
I know want to use this constructor in my main method to create an array of integers but can't seem to get the syntax correct to declare this.
At the minute I have:
MinInt theArray = new MinInt {2,4,3,1,6,8};
But this does not work.
I know this is probably very simple.
Re: Creating an array constructor and then creating an object in a main method
Define an array of ints
Pass the name of that array in the constructor.
Re: Creating an array constructor and then creating an object in a main method
So this would be correct?
int [] anArray = {3,76,2,33,76,24,65,9};
MinInt theArray = new MinInt (anArray);
Re: Creating an array constructor and then creating an object in a main method
A good way to make sure would be to try it in the compiler.
Re: Creating an array constructor and then creating an object in a main method
Yes, it worked.
Just wanted to check that, how I have done it is the best way..