-5

I currently have the following code:

String [] names = {"Fred","Perry","Dave","Tom"};
for (String s: names) {           
Threads greetings = new Threads(s); 
}

And this works fine - however what i am doing is assigning Fred","Perry","Dave","Tom" all to greetings.

What i wish to do is the following

greetings0 = Tom
greetings1 = Perry
greetings2 = Dave
greetings3 = Tom

This will mean i can later on run greetings.start(); greetings1.start() etc at the same time in its own thread.

6
  • 6
    Well it's Thread not Threads. Could we see some code that compiles?
    – markspace
    Dec 27, 2015 at 0:01
  • I have improved the question as it wasnt to clear
    – Ingram
    Dec 27, 2015 at 11:45
  • If you really must have those variable names, you can't do that in a loop. You should use an array or list (as suggested in Josh's answer) as this lets you use a loop and keeps the code simpler.
    – Krease
    Dec 27, 2015 at 18:53
  • @Chris but how do i incorporate the variable names greetings* into this?
    – Ingram
    Dec 27, 2015 at 18:58
  • @Chris this is where i am stuck
    – Ingram
    Dec 27, 2015 at 18:59

1 Answer 1

1

You need a list of Thread objects, each one with the assigned name. You are trying to create dynamic variable names which isn't possible. Here is how to loop through the names and add each named Thread to a List.

I hope this is what you were looking for. It's a little unclear in your problem description about what you want with greetings, greetings1,greetings2,greetings3.

    List<Thread> threads = new ArrayList<>();
    String [] names = {"Fred","Perry","Dave","Tom"};
    for (String s : names){           
        threads.add(new Thread(s));
    }

EDIT: Since you said you will only ever have 4 greetings then you can do this:

Thread greeting = new Thread("Fred");
Thread greeting1 = new Thread("Perry");
Thread greeting2 = new Thread("Dave");
Thread greeting3 = new Thread("Tom");

You can also do this if you want to keep them in a List for some reason:

List<Thread> threads = new ArrayList<>();
String[] names = { "Fred", "Perry", "Dave", "Tom" };
for(String s : names)
{
    threads.add(new Thread(s));
}
Thread greeting = threads.get(0);
Thread greeting1 = threads.get(1);
Thread greeting2 = threads.get(2);
Thread greeting3 = threads.get(3);
5
  • Hello Josh not quite what i want, but that is my fault for not explaining properly. I am going to improve my question
    – Ingram
    Dec 27, 2015 at 11:39
  • Are you always going to have exactly 4 greetings? Dec 27, 2015 at 19:00
  • @ Josh Chappelle yes i will always have 4 greetings
    – Ingram
    Dec 27, 2015 at 21:33
  • 1
    I edited my answer with how to do what I think you are wanting. I hope that helps. Dec 27, 2015 at 23:09
  • @ Josh of course - call the index of the Arraylist, thats brilliant - many thanks
    – Ingram
    Dec 28, 2015 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.