0

It appears to me that there could be a better way to this, maybe using loops, I think.

 String hora1 = listaH.get(0);
 String hora2 = listaH.get(1);
 String hora3 = listaH.get(2);
 String hora4 = listaH.get(3);
 String hora5 = listaH.get(4);
 String hora6 = listaH.get(5);
 String hora7 = listaH.get(6);
 String hora8 = listaH.get(7);
 String hora9 = listaH.get(8);

Is there another way to write this using less words? Thanks

5
  • 4
    If you need a seperate variable to hold each element, why maintain an array to begin with? Commented Sep 3, 2013 at 2:39
  • If you're willing to do operations on each element per occurrence (that is, you don't use a variable for each), then yes. Commented Sep 3, 2013 at 2:39
  • If you need nine separate variables, then the answer is "no". If you would be fine with an String[] hora array of nine elements, then yes. Commented Sep 3, 2013 at 2:40
  • Each string shows me a different word. But I want all the strings to do the same thing: ".setText()" in a jLabel. I have 9 jLabels created and I want one string per label. Commented Sep 3, 2013 at 2:42
  • Why not create the JLabels, set their text, and add them to your panel/frame in a loop that iterates over your listaH? Or maintain a separate list/array of your labels that coincide with the strings you want them to display. Commented Sep 3, 2013 at 2:46

4 Answers 4

3

It depends on what you want to achieve and what you hope to gain from it...but...

Assuming that listaH is java.util.List, you could use

for (String horse : listaH) {
    System.out.println(horse);
}

(NB: You can do the same thing with arrays)

Take a look at The for statement and The while and do-while statements for more details

Sign up to request clarification or add additional context in comments.

Comments

0

Use arrays for hora variable. It can be like

String[] hora = new String[9];

Now you can use any loops, but for is best for your case.

for(int i = 0; i < 9 ; i++){
 hora[i] = listaH.get(i);
}

But why waste resource and complexity on new variables? You can do something like listaH.get(4) wherever you need hora5.

Comments

0
  Iterator itr = listaH.iterator();
  while(itr.hasNext()) {
     String element = (String) itr.next();
     System.out.print(element + " ");
  }   

 or 

 for (int i=0;i<listaH.size();i++) {
        String element  = (String)listaH.get(i); 
        System.out.print(element + " ");
  }

if you need String array then use this one

String[] array = listaH.toArray(new String[listaH.size()]);

Comments

0

Yes. Instead of separate variables for each element, you should just leave them in the original list and then access them that way. So, for example, when you need the value that you are trying to store in hora9, use listaH.get(8) instead.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.