zoukankan      html  css  js  c++  java
  • Java数组

    7.1数组

    l  数组是多个相同类型数据的组合,实现对这些数据的统一管理

    l  数组中的元素可以是任何数据类型,包括基本数据类型和引用数据类型

    l  数组属引用类型,数组型数据是对象(object),数组中的每个元素相当于该对象的成员变量

    7.2一维数组

    public class TestArray {

           public static void main(String[] args) {

                  int i1;

                  i1 = 12;

                  boolean b = true;

                  //1.如何定义一个数组

                  //1.1数组的声明

                  String[] names;

                  int scores[];

    //【注】Java语言中声明数组时不能指定其长度(数组中元素的数), 例如: int a[5]; //非法

                  //1.2初始化

                  //第一种:静态初始化:初始化数组与给数组元素赋值同时进行。

                  names = new String[]{"周爽","郭强强","俞乾龙"};

                  //第二种:动态初始化:初始化数组与给数组元素赋值分开进行。

                  scores = new int[4];

                  //2.如何调用相应的数组元素:通过数组元素的下角标的方式来调用。

                  //下角标从0开始,到n -1 结束。其中n表示的数组的长度。

                  scores[0] = 87;

                  scores[1] = 89;

                  scores[3] = 98;

                  //3.数组的长度:通过数组的length属性。

                  System.out.println(names.length);//3

                  System.out.println(scores.length);//4

                  //4.如何遍历数组元素

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

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

                  }}}

    //声明数组的错误写法:

         1)String[] names = new String[5]{"AA","BB","CC"};//右边[ ]中的5不用写了

         2)int i[10];//正确写法:int i[] = new int[10];

         3)int i = new int[];//[ ]中要指明数组长度

    【注】不管是动态还是静态初始化数组,一定在创建的时候,就指明了数组的长度!每个数组都有一个属性length指明它的长度,例如:a.length 指明数组a的长度(元素个数)。数组一旦初始化,其长度是不可变的。

    关于数组元素的默认初始化值

    //关于数组元素的默认初始化值:

    1)byte short int long 而言:0

    2)float double 而言:0.0

    3)char而言:空格

    4)boolean而言:false

    5)引用类型变量而言:null

    //初始化时的一些其他正确写法:

    1.Int[]  myInt;  myInt = new int[]{1,2,3};

    2.int[]  myInt = {1,2,3};

    // 错误:

    int myInt;

    myInt = {1,2,3};

    //数组的内存结构

     

    【练习1】

    /*

      1.(1)定义类Pritimive,在类中定义一个有3个元素的boolean类型的数组t作为其成员变量。数组元素未赋值。

           定义类TestPritimive,在TestPritimive的main()方法中创建Pritimive对象d,输出其成员变量t的三个元素值。

           练习目的:检验基本数据类型数组创建时的自动赋值。

       (2)给对象d的成员变量t赋值为{true,true,true},并输出t的三个元素值。

     */

    public class TestPritimive {

           public static void main(String[] args) {

                  //创建Pritimive的对象d

                  Pritimive d = new Pritimive();

                  //遍历d的数组元素

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

                         System.out.println(d.t[i]);

                  }

                  //d的数组元素重新赋值

                  d.t[0] = true;

                  d.t[1] = true;

                  d.t[2] = true;

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

                         System.out.println(d.t[i]);

                  }

           }

    }

    class Pritimive{

           boolean[] t = new boolean[3];

    }

    【练习2】

    /*  从键盘读入学生成绩,找出最高分,并输出学生成绩等级。

           成绩>=最高分-10    等级为’A’  

           成绩>=最高分-20    等级为’B’

           成绩>=最高分-30    等级为’C’  

           其余               等级为’D’

           提示:先读入学生人数,根据人数创建int数组,存放学生成绩。

    */

    import java.util.Scanner;

    public class TestStudentScore {

           public static void main(String[] args) {

                  //1.创建Scanner的对象,并从键盘获取学生的个数n

                  Scanner s = new Scanner(System.in);

                  System.out.println("请输入学生的个数:");

                  int count = s.nextInt();//count用来记录学生的个数

                  //2.根据输入的学生的个数n,创建一个长度为nint型的数组

                  int[] scores = new int[count];

                  int maxScore = 0;

                  //3.依次从键盘获取n个学生的成绩,并赋给相应的数组元素,并获取n个学生中的最高分

                  System.out.println("请输入" + count + "个成绩:");

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

                         int score = s.nextInt();//依次从键盘获取学生的成绩

                         scores[i] = score;

                         if(scores[i] > maxScore){

                                maxScore = scores[i];

                         }

                  }

                  //4.遍历学生成绩的数组,并根据学生成绩与最高分的差值,赋予相应的等级,并输出

                  System.out.println("最高分为:" + maxScore);

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

                         char level;

                         if(scores[i] >= maxScore - 10){

                                level = 'A';

                         }else if(scores[i] >= maxScore - 20){

                                level = 'B';

                         }else if(scores[i] >= maxScore - 30){

                                level = 'C';

                         }else{

                                level = 'D';

                         }

                         System.out.println("student " + i + " score is " + scores[i] + " grade is " + level);

                  }}}

    //输出效果:

    7.3二维数组

     

    public class TestArray2 {

           public static void main(String[] args) {

                  int[] scores1 = new int[10];

                  int[][] scores2;

                  String[][] names;

    //1.二维数组的初始化

                  scores2 = new int[][]{{1,2,3},{3,4,5},{6}};//静态初始化

    names = new String[6][5];//动态初始化的方式一

                  names = new String[6][];//动态初始化的方式二

                  names[0] = new String[5];

                  names[1] = new String[4];

                  names[2] = new String[7];

                  names[3] = new String[5];

                  names[4] = new String[8];

                  names[5] = new String[5];

    //错误的初始化方式

                  //names = new String[][];

                  //names = new String[][5];

                 

    //2.如何来引用具体的某一个元素

                  int[][] i = new int[3][2];//int[] i[] = new int[3][2];

                  i[1][0] = 90;

                  i[2][1] = 100;

    //3.数组的长度

                  //二维数组的长度:length属性

                  System.out.println(i.length);//3

                  //二维数组中元素的长度

                  System.out.println(i[0].length);//2

                  System.out.println(names.length);//6

                  System.out.println(names[4].length);//8

                  System.out.println();

                  //4.如何遍历二维数组

                  for(int m = 0;m < scores2.length;m++){//控制行数

                         for(int n = 0;n < scores2[m].length;n++){

                                System.out.print(scores2[m][n] + "  ");}

                         System.out.println();}

                  //5.内存结构

                  int[] x,y[];

                  //int[] x;//一维

                  //int[] y[];//二维

                  y = new int[3][2];

                  x = y[0];

                  x[0] = y[1][2];}}

    【练习1】

    public class TestGetSum {

           public static void main(String[] args) {

                  int[][] m = new int[][]{{3,8,2},{2,7},{9,0,1,6}};

                  int sum = 0;

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

                         for(int j = 0;j < m[i].length;j++){

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

                                sum += m[i][j];

                         }

                         System.out.println();

                  }

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

           }

    }

    【练习2】

    【练习3】

    public class TestYangHui {

           public static void main(String[] args) {

                  int[][] yangHui = new int[10][];

    //1.初始化二维数组

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

                         yangHui[i] = new int[i + 1];

                  }

    //2.显式的为二维数组的每个元素赋值

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

                         for(int j = 0;j < yangHui[i].length;j++){

                                yangHui[i][0] = yangHui[i][i] = 1;

                               

                                if(i > 1 && j > 0 && j < i){

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

                                }

                         }

                  }

    //遍历二维数组

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

                         for(int j = 0;j < yangHui[i].length;j++){

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

                         }

                         System.out.println();

                  }

           }

    }

    7.4数组的常见异常

    public class TestException {

           public static void main(String[] args) {

    //1.数组下标越界的异常:java.lang.ArrayIndexOutOfBoundsException

                  int[] i = new int[10];

                  i[0] = 90;

           i[10] = 99;    

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

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

                  }

    //2.空指针的异常:NullPointerException

           //第一种:

                  boolean[] b = new boolean[3];

                  b = null;//b指向的地址变成null了,找不到b[0]

                  System.out.println(b[0]);

                 

           //第二种:

                  String[] str = new String[4];

                  //str[3] = new String("AA");//str[3] = "AA";//没有这两句中的一句,就会出现异常

                  System.out.println(str[3].toString());

                 

           //第三种:

                  int[][] j = new int[3][];

                  j[2][0] = 12;

           }

    }

    7.5数组的常用算法问题

    1.求数组元素的最大值、最小值、平均数、总和等

    【练习】定义一个int型的一维数组,包含10个元素,分别赋一些随机整数,然后求出所有元素的最大值,最小值,平均值,和值,并输出出来。

    【代码】

    public class TestArray3 {

           public static void main(String[] args) {

                  int[] arr = new int[] { 12, 43, 9, 0, -65, -99, 100, 9 };

           // 最大值

                  int max = arr[0];

                  for (int i = 1; i < arr.length; i++) {

                         if (max < arr[i]) {

                                max = arr[i];}}

                  System.out.println("数组的最大值为:" + max);

           // 最小值

                  int min = arr[0];

                  for (int i = 1; i < arr.length; i++) {

                         if (min > arr[i]) {

                                min = arr[i];

                         }

                  }

                  System.out.println("数组的最小值为:" + min);

           // 总和

                  int sum = 0;

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

                         sum += arr[i];

                  }

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

           // 平均数

                  int avg = 0;

                  avg = sum / arr.length;

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

    }

    }

    【输出函数的快捷键】syso +alt+/

    2.数组的复制、反转

    【练习】

    使用简单数组

    (1)创建一个名为TestArray的类,在main()方法中声明array1和array2两个变量,他们是int[]类型的数组。

    (2)使用大括号{},把array1初始化为8个素数:2,3,5,7,11,13,17,19。

    (3)显示array1的内容。

    (4)赋值array2变量等于array1,修改array2中的偶索引元素,使其等于索引值(如array[0]=0,array[2]=2)。打印出array1。

    思考:array1和array2是什么关系?array2 = array1:表示将array1的地址值赋给了array2

    拓展:修改题目,实现array2对array1数组的复制

    【代码】修改array2的时候,array1也被改变了,类似“快捷方式”

    public class TestArray {

           public static void main(String[] args) {

                  int[] array1, array2;

                  array1 = new int[] { 2, 3, 5, 7, 11, 13, 17, 19 };

           // 遍历array1

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

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

                  }

                  System.out.println();

                  System.out.println(array1);

                  array2 = array1;

                  System.out.println(array2);

           // 修改array2

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

                         if (i % 2 == 0) {

                                array2[i] = i;

                         }

                  }

           // 遍历array1

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

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

                  }}}

    正确的复制方式:

    public class TestArray3 {

           public static void main(String[] args) {

                  int[] arr = new int[] { 12, 43, 9, 0, -65, -99, 100, 9 };

           // 数组的复制

                  int[] arr1 = new int[arr.length];

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

                         arr1[i] = arr[i];

                  }

           // 数组元素的反转(逆序)

            //方法一:

    for(int i = 0;i < arr.length/2;i++){

                   int temp = arr[i];

                   arr[i] = arr[arr.length-1 - i];

                   arr[arr.length - 1 - i] = temp;

                   }

      //方法二:

           for (int x = 0, y = arr.length - 1; x < y; x++, y--) {

                         int temp = arr[x];

                         arr[x] = arr[y];

                         arr[y] = temp;

                  }

                  System.out.println("反转以后:");

           // 遍历

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

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

    }}}

    3.数组元素的排序(面试前看一看)

     

    l  插入排序

    • 直接插入排序、折半插入排序、希尔(Shell)排序

    l  交换排序

    • 冒泡排序、快速排序(或分区交换排序)

    l  选择排序

    • 简单选择排序、堆排序

    l  归并排序

    l  基数排序

    public class TestArray3 {

           public static void main(String[] args) {

                  int[] arr = new int[] { 12, 43, 9, 0, -65, -99, 100, 9 };

           // 使用冒泡排序使数组元素从小到大排列

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

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

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

                                       int temp = arr[j];

                                       arr[j] = arr[j + 1];

                                       arr[j + 1] = temp;}

                         }

                  }

           //使用直接选择排序使数组元素从小到大排列

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

                         int t = i; //默认i处是最小的

                         for(int j = i;j < arr.length;j++){

                                //一旦在i后发现存在比其小的元素,就记录那个元素的下角标

                                if(arr[t] > arr[j]){

                                       t = j;}}

                         if(t != i){

                                int temp = arr[t];

                                arr[t] = arr[i];

                                arr[i] = temp;}

                  }

                  Arrays.sort(arr);

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

           // 遍历

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

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

                  }}}

  • 相关阅读:
    k8s中pod的定义
    k8s上pod一次pending解决过程
    docker创建一个容器
    什么是进程
    在线程中显示一个窗口(多个UI线程)
    ComboBox过滤
    TreeView虚拟化跳转
    使用MVVM的常见误区(1)在ViewModel中和用户交互
    <转载>玩转git, rebase
    MySQL8.0 connector JAR包的下载位置及JDBC配置
  • 原文地址:https://www.cnblogs.com/superjishere/p/11785439.html
Copyright © 2011-2022 走看看