有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

对于“偶数团队”代码,java迭代运行得更快

我正在编写代码,根据球员的得分(puntajes)创建两个“偶数队”

该算法在球员数组中运行,比较每个球员的得分以获得最小的差异,然后将球员分成两个数组,每个队一个

这是我的代码:

if (listaDeJugadores.size() == 6) 
//In this case I'm looking for a 6 player array, to create 3 vs 3 teams, but I'm looking to do until 22 (11 vs 11). Any ideas are welcomed.
{
        int dif1 = Math.abs((listaDeJugadores.get(0).getPuntaje() + listaDeJugadores.get(1).getPuntaje() + listaDeJugadores.get(2).getPuntaje())
                - (listaDeJugadores.get(3).getPuntaje() + listaDeJugadores.get(4).getPuntaje() + listaDeJugadores.get(5).getPuntaje()));
        int jugador1 = 0;
        int jugador2 = 1;
        int jugador3 = 2;
        int jugador4 = 3;
        int jugador5 = 4;
        int jugador6 = 5;
        int a = 0;
        int b = 0;
        int c = 0;

//The two fors are to search the arrays. The iterador is to find the other three remaining positions to compare.
            for (int cont2 = 1; cont2 < listaDeJugadores.size() - 1; cont2++) {
                for (int cont3 = cont2 + 1; cont3 < listaDeJugadores.size(); cont3++) {
                    ArrayList<Integer> arr = new ArrayList<>();
                    int iterador[] = {0,1,2,3,4,5,6};
                    int j = 1;
                    for (int i=0;i<iterador.length;i++)
                    {
                        //I look for the missing players to compare from the 6 possible
                        if (cont2==iterador[i]|cont3==iterador[i])
                        {
                            j++;
                        }
                        else
                        {
                            c=b;
                            b=a;
                            a=j;
                            i--;
                            j++;
                        }
                    }

                    int dif = Math.abs((listaDeJugadores.get(0).getPuntaje() + listaDeJugadores.get(cont2).getPuntaje() + listaDeJugadores.get(cont3).getPuntaje())
                            - (listaDeJugadores.get(a).getPuntaje() + listaDeJugadores.get(b).getPuntaje() + listaDeJugadores.get(c).getPuntaje()));
                    if (dif < dif1) {
                        dif = dif1;
                        jugador1 = 0;
                        jugador2 = cont2;
                        jugador3 = cont3;
                        jugador4 = a;
                        jugador5 = b;
                        jugador6 = c;
                    }
                }
        }
        //I add the best available sorted teams to EquipoBlanco or EquipoNegro.
        listaEquipoBlanco.add(listaDeJugadores.get(jugador1));
        listaEquipoBlanco.add(listaDeJugadores.get(jugador2));
        listaEquipoBlanco.add(listaDeJugadores.get(jugador3));
        listaEquipoNegro.add(listaDeJugadores.get(jugador4));
        listaEquipoNegro.add(listaDeJugadores.get(jugador5));
        listaEquipoNegro.add(listaDeJugadores.get(jugador6));

        team1.setText("Equipo Blanco: " + (listaEquipoBlanco.get(0).getPuntaje() + listaEquipoBlanco.get(1).getPuntaje() + listaEquipoBlanco.get(2).getPuntaje()));
        team2.setText("Equipo Negro: " + (listaEquipoNegro.get(0).getPuntaje() + listaEquipoNegro.get(1).getPuntaje() + listaEquipoNegro.get(2).getPuntaje()));

我认为代码还可以,但当我尝试运行它时,它不会打开,因为它的性能非常差。我在想我可能已经迭代到无穷大或类似的东西,但当我看到它,看到fors的内部fors的内部fors的内部fors时,我知道有些东西是错的

如何使其运行更快并具有更好的性能


共 (1) 个答案

  1. # 1 楼答案

    快速查看一下,内部for循环看起来很可疑。如果不尝试,我可能是错的(糟糕的我),但它有一个i ;在那里,而i是循环索引,所以这种情况总是发生,或者经常发生,你永远不会退出那个循环

    如果这不是真的,就会发生这种情况:cont2==iterador[i]|cont3==iterador[i](按位或,顺便说一句,应该是逻辑的或||),不确定这在某个时候一定是真的吗?甚至可能来回。cont2和CONT3没有改变,但是i可以稍微改变一下

    没有保护i降到零以下,尽管这两种情况都可能崩溃和烧毁(例外)