0
好的,仍在学习数组。 我写了这个代码,填充名为“rand”的数组,其中0和1之间的随机数(独占)。 我想开始学习复杂性。 For循环执行n次(100次),每次需要O(1)次,所以最坏的情况是O(n),对吗? 此外,我用ArrayList来存储100个元素,我导入了“Collections”,并使用Collections.sort()方法对元素进行排序。Sorting Array,Sorting ArrayList,using Collections
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));
}
}
}
那么你的问题是什么? – Tudor