【问题标题】:Why does a method that uses an ArrayList but returns a primitive type require the method name to include "static"? [duplicate]为什么使用 ArrayList 但返回原始类型的方法要求方法名称包含“静态”? [复制]
【发布时间】:2021-04-04 05:52:34
【问题描述】:

此程序有效,但我不确定为什么使用ArrayList 的方法必须在方法标头中包含static。仅作为背景,该程序接收一个文本文件,读取它并将其内容保存到ArrayList<Game>(游戏)中。用户输入文件名和球队名,程序会输出该球队打了多少场比赛,赢了多少场,输了多少场。

它读取的 .csv 格式如下:

ENCE,Vitality,9,16
ENCE,Vitality,16,12
etc..

以下所有四种方法,一种方法返回ArrayList,另外三种返回ints,除非我在方法标题中输入关键字static,否则将不起作用。如果我只为方法写public ArrayList<Game>public int,它们将不起作用。

在任何时候将Arraylist 传递到方法中是否意味着方法标头必须始终包含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;
    }


}

【问题讨论】:

    标签: java arraylist methods compiler-errors static


    【解决方案1】:

    错误与参数列表或返回类型无关。您在静态上下文中使用这些方法(即,您没有在特定实例上调用它们),因此它们必须是 static

    【讨论】:

      【解决方案2】:

      static 只是意味着它不是您从类创建的对象的一部分。它独立于您创建的对象/实例。你可以说它只是与类相关/是类的一部分,而不是一个特性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        • 2019-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-28
        • 2014-04-23
        相关资源
        最近更新 更多