-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomizeArray.java
53 lines (40 loc) · 971 Bytes
/
RandomizeArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package JavaTutorials;
import java.util.Random;
import java.util.Scanner;
public class RandomizeArray {
int j,temp;
Random rand = new Random();
public RandomizeArray() {
}
public int [] buildArray(int num){
int [] myArray;
myArray = new int [num];
Scanner sc = new Scanner(System.in);
System.out.println("Enter array elements.");
for (int i = 0; i < myArray.length; i++) {
myArray[i] = sc.nextInt();
}
sc.close();
return myArray;
}
public void printArray(int [] myArray){
System.out.print("{ ");
for (int i = 0; i < myArray.length; i++) {
if(i!=myArray.length-1)
System.out.print(myArray[i]+", ");
else
System.out.print(myArray[i]);
}
System.out.print(" }");
}
public void randomizeArray(int [] myArray){
temp = 0;
for (int i = 0; i < myArray.length; i++) {
j = rand.nextInt(myArray.length) + 0;
System.err.print(j);
temp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = temp;
}
}
}