• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Arrays

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create an array of Labels
I declare the array using:
Label example[];
at the top of me class, and in the constuctor tries to initialize it with:
Label[] example = { "ex1", "ex2", "ex3", "ex4", "ex5", "ex6 };
When I try to compile it, I get errors:
Incompatible type for array. Can't convert java.lang.String to java.awt.Label
Is there any way to initialize the whole array at once, or do I need to do something like:
example[0] = new Label("ex1");
example[1] = new Label("ex2");

etc...
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this...
String example[];
In your constructor...
example = { "ex1", "ex2", "ex3", "ex4", "ex5", "ex6 };
Bosun
 
Matthew Jones
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While that would probably work, I'm trying to create an array of Labels that I want to user further in my program, not Strings. I can probably just initialize them seperatly, but as I have a LOT of arrays that I am going to end up needing, I wanted to know if there was a quicker, more efficient way of doing this.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just heard (very briefly) about vectors being used for arrays of objects. Would a vector work?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can see what you have been trying to do is assign an array containing Strings to an array containing Labels. But this is not possible. You will have to first create Label objects with the Strings and then initialise the array. Or create the array of Label objects with empty constructors and then set the text of each label. If someone else can suggest a better solution please do so.
 
Matthew Jones
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pat:
I don't know much about vectors at this point, so I think I will stick with the arrays.
Sebu:
Thanks for the info, I was just hoping there was a quicker way, but I guess I'll have to create all the Labels individually.
Thanks, guys
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew,
Try this:

-Peter
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew,
The easiest way to probably do this is to have an array of Labels and an array of Strings:
<code>
Label example[];
String words[] = { "ex1", "ex2", "ex3", "ex4", "ex5", "ex6" };
example = new Label[words.length];
for ( int i = 0; i < words.length; i++ )
{
example[i] = new Label( words[i] );
}
</code>
HTH,
Nate
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!