Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!