无法访问ArrayList对象方法

时间:2013-04-05 03:47:36

标签: java object methods arraylist

我正在尝试编写一个从池中随机选择3个国家/地区的程序,然后将它们作为字符串输出。这些国家将按顺序获得金,银和铜。方法olympicResults将每个字符串添加到一个数组中。最终,所有国家将按获得的金牌,银牌和铜牌数进行分类。

我遇到了eventResult方法的问题 - 我似乎无法从我的国家/地区类访问方法,比如getName() - 只是给了我找不到符号错误。它似乎在我在eventResult方法中初始化ArrayList之前工作,但这不起作用,因为每次调用方法时都会初始化一个新的arraylist。

另一个问题是防止出现在eventResult字符串中的国家重复 - 我做错了什么想法?

代码:

public class OlympicMedals 
{
public static void main (String[] args)
{
    String[] countries = {"CAN", "BRB", "BLR", "HKG", "CHN", "SWE"}
    ArrayList<Country> list = new ArrayList<Country>();
    for (int j=0; j<9; j++) //initialize countries
    {
        list.add(new Country());
        list.get(j).setName(countries[j]);
    }

    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();

    Print(n, list);

}


public static String eventResult (ArrayList list) //generates result of discipline
{
    String res = "";
    Random rand = new Random();
    ArrayList<Country> listCopy = new ArrayList<Country>(list); 

    for (int i=0; i<3; i++) //generate list, prevent repeats
    {  
        int n = listCopy.size();
        int random = rand.nextInt(n);
        res += list.get(random).getName() + " ";

        if (i==0)
            list.get(random).setGold(1);
        if (i==1)
            list.get(random).setSilver(1);
        if (i==2)
            list.get(random).setBronze(1);

        listCopy.remove(random);
    }
    return res;           
}

public static String[] olympicResults(int n, ArrayList list) //array of strings from eventResult
{
    String[] result;
    result = new String[n];

    for (int i=0; i<n; i++)
    {
        result[i] = eventResult(list);
    }
    return result;
}

public static void Print(int n, ArrayList list)
{
    for (int i=0; i<n; i++) //print results
    {
        System.out.println(olympicResults(n, list)[i]);
    }
}

国家/地区类:

public class Country 
{
private int gold;
private int silver;
private int bronze;
private String name;

public Country() //default constructor
{
    setName("");
    setGold(0);
    setSilver(0);
    setBronze(0);
}

public void setName(String n)
{
    name = n;
}

public String getName()
{
    return name;
}

public void setGold(int g)
{
    gold += g;
}

public int getGold()
{
    return gold;
}

public void setSilver(int s)
{
    silver += s;
}

public int getSilver()
{
    return silver;
}

public void setBronze(int b)
{
    bronze += b;
}

public int getBronze()
{
    return bronze;
}

}

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码更改吗?

public static String eventResult (ArrayList<Country> list) //generates result of discipline
{
    String res = "";
    Random rand = new Random();
    ArrayList<Country> listCopy = new ArrayList<Country>(list); 

    for (int i=0; i<3; i++)  { 
        int random = rand.nextInt(listCopy.size());
        res += listCopy.get(random).getName() + " ";
        if (i==0)
            listCopy .get(random).setGold(1);
        if (i==1)
            listCopy .get(random).setSilver(1);
        if (i==2)
            listCopy .get(random).setBronze(1);

        listCopy.remove(random); // remove the country which received a medal from the list
    }    
    return res;           
}