具体用法可以参考 http://www.cnblogs.com/YSO1983/archive/2011/08/22/2149711.html
不同于JavaScript,Java中数组不是一个数组Array走天下
区别:
- Array([]):最高效;但是其容量定且无法动态改变;
- ArrayList:容量可动态增长;但牺牲效率;
个人认为,虽然Arrays高效,但太不灵活,实际中几乎很少用到,所以重点记录一下ArrayList的用法 demo如下:
public class Play02 {
public static void main(String[] args) {
int[] scores = {89 , -23 , 64 , 91 , 119 , 52 , 73};
Play02 hello = new Play02();
hello.showTop3(scores);
}
public void showTop3(int[] scores){
ArrayList List = new ArrayList(); //新建一个ArrayList对象
for(int i=0; i<scores.length; i++){
if(scores[i]>=1 && scores[i]<=100){
List.add(scores[i]); //往ArrayList插入一个元素
}
}
Collections.sort(List); //类似Arrays的升序方法
for(int j=List.size()-3; j<List.size(); j++){
System.out.println(List.get(j)); //ArrayList的获取对应下标的元素的方法
}
/*
* System.out.println(List);
* 不同于Arrays,ArrayList可以直接打印出来
*/
}
}