*
The moose likes Beginning Java and the fly likes where to define array and initialise Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "where to define array and initialise" Watch "where to define array and initialise" New topic
Author

where to define array and initialise

Marie O' Driscoll
Greenhorn

Joined: Mar 19, 2010
Posts: 11
Hi,

I'm used to programming in C and declaring all my variables at the beginning so this is taking a bit of getting used to!

I have the following code:



My problem is the the last line - It doesn't recognise array shapeVals even though I have it defined (line 22) and initialised (line 26). I tried putting it at the top of the code (around line 7)and that solved the problem with the last line but then that requires me to initialise the array there and then and I can't do that until later in the for loop when I read values in from a file.

How can I get around this?

Many thanks in advance.
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263


modify above to float[] shapeVals = new float[size of array];

In java Array is a class so you have to create Array object before you use it.


SCJP 1.5(97%) My Blog
Marie O' Driscoll
Greenhorn

Joined: Mar 19, 2010
Posts: 11
Hi Shivendra,

Thanks for the reply. I'm stil stuck though.

I changed the line to
This brings me back to my original problem though. When I do this the last line doesn't recognise the array shapeVals.

I think the problem is the array scope of definition. Because I defined shapeVals within a local block of code, it's not recognised (or maybe destroyed) when the block of code ends and I can't then use it outside the closing bracket. The only way around this that I can see if to define shapeVals at the top of my code but that would mean I would again need to specify the size of the array and I won't know that until I read a value for shapeLength.

any other suggestions?
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
OK. Perfect case to use ArrayList, if you dont know the array size in advance. And declare it outside the local method i.e. as instance variable.
ArrayList shapeVals = new ArrayList();
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Personally, I'd rework the logic altogether--that's *really* difficult to read.

Using a list, then converting it to an array if you don't have control over the method you're sending it to, is a viable option... but it doesn't make the code any easier to understand. Mixing tabs and spaces isn't helping readability, either: indentation is important.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Marie O' Driscoll wrote:I think the problem is the array scope of definition. Because I defined shapeVals within a local block of code, it's not recognised (or maybe destroyed) when the block of code ends and I can't then use it outside the closing bracket. The only way around this that I can see if to define shapeVals at the top of my code but that would mean I would again need to specify the size of the array and I won't know that until I read a value for shapeLength.

No, you can declare the variable as an array in the correct scope, then initialize it to an array of the correct size later.

Note that convention in Java says that arrays are declared "float foo[]" rather than the C-style.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Marie O' Driscoll wrote:I think the problem is the array scope of definition. Because I defined shapeVals within a local block of code, it's not recognised (or maybe destroyed) when the block of code ends and I can't then use it outside the closing bracket. The only way around this that I can see if to define shapeVals at the top of my code but that would mean I would again need to specify the size of the array and I won't know that until I read a value for shapeLength.

No, you can declare the variable as an array in the correct scope, then initialize it to an array of the correct size later.

Note that convention in Java says that arrays are declared "float foo[]" rather than the C-style.

And you don't need to escape a space in a regex.

It also seems like you loop over the array and set shapeVals values, then do it again, and reset a bunch of them--is there any reason you have to do it twice?
Marie O' Driscoll
Greenhorn

Joined: Mar 19, 2010
Posts: 11
Thanks to everyone who has replied so far - I'm making progress slowly but surely with your help.

I used ArrayList and then subsequently converted it to an array as follows:



Later in another class I need to access these values, and convert them all to floats. I currently have the following:



I think the above line should be (Float) thislane.shapeVals.get(f); but the problem is the ".get" option is not appearing or being allowed in Eclipse. When I hold mouse over a tooltip advises type mismatch can't change Float to float [] but they should both be arrays so I don't understand where the problem lies.

Is there an obvious reason that I'm missing for why this is the case? Or is there another way I can take this object array and make every element a float?

Many thanks - appreciate the advice so far.
Nisha Ganeriwal
Greenhorn

Joined: Feb 25, 2010
Posts: 8

You cannot declare an array like that. Declare the array outside the loop:

You can then do the initialisation in the loop.
Also specifying the array size in the declaration is not legal and will not compile.
Refer here.
Marie O' Driscoll
Greenhorn

Joined: Mar 19, 2010
Posts: 11
Hi Nisha,

Thanks for the reply - I have fixed the definition (below). This still leaves me the same issue though that the "(Float) thislane.shapeVals.get" option isn't available so I can't seem to cast each element of the object array to a float and read it into the new array.



I can't see anything wrong with the above and it seems to follow the syntax of other links that I've looked at for changing an Arraylist to an array and initialising the values.
Marie O' Driscoll
Greenhorn

Joined: Mar 19, 2010
Posts: 11
Sorry I no sooner had it posted and I realised the mistake (It's late and I;ve been looking at this too long)

The amended (working) line below:


Thanks to everyone who helped.
 
 
subject: where to define array and initialise
 
Similar Threads
Large Numbers
Determine If Value is Integer or Float
JTable and getValueAt(int, int)
applet and servlet communication error: java.io.EOFException
XML Parsing - how to read unknown number of values