Data Structures for Junior Java Developer
- by user1639637
Ok,still learning Arrays.
I wrote this code which fills the array named "rand" with random numbers between 0 and 1( exclusive).
I want to start learning Complexity.
the For loop executes n times (100 times) ,every time it takes O(1) time,so the worse case scenario is O(n),am I right?
Also,I used ArrayList to store the 100 elements and I imported "Collections" and used Collections.sort() method to sort the elements.
import java.util.Arrays;
public class random
{
public static void main(String args[])
{
double[] rand=new double[10];
for(int i=0;i<rand.length;i++)
{
rand[i]=(double) Math.random();
System.out.println(rand[i]);
}
Arrays.sort(rand);
System.out.println(Arrays.toString(rand));
}
}
ArrayList:
import java.util.ArrayList;
import java.util.Collections;
public class random
{
public static void main(String args[])
{
ArrayList<Double> MyArrayList=new ArrayList<Double>();
for(int i=0;i<100;i++)
{
MyArrayList.add(Math.random());
}
Collections.sort(MyArrayList);
for(int j=0;j<MyArrayList.size();j++)
{
System.out.println(MyArrayList.get(j));
}
}
}