【问题标题】:Unable to access ArrayList object method无法访问 ArrayList 对象方法
【发布时间】:2013-03-27 09:01:21
【问题描述】:

我正在尝试编写一个程序,从池中随机选择 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;
}

}

【问题讨论】:

  • 你应该在任何地方使用泛型,比如eventResult (ArrayList list) 应该是eventResult (ArrayList&lt;Country&gt; list)
  • 先清理你的代码。它有相当多的错误。 syntacticallogical
  • @Apurv 哇哦。那解决了,谢谢!对于重复预防有什么建议吗?
  • 防止国家/地区重复出现在 eventResult 字符串中是什么意思
  • 6,7,8,9countries[j] 怎么没有给出ArrayIndexOutOfBoundsException?我很惊讶。

标签: java object methods arraylist


【解决方案1】:

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

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;           
}

【讨论】:

  • 不起作用。不确定 for 循环应该如何工作。不过谢谢,我会继续努力的。
猜你喜欢
  • 1970-01-01
  • 2013-05-03
  • 2015-10-27
  • 2020-07-31
  • 2014-07-16
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多