数组、冒泡排序

数组声明格式:数组类型[ ]  数组名:

                         int  ints[];             int[]  ints;

数组创建的三种方式:

        1、先声明后创建    char[]    chars;         chars=new   char[10];

        2、声明的同时创建数组     double[]   doubles=new   double[10];

        3、直接给数组赋值     string[]   str={1,2,3,4,5,6,7,8,9,0};

数组一旦建立,其长度就已确定无法更改,数组下标是从0开始的,长度属性length,

赋值:Array.fill(arg1,arg2)  第一个参数指定的数组名,第二个参数是要给指定的数组的所有元素赋的常量值。Array.fill(ints,5);

复制:Array.copyof(arg1,arg2)从指定的数组中(ints)复制其所有元素的值到副本数组中(intss),第一个参数代表从哪个数组取值,               第二个代表副本数组的长度;int[]   intss=Arrays.copyof(ints,10);

排序:Array.sort(a);

冒泡排序:

        在要排序的一组数组中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让比较大的数往下沉,较小的往上冒。

        int[]    a={12,24,2,11,56};

        for(int  i=0;i<a.length;i++){

              for(int   j=0;j<a.length-1-i;j++){

                     if(a[j]>a[j+1]){

                           int   temp=a[j];

                           a[j]=a[j+1];

                           a[j+1]=temp;

                     }      

              }

        }

Java集合框架:

       collection{list--ArrayList数据形式存储\LinkedList链条形式存储

                      {set--HashSet无序存储,不能包含重复元素

       List集合三种遍历方式:for循环、增强for循环、Iterator迭代器遍历    例:Iterator    iter=list.iterator();  while(iiter.hasNext()){}

       list.add(2,stus)   把stus插入到下标为2的位置

       list.contains(stus)   判断集合list是否包含stus元素,返回值boolean类型

       list.remove(2)    移除下标为2的元素

       list.clear()   清空集合

       Set集合两种遍历方式:增强for循环、迭代器遍历,不能用for循环,因为没有get方法

猜你喜欢

转载自blog.csdn.net/FUKEXIN_xiaojiang/article/details/82769822