简体   繁体   English

[JAVA]数组迭代器的arraylist

[英][JAVA]arraylist of arrays iterator

as the title says i'm heaving problem while iterating arraylist of arrays. 正如标题所说,我在迭代数组的arraylist时会出现问题。 Here is my definition: public static ArrayList<JTextArea[]> ta; 这是我的定义: 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. JTextArea数组总是一个包含四个元素的数组,在此我必须阅读第一个元素的文本。 The problem is simple, the "next()" method gives me every JTextArea object, not the complete array made of 4 JTextArea s. 问题很简单,“next()”方法为我提供了每个JTextArea对象,而不是由4个JTextArea组成的完整数组。

Any way to get the Next() method pick all the array? 有没有办法让Next()方法选择所有数组?

Thanks in advance 提前致谢

Use For Each instead of Iterator 使用For Each而不是Iterator

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

It is very simple to use rather than Iterator 它使用起来非常简单,而不是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: 你应该能够为t数组中的其他3引用t [1],t [2],t [3],所以你的代码应该是:

  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() + ", ";
     }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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