Here is my java code:-
import java.util.Scanner;
class Test
{
int maxSize;
int array[] = new int[maxSize];
Test(int maxSize)
{
this.maxSize = maxSize;
}
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Enter size of Array:- ");
int maxSize = input.nextInt();
Test stack = new Test(maxSize);
System.out.println("Enter array element:- ");
for(int i=0; i<maxSize; i++)
{
stack.array[i] = input.nextInt();
}
for(int i=0; i<maxSize; i++)
{
System.out.print(stack.array[i]);
}
}
}
This code gives an error, Array Index Out Of Bounds. How can I make the size of the array as the maxSize?
I tried to send the maxSize through constructor as shown in the code above. But it does not works.
I tried as
class Test
{
int maxSize;
int array[];
Test(int maxSize)
{
this.maxSize = maxSize;
this.array[] = new int[maxSize];
:::::::::::::::::::::::::
It doesn't works either. Can anyone suggest the solution/improvement to make it work as expected.