0

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]];
Evan Gertis
  • 1,394
  • 1
  • 14
  • 35

0 Answers0