File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays

 
Matthew Jones
Ranch Hand
Posts: 68
  • 0
  • 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...
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • 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
  • 0
  • 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.
 
Pat Barrett
Ranch Hand
Posts: 63
  • 0
  • 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?
 
sebu koleth
Greenhorn
Posts: 4
  • 0
  • 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
  • 0
  • 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
 
Peter Tran
Bartender
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew,
Try this:

-Peter
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic