1

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.

4 Answers 4

6

This line:

int array[] = new int[maxSize];

is executed before the constructor.

int maxSize;

effectivley means:

int maxSize = 0;

so array is always initialized as a zero-length array. Change your code to this:

int maxSize;
int array[];

Test(int maxSize)
{
    this.maxSize = maxSize;
    array = new int[maxSize];
}

The maxSize member of the class is not even necessary, you can even simply do this:

int array[];

Test(int maxSize)
{
    array = new int[maxSize];
}

You can always get the size of the array with (assuming that array has already been initialized and is not null):

int length = array.length;
Sign up to request clarification or add additional context in comments.

Comments

5

Initialise the array in your constructor. The syntax is

Test(int maxSize) {
    this.maxSize = maxSize;
    this.array = new int[maxSize];
}

Comments

1

when you initiate array at declaration maxSize is 0. better to initiate array in constructor:

int maxSize;
int[] array;

Test(int maxSize) {
    this.maxSize = maxSize;
    this.array = new int[maxSize];
}

Comments

1

you should use the lists, for example a vector

import java.util.*;
class Test
{
    //int maxSize;
    public Vector<Integer> array;

    Test()
    {
        //this.maxSize = maxSize;
        array = new Vector<Integer>();
    }
    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();
        System.out.println("Enter array element:- ");
        for(int i=0; i<maxSize; i++)
        {
            stack.array.add(input.nextInt());
        }
        for(int i : stack.array)
        {
            System.out.print(i);
        }

    }
}

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.