数组

1、数组的定义

         a、数组里面的元素必须是同一个类型

         b、数组一旦创建,长度就不能变化

         数组也是引用类型:

                  1.1静态初始化

                  1.2动态初始化

2、访问数组元素,修改数组元素

         通过下标/索引访问、修改元素

         注意:1、数组下标越界异常  ArrayIndexOutOfBoundsException

                     2、空指针异常   NullPointerException

3、遍历数组

         3.1for循环利用下标遍历

         3.1foreach循环遍历

                   for循环遍历可以改变数组的元素,foreach循环不能改变数组的元素

         {

char[] a={'h',101,'l','l','o'};

                   //1、for循环

                   for(inti=0;i<5;i++){

                            a[i]='\u4e01';//for循环能够修改数组

                            System.out.println(a[i]);

                   }

                   System.out.println("****************");

                   //2、增强for /foreach循环

                   for(charch:a){

                            ch='\u4e02';                       //ch改变了,但是数组没有变

                            System.out.println(ch);

                   }

}

4、数组一般运算

         4.1求取最大值、最小值

         4.2求取平均值

{

         int[]a={9,5,7,8};

                   //求最大值

                   //max中存放a[0]

                   intmax=a[0];

                   intmin=a[0];

                   for(inti=1;i

                            if(max

                                     max=a[i];

                            }

                            if(min> a[i]){

                                     a[i]= min;

                            }

                           

                   }

                   System.out.println("最大值是:"+max);

                   System.out.println("最大值是:"+min);

                   //求和:一项一项相加

                   intsum=0;

                   for(inti=0;i

                            sum+=a[i];

                   }

                   System.out.println("和是:"+sum);

                            //算平均值

                   doubleave = sum / (a.length+1)*1.0;

                   System.out.println("平均值为:"+ave);

}

         4.3反序

         4.4打乱/洗牌shuffle

5、排序

         5.1冒泡排序(常见面试题)

                   列题:

      {

inta[] = { 89, 56, 88, 46,77, 99, 12, 35, 89, 94 };

      System.out.println("排序前:");

      System.out.println(Arrays.toString(a));

      // 冒泡排序法

      inttemp = 0;

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

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

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

                temp = a[j];

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

                a[j + 1] = temp;

            }

         }

      }

      System.out.println("排序后:");

      System.out.println(Arrays.toString(a));

}

  

                   拓展:选择排序

         5.2快速排序。

6、查找

         6.1二分查找(jdk中采用的查找),一定要先排序。

7、二维数组

         7.1二维数组的初始化

         7.2二维数组的内存图:如图所示

         7.3杨辉三角

  

Scanner sc= newScanner(System.in);

      intn = sc.nextInt();

      int[][] a = newint[n][];

 

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

         a[i] = newint[i + 1];

         a[i][0] = 1;

         a[i][i] = 1;

      }

 

      for (inti = 1; i < a.length; i++) {

         for (intj = 1; j < i; j++) {

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

         }

      }

 

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

         for (intj = 0; j < a[i].length; j++) {

            System.out.print(a[i][j] + " ");

         }

         System.out.println();

      }

}

  

你可能感兴趣的:(数组)