如何将对象存储到arraylist?

时间:2015-04-22 04:28:49

标签: java object arraylist

所以我试图将团队的对象存储在ArrayList中但是给我一个&#34的错误;非静态变量存储不能从静态上下文中引用"

尝试并搜索但没有运气:/

接受对我的编码技能的任何批评!努力学习不会害怕批评。

package testing;
import java.util.Scanner;
import java.util.ArrayList;
public class Testing {

    /**
     * @param args the command line arguments
     */
    ArrayList<Team> store = new ArrayList<Team>();
    public static void main(String[] args) {
        String tourName , tourDate , location;
        int maxNumberofTeams , avalSoft, avalHard , avalFieldTest;
        Scanner input = new Scanner(System.in);
        System.out.print("Please Enter tournament Name?\n");
        tourName = input.next();
        System.out.print("please Enter tournament Date\n");
        tourDate = input.next();
        System.out.print("please Enter location\n");
        location = input.next();
        System.out.print("Please Enter Max number of Teams\n");
        maxNumberofTeams = input.nextInt();
        System.out.print("Please Enter avalSoft\n");
        avalSoft = input.nextInt();
        System.out.print("Please enter aval Hard\n");
        avalHard = input.nextInt();
        System.out.print("Please Enter avalFieldTest\n");
        avalFieldTest = input.nextInt();
        Tournament tour = new Tournament (tourName , tourDate, location , maxNumberofTeams, avalSoft , avalHard, avalFieldTest);
        for (int i = 1 ; i <= maxNumberofTeams ; i++)
        {
            String teamName , sponsoringSchool , financialSponsor , judgeLocation;
            int teamNumber , noOfTeamMem;
            System.out.print("Please Enter %s team Name\n");
            teamName = input.next();
            System.out.print("Number of Team Memebers\n");
            noOfTeamMem = input.nextInt();
            System.out.print("Please Enter Sponsoring Schoolr\n");
            sponsoringSchool = input.next();
            System.out.print("Please Enter financialSponsor\n");
            financialSponsor = input.next();
            System.out.print("Please Enter judge Location\n");
            judgeLocation = input.next();
            teamNumber = i;

            Team team = new Team(teamName , teamNumber , noOfTeamMem , sponsoringSchool , financialSponsor, judgeLocation);
            store.add(team);
        }

3 个答案:

答案 0 :(得分:3)

您正尝试从static上下文中访问非静态(实例)字段。

Testing课程中创建一个方法,将程序的逻辑放在其中。在main方法中,创建Testing的实例并调用此方法

public static void main(String[] args) {
    Testing testing = new Testing();
    testing.makeItSo();
}

public void makeItSo() {
    String tourName , tourDate , location;
    int maxNumberofTeams , avalSoft, avalHard , avalFieldTest;
    Scanner input = new Scanner(System.in);
    System.out.print("Please Enter tournament Name?\n");
    tourName = input.next();
    System.out.print("please Enter tournament Date\n");
    tourDate = input.next();
    System.out.print("please Enter location\n");
    location = input.next();
    System.out.print("Please Enter Max number of Teams\n");
    maxNumberofTeams = input.nextInt();
    System.out.print("Please Enter avalSoft\n");
    avalSoft = input.nextInt();
    System.out.print("Please enter aval Hard\n");
    avalHard = input.nextInt();
    System.out.print("Please Enter avalFieldTest\n");
    avalFieldTest = input.nextInt();
    Tournament tour = new Tournament (tourName , tourDate, location , maxNumberofTeams, avalSoft , avalHard, avalFieldTest);
    for (int i = 1 ; i <= maxNumberofTeams ; i++)
    {
        String teamName , sponsoringSchool , financialSponsor , judgeLocation;
        int teamNumber , noOfTeamMem;
        System.out.print("Please Enter %s team Name\n");
        teamName = input.next();
        System.out.print("Number of Team Memebers\n");
        noOfTeamMem = input.nextInt();
        System.out.print("Please Enter Sponsoring Schoolr\n");
        sponsoringSchool = input.next();
        System.out.print("Please Enter financialSponsor\n");
        financialSponsor = input.next();
        System.out.print("Please Enter judge Location\n");
        judgeLocation = input.next();
        teamNumber = i;

        Team team = new Team(teamName , teamNumber , noOfTeamMem , sponsoringSchool , financialSponsor, judgeLocation);
        store.add(team);
    }
}

有关详细信息,请参阅Understanding Class Members

答案 1 :(得分:1)

您无法从静态上下文访问非静态成员:main()是一个静态方法,这意味着它是类的一个方法,它只能看到类变量(静态)。

你声明store的方式 - 它是一个实例成员,意思是它“生活”在一个类的实例中。

有两种方法可以从store访问main():第一种是将其声明为静态,第二种是实例化一个新的类实例并使用它来访问自己的私有副本store

答案 2 :(得分:0)

您需要更改

ArrayList store = new ArrayList();

static ArrayList store = new ArrayList();

因为main是静态方法,因此内部使用的变量也应该是静态的。

相关问题