Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
How to convert a javascript shuffling function into a java method

Given this javascript method:

function shuffleKeys(a){
        for(let j,i=a.length;i>1;){
         j=Math.floor(rand*i--);
         if (i!=j) [a[i],a[j]]=[a[j],a[i]]
        }
        console.log("shuffled elarray")
        console.log(a)
        return a
    }

I'd like to convert it into a java method. I've found the libraries needed to replace the math functions, but the array swapping is unique to js. I'd like to figure out how to do it in java. This is my attempt so far

private static ArrayList<String> shuffleKeys(ArrayList<String> a){
        for(int j,i=a.size();i>1;){
                double rand = Math.random();
                j=(int) Math.floor(rand*i--);
                if (i!=j) 
                    [a[i],a[j]]=[a[j],a[i]];
        }
        System.out.println("shuffled elarray");
        System.out.println(a);
        return a;
    }

any help figuring out how to fix this piece of code would be greatly appreciated:

if (i!=j) 
        [a[i],a[j]]=[a[j],a[i]];
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda