The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

BrianB (16) [Avatar] Offline
Perhaps I've missed something. To say the least, I am confused. I was practicing declaring, allocating, and initializing arrays and following the examples laid out in the book in (sections: 4.3.2, 4.3.3, and 4.3.4).

Using netBeans, I placed the following into the IDE:

int intArray[]; // array declaration

No errors were present.

Then I placed the next line:

intArray = new int[2]; // allocates the array

Now, it starts to warn me an error.

Finally, I placed the for loop to initialize the array

for (int I = 0; I < intArray.length; i++) {
intArray[i] = i + 5;

Again, this generates a warning from the IDE.

This will not compile, despite the book stating it's a valid way of declaring, allocating, and initializing an array.

To be sure it wasn't the IDE, I copied the code straight from the text and pasted it into a text editor and tried to compile it from the command line. Sure enough it generates a laundry list of errors.

Why is this?

Message was edited by:
mala.gupta (245) [Avatar] Offline
Re: Chapter 4 - 4.3.2, 4.3.3, and 4.3.4
Hi Brian,

There is typing mistake with the code snippet that you have reported.

The correct code, as mentioned in the book, is as follows:

for (int i = 0; i < intArray.length; i++) {
intArray[i] = i + 5;

The preceding code executes without any issues.

With much respect,