List<Player> in java
时间: 2024-04-30 07:18:16 浏览: 95
`List<Player>` in Java is a declaration of a generic list that can contain objects of type `Player`. It allows you to store and manipulate a collection of `Player` objects, and provides methods such as `add()`, `remove()`, `get()`, and `size()` to manipulate the list.
Here's an example of how it can be used:
```java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Create a list of players
List<Player> players = new ArrayList<>();
// Add some players to the list
players.add(new Player("John", "Doe"));
players.add(new Player("Jane", "Doe"));
// Print the list of players
for (Player player : players) {
System.out.println(player.getFullName());
}
}
}
class Player {
private String firstName;
private String lastName;
public Player(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFullName() {
return firstName + " " + lastName;
}
}
```
In this example, we create a `List<Player>` called `players`, and add two `Player` objects to it. We then iterate through the list and print out each player's full name using the `getFullName()` method of the `Player` class.
阅读全文