Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

populate an array of string with

Thread view: 
Mike Phelps - 17 May 2004 05:28 GMT
Hi Guys,

In the following snippet:

.....
String item1 = "hello";
String item2 = "world";
String[] items = new String[2];
for(int i=0; i<items.length; i++) {
   //populate items[i] with contents of item1 or item2 or .....
   items[i] = ????????
  // not possible to do
  // if (i == 1){item[i] = item1;} else {item[i] = items2}
  // or items = {item1, item2}
  // in case you wonder why doing something so stupid, it is for
  // testing purpose and there is in fact a lot more than 2 strings

}

Does anybody see an easy solution?
Many Thanks, Mike
Elie De Brauwer - 17 May 2004 05:37 GMT
> Hi Guys,
>
[quoted text clipped - 16 lines]
> Does anybody see an easy solution?
> Many Thanks, Mike

Use a datastructure like a vector
( http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html )
for example, add the Strings to the vector (using add(Object o) and use
the toArray() to get an array of the objects (cast to String []) contained
in the Vector.

hth

Signature

Elie De Brauwer
http://www.de-brauwer.be

No animals were hurt and no microsoft products were used during the
creation of this e-mail

Mike Phelps - 17 May 2004 06:34 GMT
>> Hi Guys,
>>
[quoted text clipped - 24 lines]
>
>hth

Thanks for your help Elie but I am failing to see how this is going to
work:

remember we are inside a for loop: so  we can access the index i

pb is I can build a string concat of "item" and the index but what i
need is the content of this string

how can I build the object
Vector vec = new Vector();
vec.add( ??????); inside a loop

Sorry, maybe I am missing something, let me know if you can you write
the code?
Thanks again,
Mike
Inertia_sublimation - 18 May 2004 17:06 GMT
I'm not completely sure of your original request, but this might help:

Vector vec = new Vector();
vec.add("hello");
vec.add("world");

String[] contents = (String[])vec.toArray(contents);

or do you have something like this:

String item1 = "hello";
String item2 = "world";
.....
String itemN = "someString";

and want to fill a vector like that? In that case, you either need to use
reflection, or manually fill the contents of the vector.
However, if you want to do that, you should have instead used an array to begin
with.

String[] items = new String[2]; // You have to know the # of items though.
items[0] = "hello";
items[1] = "world";

or

String[] items = {
   "hello",
   "world",
}; // This way, you dont need to know the # of items, but your stuck with the
number of elements you insert into your items String array in its declaration.

> Thanks for your help Elie but I am failing to see how this is going to
> work:
[quoted text clipped - 12 lines]
> Thanks again,
> Mike
Oscar kind - 17 May 2004 07:40 GMT
> In the following snippet:
>
[quoted text clipped - 14 lines]
>
> Does anybody see an easy solution?

No. AFAIK, this is not possible. The reason: java is not an interpreted
language with an "evaluate" command. At compile time you must be able to
identify the exact variable name (statically; not dynamically using a
piece of code).

A way to solve this problem is to transform the code that defines the
String objetcs:
        String item1 = "hello";
        String item2 = "world";
        ...
        String itemN = "foobar";
        String[] items = // Problem

    Becomes:
        ArrayList stringItems = new ArrayList();
        stringItems.add("hello");
        stringItems.add("world");
        ...
        stringItems.add("foobar");
        String[] items = (String[])stringItems.toArray(new String[] {});

You'll notice the first and last lines stay the same regardless of the
number of strings. Also note, that the index numbers of the String objects
"itemN" are dropped, so make sure you add the strings in sequence.

Oscar

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2

John - 17 May 2004 08:24 GMT
>>In the following snippet:
>>
[quoted text clipped - 41 lines]
>
> Oscar

I agree. When I have to do something like this I always use larger data
structures to select from. People used to Perl/PHP usually get
frustrated with having to do this. I would probably just do:

String[] chooseFrom = new String[]{"stringa","stringb","stringc"};
int resultLength = 2;
String[] result = new String[resultLength];

for(int i=0; i<result.length; i++) {
    result[i] = randomSelect(chooseFrom); //randomSelect elsewhere
}

I've just re-read the OP. If you don't need nadom selection then the
first line will do.

John
John - 17 May 2004 08:48 GMT
<snip>
> I've just re-read the OP. If you don't need nadom selection then the
> first line will do.
>
> John

correction: nadom -> random
Yu SONG - 19 May 2004 15:51 GMT
> Hi Guys,
>
[quoted text clipped - 17 lines]
> Does anybody see an easy solution?
> Many Thanks, Mike

It depends on how many "item"s (e.g item1, item2 ... item1000? you know
the number of items or not) and how they are stored.(all in separated
strings? or in an array?)

If "item"s stored in this case (in separated strings),
why don't you use "items = {item1, item2}" in the first instance?

Signature

Song

More info.:
http://www.dcs.warwick.ac.uk/~esubbn/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.