Home > Archive > Java Help > January 2008 > declaring an array..
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
declaring an array..
|
|
|
| hi,
I need to declare an array, but not give it a value initially; values
will given inside conditionals.. I need to do what with strings you
would like this:
String example = "";
if ( cond1) {
example = "blue";
}
if ( cond2 ) {
example = "green";
}
how do you do this w/an array? just declare the array, then give it
values inside conditionals??? can't seem to get this right.. have tried:
String[]pages;
// String pages[];
pages[] = {"page1", "page2", "page3", "page4", "page5", "page6"};
all of them throw an an error.. how do you JUST DECLARE an array without
right away giving it a value?
thank you.....
| |
| Nigel Wade 2008-01-15, 7:25 pm |
| maya wrote:
> hi,
>
> I need to declare an array, but not give it a value initially; values
> will given inside conditionals.. I need to do what with strings you
> would like this:
>
> String example = "";
>
> if ( cond1) {
> example = "blue";
> }
>
> if ( cond2 ) {
> example = "green";
> }
>
> how do you do this w/an array? just declare the array, then give it
> values inside conditionals??? can't seem to get this right.. have tried:
>
>
> String[]pages;
> // String pages[];
>
> pages[] = {"page1", "page2", "page3", "page4", "page5", "page6"};
>
> all of them throw an an error.. how do you JUST DECLARE an array without
> right away giving it a value?
>
> thank you.....
String pages[];
....
pages = new String[] {"page1", "page2", "page3", "page4", "page5", "page6"};
--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
| |
| RedGrittyBrick 2008-01-15, 7:25 pm |
| Nigel Wade wrote:
> maya wrote:
>
>
>
> String pages[];
>
> ...
>
> pages = new String[] {"page1", "page2", "page3", "page4", "page5", "page6"};
>
>
You can omit "new String[]"
String[] pages;
....
pages = {"page1", "page2", "page3", "page4", "page5", "page6"};
| |
| Patricia Shanahan 2008-01-15, 7:25 pm |
| RedGrittyBrick wrote:
....
> You can omit "new String[]"
>
> String[] pages;
> ...
> pages = {"page1", "page2", "page3", "page4", "page5", "page6"};
....
No, you cannot. The "new String[]" can be omitted when initializing a
String[] as part of its declaration, but not when assigning an array
creation expression.
Patricia
| |
| RedGrittyBrick 2008-01-15, 7:25 pm |
| Patricia Shanahan wrote:
> RedGrittyBrick wrote:
> ...
> ...
>
> No, you cannot. The "new String[]" can be omitted when initializing a
> String[] as part of its declaration, but not when assigning an array
> creation expression.
>
Oops - Thanks for the correction.
| |
|
| Patricia Shanahan wrote:
> RedGrittyBrick wrote:
> ...
> ...
>
> No, you cannot. The "new String[]" can be omitted when initializing a
> String[] as part of its declaration, but not when assigning an array
> creation expression.
indeed you cannot.. I just tested it...
thank you all for yr responses.. tested in a stand-alone class, works
fine (even if the arrays have diff no. of elements in each conditional
-- ....:)
| |
|
|
| Roedy Green 2008-01-15, 7:25 pm |
| On Tue, 15 Jan 2008 10:42:00 -0500, maya <maya778899@yahoo.com> wrote,
quoted or indirectly quoted someone who said :
>
>all of them throw an an error.. how do you JUST DECLARE an array without
>right away giving it a value?
see http://mindprod.com/jgloss/array.html
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
| |
|
| Nigel Wade wrote:
> String pages[];
This is legal, but it's more "Javaesque" to declare
String [] pages;
This reads as "String array pages", as opposed the first form that would read,
"String pages array". The first shows part of the type, the variable, then
the rest of the type; the second keeps type information parts together, then
the variable follows all of them.
--
Lew
| |
| Hal Rosser 2008-01-15, 10:22 pm |
|
"maya" <maya778899@yahoo.com> wrote in message news:fmik88$cmt$1@aioe.org...
> hi,
>
> I need to declare an array, but not give it a value initially; values will
> given inside conditionals.. I need to do what with strings you would like
> this:
>
> String example = "";
>
> if ( cond1) {
> example = "blue";
> }
>
> if ( cond2 ) {
> example = "green";
> }
>
> how do you do this w/an array? just declare the array, then give it
> values inside conditionals??? can't seem to get this right.. have tried:
>
>
> String[]pages;
> // String pages[];
>
> pages[] = {"page1", "page2", "page3", "page4", "page5", "page6"};
>
> all of them throw an an error.. how do you JUST DECLARE an array without
> right away giving it a value?
>
> thank you.....
Declare an ArrayList
Then after you fill it in the If statement
Then use the toArray method to get the array you want.
|
|
|
|
|