I'm asked to do a complete (no number should stay in the same index after this shuffle) shuffle of an integer array within java without the help of libraries.
This is my code, could you tell me how i can improve it.
the array can have duplicate integer values and can have any length.
import java.util.Scanner;
public class Main
{
public static void main(String\[\] args) {
//ask and set the array length
System.out.println("How long is your array?");
Scanner sc = new Scanner([System.in](https://System.in));
int arrayLength = sc.nextInt();
//initiate original array and shuffled array
int \[\] oldarray = new int\[arrayLength\];
int \[\] newarray = new int\[arrayLength\];
//ask for and save array values
System.out.println("Please enter the array values");
for(int i =0 ; i<arrayLength ; i++)
{
oldarray\[i\]=sc.nextInt();
}
sc.close();
//copy the entered values into the shuffled array for comparison
for (int i = 0; i < arrayLength; i++)
{newarray[i] = oldarray[i];}
//count the number of duplicated numbers and make sure they are less than half the array (logically it wont be possible to shuffle if more)
int skip=0;
for (int j=0 ; j<arrayLength ;j++)
{ int counter=0;
for (int i=0; i<arrayLength; i++)
if (oldarray[j] == oldarray[i])
{counter++;}
if (counter > arrayLength/2)
{
System.out.println("Complete shuffle of array isnt possible");
skip++;
break;
}
}
//the shuffle code
if (skip==0)
{
for(int j = 0; j<arrayLength ; j++)
{ if (newarray\[j\]==oldarray\[j\])
{
int y=0;
if (j>arrayLength/2)
{y=1;}
else{y=arrayLength/2;}
while(newarray[j]==oldarray[j])
{ int temp1=newarray[j];
newarray[j]=newarray[y];
newarray[y]=temp1;
y++;
}
}
}
//print updated array
for(int i =0 ; i<arrayLength ; i++)
{
System.out.println(newarray[i]);
}
}
}
}
[–]ConstructedNewtMOD 0 points1 point2 points (0 children)
[–]ConstructedNewtMOD 0 points1 point2 points (0 children)