So I wanted to make an array and then print the elements from the array in a random order. But I want each element of the array to be printed once, or the number of times it occurs in the original array.
public class ShufflingAList {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int [] array = {1, 2, 3, 4, 5, 6, 7, 8 ,9, 0};
for (int i = 0; i < array.length-1; i++){
if (array[i]>array[i + 1]^array[i]<array[i+1]){
array[i] = array[(int)(Math.random() * 10)];
}
}
for (int i = 0; i < array.length; i++){
System.out.print(array[i] + " ");
}
}
}
I got this as output, which is not what I wanted:
3 3 4 6 9 9 9 3 9 0
What do I do to print each element of the array only once? Store it in another array and check for duplicates in that array?