简体   繁体   中英

[JAVA]arraylist of arrays iterator

as the title says i'm heaving problem while iterating arraylist of arrays. Here is my definition: public static ArrayList<JTextArea[]> ta; here is how i read value from it:

Iterator<JTextArea[]> i = Interface.ta.iterator();
        while(i.hasNext())
        {
            JTextArea[] t = (JTextArea[]) i.next();
            ret += t[0].getText() + ", ";
        }

The JTextArea array is always an array of four elements and in this while I have to read the text of the firstone. The problem is simple, the "next()" method gives me every JTextArea object, not the complete array made of 4 JTextArea s.

Any way to get the Next() method pick all the array?

Thanks in advance

Use For Each instead of Iterator

for (JTextArea[] jTextAreaArray : ta) {
 // Use jTextAreaArray to do your process.
}

It is very simple to use rather than Iterator

To get Individual Objects use.

 for (JTextArea[] jTextAreaArray : ta) {
   for(JTextArea jTextArea : jTextAreaArray ){
    // Do your Process with Individual jTextArea.
    }
 }

You should be able to reference t[1], t[2], t[3] for the other 3 in the t array, so your codes should be:

  ArrayList<JTextArea[]> ta=null;
  // setup ta here;

  String ret = "";
  Iterator<JTextArea[]> i = ta.iterator();
  while (i.hasNext()) {
     JTextArea[] t = i.next();
     ret += t[0].getText() + ", ";
     ret += t[1].getText() + ", ";
     ret += t[2].getText() + ", ";
     ret += t[3].getText() + ", ";
     // or
     for (int x=0; x<t.length; x++) {
        ret += t[x].getText() + ", ";
     }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM