简体   繁体   中英

User defined array size in java class

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.

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;

Initialise the array in your constructor. The syntax is

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

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);
        }

    }
}

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM