ArrayList迭代中的Java列表

时间:2017-04-30 21:06:18

标签: java for-loop arraylist iteration

我正在尝试使用嵌套的for循环迭代ArrayList中的列表以返回到某个对象(在本例中为电子邮件)。这是我的代码 -

private ArrayList<Trainer> trainers;
...

public String checkEmail(){
    String userEmail = "";
    for(int i=0; i<trainers.size(); i++){              
        for(int j=0; j<trainers.get(i).size(); j++){   
            userEmail = userEmail + trainers.get(i).get(j);
        }

        return userEmail;
    }
}

这是导出到xml.file中的ArrayList,电子邮件字段是我想要访问的

<object-stream>
  <list>
    <Member>
      <email>example@gmail.com</email>
      <name>example</name>
      <address>example</address>
      <gender>M</gender>
      <height>1.78</height>
      <weight>65.0</weight>
      <chosenPackage>student</chosenPackage>
    </Member>
  </list>
</object-stream>

谢谢!

编辑:很抱歉我的问题含糊不清,我想使用第一个for循环(i)迭代每个'Memeber',如xml所示,嵌套循环(j)迭代每个成员。我想要返回特定成员的电子邮件,但是在for(int j=0; j<trainers.get(i).size(); j++){行上我得到'size()未定义类型Trainer'。

1 个答案:

答案 0 :(得分:0)

请在for循环外写下return语句。 (我只是猜测你错误地写了它)。如果继续使用相同的循环,则return语句将在i = 0本身上执行,从而产生不正确的结果。

/* *****************************************************************************
API declarations - should be moved to a separate .h file.
***************************************************************************** */

/** Defer an execution of a function for later. Returns -1 on error.*/
int defer(void (*func)(void *), void *arg);

/** Performs all deferred functions until the queue had been depleated. */
void defer_perform(void);

/** returns true if there are deferred functions waiting for execution. */
int defer_has_queue(void);

/* *****************************************************************************
Compile time settings
***************************************************************************** */

#ifndef DEFER_QUEUE_BUFFER
#define DEFER_QUEUE_BUFFER 1024
#endif


/* *****************************************************************************
spinlock / sync for tasks
***************************************************************************** */
#if defined(__unix__) || defined(__APPLE__) || defined(__linux__)
#define _GNU_SOURCE
#include <time.h>
#endif /* _GNU_SOURCE */
#include <stdlib.h>

/* manage the way threads "wait" for the lock to release */
#if defined(__unix__) || defined(__APPLE__) || defined(__linux__)
/* nanosleep seems to be the most effective and efficient reschedule */
#define defer_nanosleep(length)                                                \
  {                                                                            \
    static const struct timespec tm = {.tv_nsec = length};                     \
    nanosleep(&tm, NULL);                                                      \
  }
#define reschedule_thread() defer_nanosleep(1)
#define throttle_thread() defer_nanosleep(8388608UL)

#else /* no effective rescheduling, just spin... */
#define reschedule_thread()
#define throttle_thread()
#endif

/** locks use a single byte */
typedef volatile unsigned char spn_lock_i;
/** The initail value of an unlocked spinlock. */
#define SPN_LOCK_INIT 0

/* Select the correct compiler builtin method. */
#if defined(__has_builtin)
#if __has_builtin(__sync_swap)
#define SPN_LOCK_BUILTIN(...) __sync_swap(__VA_ARGS__)
#elif __has_builtin(__sync_fetch_and_or)
#define SPN_LOCK_BUILTIN(...) __sync_fetch_and_or(__VA_ARGS__)
#else
#error Required builtin "__sync_swap" or "__sync_fetch_and_or" missing from compiler.
#endif /* defined(__has_builtin) */
#elif __GNUC__ > 3
#define SPN_LOCK_BUILTIN(...) __sync_fetch_and_or(__VA_ARGS__)
#else
#error Required builtin "__sync_swap" or "__sync_fetch_and_or" not found.
#endif

/** returns 1 and 0 if the lock was successfully aquired (TRUE == FAIL). */
static inline int spn_trylock(spn_lock_i *lock) {
  return SPN_LOCK_BUILTIN(lock, 1);
}

/** Releases a lock. */
static inline __attribute__((unused)) void spn_unlock(spn_lock_i *lock) {
  __asm__ volatile("" ::: "memory");
  *lock = 0;
}
/** returns a lock's state (non 0 == Busy). */
static inline __attribute__((unused)) int spn_is_locked(spn_lock_i *lock) {
  __asm__ volatile("" ::: "memory");
  return *lock;
}
/** Busy waits for the lock. */
static inline __attribute__((unused)) void spn_lock(spn_lock_i *lock) {
  while (spn_trylock(lock)) {
    reschedule_thread();
  }
}

/* *****************************************************************************
Data Structures
***************************************************************************** */

typedef struct {
  void (*func)(void *);
  void *arg;
} task_s;

typedef struct task_node_s {
  task_s task;
  struct task_node_s *next;
} task_node_s;

static task_node_s tasks_buffer[DEFER_QUEUE_BUFFER];

static struct {
  task_node_s *first;
  task_node_s **last;
  task_node_s *pool;
  spn_lock_i lock;
  unsigned char initialized;
} deferred = {.first = NULL,
              .last = &deferred.first,
              .pool = NULL,
              .lock = 0,
              .initialized = 0};

/* *****************************************************************************
API
***************************************************************************** */

/** Defer an execution of a function for later. */
int defer(void (*func)(void *), void *arg) {
  if (!func)
    return -1;
  task_node_s *task;
  spn_lock(&deferred.lock);
  if (deferred.pool) {
    task = deferred.pool;
    deferred.pool = deferred.pool->next;
  } else if (deferred.initialized) {
    task = malloc(sizeof(task_node_s));
    if (!task)
      goto error;
  } else { /* lazy initialization of task buffer */
    deferred.initialized = 1;
    task = tasks_buffer;
    deferred.pool = tasks_buffer + 1;
    for (size_t i = 2; i < DEFER_QUEUE_BUFFER; i++) {
      tasks_buffer[i - 1].next = tasks_buffer + i;
    }
  }
  *deferred.last = task;
  deferred.last = &task->next;
  task->task.func = func;
  task->task.arg = arg;
  task->next = NULL;
  spn_unlock(&deferred.lock);
  return 0;
error:
  spn_unlock(&deferred.lock);
  return -1;
}

/** Performs all deferred functions until the queue had been depleted. */
void defer_perform(void) {
  task_node_s *tmp;
  task_s task;
restart:
  spn_lock(&deferred.lock);
  tmp = deferred.first;
  if (tmp) {
    deferred.first = tmp->next;
    if (!deferred.first)
      deferred.last = &deferred.first;
    task = tmp->task;
    if (tmp <= tasks_buffer + (DEFER_QUEUE_BUFFER - 1) && tmp >= tasks_buffer) {
      tmp->next = deferred.pool;
      deferred.pool = tmp;
    } else {
      free(tmp);
    }
    spn_unlock(&deferred.lock);
    task.func(task.arg);
    goto restart;
  } else
    spn_unlock(&deferred.lock);
}

/** returns true if there are deferred functions waiting for execution. */
int defer_has_queue(void) { return deferred.first != NULL; }