This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays" Watch "Arrays" New topic
Author

Arrays

Matthew Jones
Ranch Hand

Joined: Dec 21, 2000
Posts: 68
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...
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Try this...
String example[];
In your constructor...
example = { "ex1", "ex2", "ex3", "ex4", "ex5", "ex6 };
Bosun

Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Matthew Jones
Ranch Hand

Joined: Dec 21, 2000
Posts: 68
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.
Pat Barrett
Ranch Hand

Joined: Jan 03, 2001
Posts: 63
I just heard (very briefly) about vectors being used for arrays of objects. Would a vector work?
sebu koleth
Greenhorn

Joined: Nov 26, 2000
Posts: 4
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

Joined: Dec 21, 2000
Posts: 68
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
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Matthew,
Try this:

-Peter
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Arrays
 
Similar Threads
Integer class again
Difference between the way to create a variable in the memory
K&B Chapter 9 exercise 9-2 page 738.
Variable Issues
Help with Java Beans