In: Computer Science
IN JAVA: I am using binary and linear search methods in java. How can I Generate a new array with 10000 elements, and initialize the elements to random values using Math.random() and how can i sort the array using Arrays.sort(). I just need examples, no exact code is necessary. The array must be of doubles.
We declare the array of 10000 elements and populate it with Math.random(). The code below shows how to sort the elements.
Code:
import java.util.*;
public class Main {
public static void main(String args[]) {
//declaring the array
double[] arr = new double[10000];
//filling the array with random values
for(int i = 0;i<arr.length;++i){
arr[i] = Math.random();
}
//sorting the array:
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}