如何遍历对象列表,传递给方法然后将返回值存储在数组列表中?

时间:2021-08-01 17:01:06

I want to iterate through a List of Team objects, each object has a string and three integers within it. I want to pass it to a method which will compare the first Team object with the second one, then second to third and so on, Then finally get the returned enumerator depending on the comparison and store it into an array list.

我想迭代一个Team对象列表,每个对象都有一个字符串和三个整数。我想将它传递给一个方法,该方法将第一个Team对象与第二个对象进行比较,然后是第二个到第三个,依此类推,然后最终根据比较得到返回的枚举器并将其存储到数组列表中。

This is what I have so far, but when I run it, it comes up with a null value exception.

这是我到目前为止所做的,但是当我运行它时,它会出现一个空值异常。

TeamList = leagueRepository.getTeamList();
    int i = 0;

    for(Team team: teamList) {
        TeamStatus teamStatus = team.leagueStatus(team, team);
        verdictArray.add(i, teamStatus);

How do I get the first and second Team objects in the List?

如何获取列表中的第一个和第二个Team对象?

4 个解决方案

#1


2  

If TeamList is a List you can call get(0) to get the first entry and get(1) to get the second entry.

如果TeamList是List,您可以调用get(0)获取第一个条目,并获取(1)获取第二个条目。

If you only want to sort the list you can take a look at this post

如果您只想对列表进行排序,可以查看这篇文章

Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?

Java列表排序:有没有办法让列表像TreeMap一样自动排序?

#2


0  

To get the first and second objects in a list, you should use list.get(0) and list.get(1).

要获取列表中的第一个和第二个对象,应使用list.get(0)和list.get(1)。

#3


0  

The simplest way is to iterate using an index:

最简单的方法是使用索引进行迭代:

for (int i = 0; i < list.size() - 1; i++) {
    Team current = list.get(i);
    Team next = list.get(i + 1);
    // do something with current and next
}

You could also store the previous element at each iteration:

您还可以在每次迭代时存储前一个元素:

Team previous = null;
for (Team current : list) {
    if (previous != null) {
        // do something with previous and current
    }
    previous = current;
}

#4


0  

It's nothing but sorting. If youe use Collections.sort with comparator you can achieve this.

这只是排序。如果您使用Collections.sort与比较器,您可以实现这一点。

#1


2  

If TeamList is a List you can call get(0) to get the first entry and get(1) to get the second entry.

如果TeamList是List,您可以调用get(0)获取第一个条目,并获取(1)获取第二个条目。

If you only want to sort the list you can take a look at this post

如果您只想对列表进行排序,可以查看这篇文章

Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?

Java列表排序:有没有办法让列表像TreeMap一样自动排序?

#2


0  

To get the first and second objects in a list, you should use list.get(0) and list.get(1).

要获取列表中的第一个和第二个对象,应使用list.get(0)和list.get(1)。

#3


0  

The simplest way is to iterate using an index:

最简单的方法是使用索引进行迭代:

for (int i = 0; i < list.size() - 1; i++) {
    Team current = list.get(i);
    Team next = list.get(i + 1);
    // do something with current and next
}

You could also store the previous element at each iteration:

您还可以在每次迭代时存储前一个元素:

Team previous = null;
for (Team current : list) {
    if (previous != null) {
        // do something with previous and current
    }
    previous = current;
}

#4


0  

It's nothing but sorting. If youe use Collections.sort with comparator you can achieve this.

这只是排序。如果您使用Collections.sort与比较器,您可以实现这一点。