• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

where to define array and initialise

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Marie O' Driscoll
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 8
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic