Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当I';您是否已经循环删除重复项?_Java_Arrays_Loops_Arraylist_Duplicates - Fatal编程技术网

Java 当I';您是否已经循环删除重复项?

Java 当I';您是否已经循环删除重复项?,java,arrays,loops,arraylist,duplicates,Java,Arrays,Loops,Arraylist,Duplicates,在我尝试学习Java的过程中,出现了一个令人讨厌的问题 我试图做的是从一个包含竞争对手、位置和三个比赛时间的ArrayList中提取唯一的竞争对手 public Play() { Setup(); String[][] temp = new String[competitors.size()][8]; int j = 0; double total = 0; boolean isFound = false; System.out.pri

在我尝试学习Java的过程中,出现了一个令人讨厌的问题

我试图做的是从一个包含竞争对手、位置和三个比赛时间的ArrayList中提取唯一的竞争对手

  public Play()
    {
    Setup();
    String[][] temp = new String[competitors.size()][8];
    int j = 0;
    double total = 0;
    boolean isFound = false;
    System.out.println("Racer" + "     " + "first venue time");
    for (Event event : events)
    {
       // System.out.println(event.GetCompetitor() + " " + event.GetVenue()  + " " + event.GetSwim()  + " " + event.GetCycle()  + " " + event.GetRun() + " " + (event.GetSwim() + event.GetCycle() + event.GetRun()));
        //System.out.println(event.GetCompetitor());
        for (int i = 0; i < temp.length; i++)
        {
            if (temp[i][0] == (event.GetCompetitor().toString()))
            {
                isFound = true;
                break;
            }
        }

        if (!isFound)
        {
            temp[j][0] = event.GetCompetitor();
            temp[j][1] = (Calculation.GetSeconds(event.GetSwim())) + (Calculation.GetSeconds(event.GetCycle())) + (Calculation.GetSeconds(event.GetRun())) + "";
            temp[j][2] = event.GetSwim() + event.GetCycle() + event.GetRun()+"";
            j++;

        }
        isFound = false;
    }
    for (int i = 0; i < temp.length; i++)
    {
       System.out.println(temp[i][0] + "           " + temp[i][1] + "           " + temp[i][2]);

    }
}
所以,基本上,我试图(比如)收回约翰的名字(我有)和四个场馆的总数,这是我目前无法理解的


非常感谢您的帮助,如果需要更多详细信息或澄清,我将尽可能提供您的
活动。GetCompetitor()。toString()
包括国家名称(“约翰西班牙”、“约翰英格兰”)。这个字符串是不同的,这就是结果包含重复项的原因。更改您的if条件:
if(temp[i][0]=(event.GetCompetitor().toString())
并尝试拆分字符串并获取第一个单词;另外,使用
equals
比较字符串:
if(temp[i][0].equals(event.GetCompetitor().toString().split(“”[0])

哦,我没提到,约翰和西班牙以及约翰和英格兰是分开的。它们都是从多维数组中提取并放入events arraylist的,并使用events.GetCompetitor()和events.getVincement()
event.GetCompetitor()进行检索返回“John England”或“John”?如果返回“johnengland”,请尝试使用split。在另一种情况下,使用
if(temp[i][0].equals(event.GetCompetitor())
进行比较。别忘了使用
equals
而不是
=
它只返回John事件。getVincement()返回英格兰或西班牙。我们同时也在处理它,最接近我想要的方式是这样在底部有一个新的小for循环。这可以检索单个参赛者在每个场地的总时间,但前提是我在temp[g][0]中输入了特定的数字。使用temp[g][0]会在线程“main”java.lang.ArrayIndexOutOfBoundsException:包含该if语句的行上出现10个错误。请替换此行:
if(temp[i][0]==(event.GetCompetitor())
,并使用以下命令:
if(temp[i][0]。equals(event.GetCompetitor())
John Iceland 15.49 20.3 45.5 81.28999999999999
Amie Iceland 14.3 19.59 36.3 70.19
Dave Iceland 12.21 22.3 41.24 75.75
Archer Iceland 13.3 21.2 38.57 73.07
Sofie Iceland 16.58 24.45 41.19 82.22
Jerry Iceland 21.5 25.5 43.2 90.2
Denise Iceland 14.2 22.53 47.23 83.96000000000001
Stuart Iceland 12.43 26.34 38.26 77.03
Amie Spain 18.47 21.28 51.12 90.87
Jerry Spain 21.02 22.5 50.45 93.97
John Spain 19.48 26.45 52.34 98.27000000000001
Stuart Spain 18.2 24.2 56.55 98.94999999999999
Claire Spain 22.5 27.32 52.23 102.05
Dave Spain 20.07 22.45 56.32 98.84
Denise Spain 19.42 24.09 54.52 98.03
Darren Spain 18.22 23.31 53.12 94.65
Dave England 15.52 19.5 34.4 69.41999999999999
Sofie England 15.2 21.22 33.27 69.69
Archer England 13.39 18.35 34.21 65.95
Denise England 14.2 19.19 31.56 64.95
Amie England 13.59 17.51 30.12 61.22
Darren England 13.5 18.56 30.54 62.6
Claire England 15.12 21.19 34.53 70.84
Jerry England 14.54 20.55 35.31 70.4
John France 17.4 21.2 51.2 89.8
Stuart France 13.01 20.34 52.47 85.82
Archer France 12.25 20.02 49.34 81.61
Amie France 12.21 19.59 48.32 80.12
Darren France 14.5 20.0 48.27 82.77000000000001
Sofie France 13.56 21.3 51.23 86.09
Dave France 14.23 21.02 52.44 87.69
Jerry France 14.01 26.59 50.05 90.65