Hi, I am new to java and I need to work with arrays.
I have found some tutorials, but everywhere was something like

int[] anArray;

        // allocates memory for 10 integers
        anArray = new int[10];

But what if I dont know how long the array will be?
When I tried to do just that:

int[] anArray;

Without allocating memory, Im getting error:

The local variable anArray may not have been initialized.

Can you please help me?

you can initialize it as null, and then when you know how long will it be, you can assign a new array.

int[] myArray = null;

 //somwhere else at your code
 
 myArray = new int[10]; //or whatever your lenght will be

just be shure to access it after it is initialized, otherwise you will get NullPointerException.


--------------
P.S: Maybe you want to sign in, I'm designing a java online course :D
<URL SNIPPED>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.