Data Structures for Junior Java Developer
Posted
by
user1639637
on Stack Overflow
See other posts from Stack Overflow
or by user1639637
Published on 2012-09-01T15:36:13Z
Indexed on
2012/09/01
15:37 UTC
Read the original article
Hit count: 271
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));
}
}
}
© Stack Overflow or respective owner