为什么使用 ArrayList 但 returns 原始类型的方法需要方法名称包含 "static"?
Why does a method that uses an ArrayList but returns a primitive type require the method name to include "static"?
此程序有效,但我不确定为什么使用 ArrayList
的方法必须在方法 header 中包含 static
。仅作为背景,该程序接收一个文本文件,读取它并将其内容保存到 ArrayList<Game>
(游戏)中。用户输入文件名和球队名,程序输出特定球队打了多少场比赛,赢了多少场,输了多少场。
它读取的 .csv 格式如下:
ENCE,Vitality,9,16
ENCE,Vitality,16,12
etc..
以下所有四种方法,一种方法返回 ArrayList
,另三种方法返回 int
,除非我在方法中输入关键字 static
,否则将不起作用 header。如果我只为方法写 public ArrayList<Game>
或 public int
,它们将不起作用。
是否任何时候将 Arraylist
传递给方法,这意味着方法 header 必须始终包含 static
?为什么会这样?
这是工作代码:
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
public class SportStatistics {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("File: ");
String file = scan.nextLine();
ArrayList<Game> record = getGame(file); //After method returns the list of
objects, it is copied over to another list.
System.out.println("Team: ");
String team = scan.nextLine();
//These methods return how many games a team played, has won and has lost.
int gamesPlayed = getGamesPlayed(record, team);
int gamesWon = getGamesWon(record, team);
int gamesLost = getGamesLost(record, team);
System.out.println("Games: " + gamesPlayed);
System.out.println("Wins: " + gamesWon);
System.out.println("Losses: " + gamesLost);
}
//This method takes in the file name given by user and saves file details into
a list of objects.
**public static** ArrayList<Game> getGame(String file){
ArrayList<Game> games = new ArrayList<>();
try(Scanner reader = new Scanner(Paths.get(file))){
while(reader.hasNextLine()){
String input = reader.nextLine();
String[] parts = input.split(",");
String homeTeam = parts[0];
String visitingTeam = parts[1];
int homePoints = Integer.valueOf(parts[2]);
int visitingPoints = Integer.valueOf(parts[3]);
games.add(new Game(homeTeam, visitingTeam, homePoints,
visitingPoints));
}
}
catch(Exception e){
System.out.println("Error: File " + file + " not found.");
}
return games;
}
**public static int** getGamesPlayed(ArrayList<Game> record, String team){
int gamesPlayed = 0;
for(Game game: record){
if (game.getHomeTeam().equals(team) ||
game.getVisitngTeam().equals(team)){
gamesPlayed++;
}
}
return gamesPlayed;
}
**public static int** getGamesWon(ArrayList<Game> record, String team){
int gamesWon = 0;
for(Game game: record){
if (game.getHomeTeam().equals(team) ||
game.getVisitngTeam().equals(team)){
if(game.getHomeTeam().equals(team) && game.getHomePoints() >
game.getVisitingPoints()){
gamesWon++;
}
if(game.getVisitngTeam().equals(team) && game.getVisitingPoints() >
game.getHomePoints()){
gamesWon++;
}
}
}
return gamesWon;
}
**public static int** getGamesLost(ArrayList<Game> record, String team){
int gamesLost = 0;
for(Game game: record){
if (game.getHomeTeam().equals(team) ||
game.getVisitngTeam().equals(team)){
if(game.getHomeTeam().equals(team) && game.getHomePoints() <
game.getVisitingPoints()){
gamesLost++;
}
if(game.getVisitngTeam().equals(team) && game.getVisitingPoints() <
game.getHomePoints()){
gamesLost++;
}
}
}
return gamesLost;
}
}
该错误与参数列表或 return 类型无关。您在静态上下文中使用这些方法(即,您 不是 在特定实例上调用它们),因此它们必须是 static
.
static 只是意味着它不是您从 class 创建的对象的一部分。它独立于您创建的 objects/instances。您可以说它仅涉及 class 的 with/is 部分,而不是功能。
此程序有效,但我不确定为什么使用 ArrayList
的方法必须在方法 header 中包含 static
。仅作为背景,该程序接收一个文本文件,读取它并将其内容保存到 ArrayList<Game>
(游戏)中。用户输入文件名和球队名,程序输出特定球队打了多少场比赛,赢了多少场,输了多少场。
它读取的 .csv 格式如下:
ENCE,Vitality,9,16
ENCE,Vitality,16,12
etc..
以下所有四种方法,一种方法返回 ArrayList
,另三种方法返回 int
,除非我在方法中输入关键字 static
,否则将不起作用 header。如果我只为方法写 public ArrayList<Game>
或 public int
,它们将不起作用。
是否任何时候将 Arraylist
传递给方法,这意味着方法 header 必须始终包含 static
?为什么会这样?
这是工作代码:
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
public class SportStatistics {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("File: ");
String file = scan.nextLine();
ArrayList<Game> record = getGame(file); //After method returns the list of
objects, it is copied over to another list.
System.out.println("Team: ");
String team = scan.nextLine();
//These methods return how many games a team played, has won and has lost.
int gamesPlayed = getGamesPlayed(record, team);
int gamesWon = getGamesWon(record, team);
int gamesLost = getGamesLost(record, team);
System.out.println("Games: " + gamesPlayed);
System.out.println("Wins: " + gamesWon);
System.out.println("Losses: " + gamesLost);
}
//This method takes in the file name given by user and saves file details into
a list of objects.
**public static** ArrayList<Game> getGame(String file){
ArrayList<Game> games = new ArrayList<>();
try(Scanner reader = new Scanner(Paths.get(file))){
while(reader.hasNextLine()){
String input = reader.nextLine();
String[] parts = input.split(",");
String homeTeam = parts[0];
String visitingTeam = parts[1];
int homePoints = Integer.valueOf(parts[2]);
int visitingPoints = Integer.valueOf(parts[3]);
games.add(new Game(homeTeam, visitingTeam, homePoints,
visitingPoints));
}
}
catch(Exception e){
System.out.println("Error: File " + file + " not found.");
}
return games;
}
**public static int** getGamesPlayed(ArrayList<Game> record, String team){
int gamesPlayed = 0;
for(Game game: record){
if (game.getHomeTeam().equals(team) ||
game.getVisitngTeam().equals(team)){
gamesPlayed++;
}
}
return gamesPlayed;
}
**public static int** getGamesWon(ArrayList<Game> record, String team){
int gamesWon = 0;
for(Game game: record){
if (game.getHomeTeam().equals(team) ||
game.getVisitngTeam().equals(team)){
if(game.getHomeTeam().equals(team) && game.getHomePoints() >
game.getVisitingPoints()){
gamesWon++;
}
if(game.getVisitngTeam().equals(team) && game.getVisitingPoints() >
game.getHomePoints()){
gamesWon++;
}
}
}
return gamesWon;
}
**public static int** getGamesLost(ArrayList<Game> record, String team){
int gamesLost = 0;
for(Game game: record){
if (game.getHomeTeam().equals(team) ||
game.getVisitngTeam().equals(team)){
if(game.getHomeTeam().equals(team) && game.getHomePoints() <
game.getVisitingPoints()){
gamesLost++;
}
if(game.getVisitngTeam().equals(team) && game.getVisitingPoints() <
game.getHomePoints()){
gamesLost++;
}
}
}
return gamesLost;
}
}
该错误与参数列表或 return 类型无关。您在静态上下文中使用这些方法(即,您 不是 在特定实例上调用它们),因此它们必须是 static
.
static 只是意味着它不是您从 class 创建的对象的一部分。它独立于您创建的 objects/instances。您可以说它仅涉及 class 的 with/is 部分,而不是功能。