配套视频教程

本文B站配套视频教程

问题

Java考试完结后,老师给张浩调配了一项工作,让他计算全班(30人)的平均分
int stu1 = 95;int stu2 = 89;int stu3 = 79;int stu4 = 64;int stu5 = 76;int stu6 = 88;……avg = (stu1+stu2+stu3+stu4+stu5…+stu30)/30;

数组

数组是一个变量,存储雷同数据类型的一组数据

申明一个变量就是在内存空间划出一块适合的空间
申明一个数组就是在内存空间划出一串间断的空间

数组基本要素

  • 标识符:数组的名称,用于辨别不同的数组
  • 数组元素:向数组中寄存的数据
  • 元素下标:对数组元素进行编号,从0开始,数组中的每个元素都能够通过下标来拜访
  • 元素类型:数组元素的数据类型

数组长度固定不变,防止数组越界
数组中的所有元素必须属于雷同的数据类型

应用数组步骤:

  1. 申明数组: 通知计算机数据类型是什么
int[ ] score1;             //Java考试成绩int score2[ ];             //oracle考试成绩String[ ] name;        //学生姓名

  1. 调配空间: 通知计算机调配几个间断的空间
score = new int[30]; avgAge = new int[6];     name = new String[30];

申明数组并调配空间
数据类型[ ] 数组名 = new 数据类型[大小] ;

  1. 赋值:向调配的格子里放数据

score[0] = 89;
score[1] = 79;
score[2] = 76;
……

![image.png](http://upload-images.jianshu.io/upload_images/468490-40a98f27e4e44f00.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)办法1: 边申明边赋值

int[ ] score = {89, 79, 76};
int[ ] score = new int[ ]{89, 79, 76};

办法2:动静地从键盘录入信息并赋值

Scanner input = new Scanner(System.in);
for(int i = 0; i < 30; i ++){

 score[i] = input.nextInt();

}

4. 对数据进行解决:计算5位学生的平均分

int [ ] score = {60, 80, 90, 70, 85};
double avg;
avg = (score[0] + score[1] + score[2] + score[3] + score[4])/5;

拜访数组成员:应用“标识符[下标]”

int [ ] score = {60, 80, 90, 70, 85};
int sum = 0;
double avg;
for(int i = 0; i < score.length; i++){

 sum = sum + score[i];

}
avg = sum / score.length;

### 例子>计算全班学员的平均分

public static void main(String[ ] args) {

    int[ ] scores = new int[5];    //问题数组    int sum = 0;            //问题总和    Scanner input = new Scanner(System.in);    System.out.println("请输出5位学员的问题:");    for(int i = 0; i < scores.length; i++){        scores[i] = input.nextInt();        sum = sum + scores[i];    //问题累加    }    System.out.println("平均分是:" + (double)sum/scores.length);  }    
### 数组应用常见谬误

public class ErrorDemo1 {

 public static void main(String[ ] args){      int[ ] score = new int[ ];      score[0] = 89;      score[1] = 63;      System.out.println(score[0]);}

}

public class ErrorDemo2 {

public static void main(String[ ] args) {    int[ ] scores = new int[2];    scores[0] = 90;    scores[1] = 85;    scores[2] = 65;    System.out.println(scores[2]);}

}

public static void main(String[ ] args){

   int[ ] score = new int[5];   score = {60, 80, 90, 70, 85};     int[ ] score2;   score2 = {60, 80, 90, 70, 85}; 

}

#### 一个练习> 有一个数列:8,4,2,1,23,344,12循环输入数列的值求数列中所有数值的和猜数游戏:从键盘中任意输出一个数据,判断数列中是否蕴含此数 

// 有一个数列:8,4,2,1,23,344,12
// 循环输入数列的值
// 求数列中所有数值的和

    //        猜数游戏:从键盘中任意输出一个数据,判断数列中是否蕴含此数    int[] array = {8,4,2,1,23,344,12};    Scanner scanner = new Scanner(System.in);    System.out.println("请输出一个数");    int num = scanner.nextInt();    int i = 0;    //拿数组中的每一个元素和num比拟,如果想等,输入蕴含,否则,输入不蕴含    for(i = 0; i < array.length; i++)    {        if(array[i]==num)        {            System.out.println("蕴含");            break;        }    }    //阐明循环了一圈都没有发现用户输出的值    if(i==array.length)    {        System.out.println("不蕴含");    }
### 数组排序>循环录入5位学员问题,进行升序排列后输入后果应用java.util.Arrays类java.util包提供了许多工具类Arrays类提供操作数组的办法,例排序、查问Arrays类的sort()办法: 对数组进行升序排列
 …… int[] scores = new int[5];    //问题数组Scanner input = new Scanner(System.in);System.out.println("请输出5位学员的问题:");for(int i = 0; i < scores.length; i++){    scores[i] = input.nextInt();}Arrays.sort(scores);            System.out.print("学员问题按升序排列:");for(int i = 0; i < scores.length; i++){    System.out.print(scores[i] + " ");}
### 查找数组中的最大值>从键盘输入本次Java考试五位学生的问题,求考试成绩最高分

// 从键盘输入本次Java考试五位学生的问题,求考试成绩最高分

    //将5个问题保留到数组中,    //而后,遍历数组,找出数组中最大的数    Scanner scanner = new Scanner(System.in);    System.out.println("请输出问题");    int[] scores = new int[5];    //将5个问题保留到数组中,    for(int i = 0; i < scores.length; i++)    {        System.out.println("输出第" + (i + 1) + "次问题");        scores[i] = scanner.nextInt();    }    //而后,遍历数组,找出数组中最大的数    int max = scores[0];////让max等于数组中第一个元素    for(int i = 1; i < scores.length; i++)    {        if(max < scores[i])        {            max = scores[i];//谁比他大,他就变成谁        }    }    System.out.println("最大值是" + max);
### 数组插入算法>有一组学员的问题{99,85,82,63, 60},将它们按升序排列。要减少一个学员的问题,将它插入问题序列,并放弃升序。![image.png](http://upload-images.jianshu.io/upload_images/468490-91ddf9a656c28cc8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)剖析:将问题序列保留在长度为6的数组中通过比拟找到插入地位将该地位后的元素后移一个地位将减少的学员问题插入到该地位 

// 有一组学员的问题{99,85,82,63, 60},将它们按升序排列。
// 要减少一个学员的问题,将它插入问题序列,并放弃升序

    int[] scores = {99,85,82,63, 60};    Arrays.sort(scores);    for(int i = 0; i < scores.length; i++)    {        System.out.print(scores[i]+" ");    }    // 要减少一个学员的问题,将它插入问题序列,并放弃升序    Scanner scanner = new Scanner(System.in);    System.out.println("请输出下一个学生问题");    int input = scanner.nextInt();    int pos = 0;//用户输出的数在新数组中正确的插入地位    //遍历原始数组,找到要插入的地位    for(int i = 0; i < scores.length; i++)    {        if(input <= scores[i])        {            pos = i;            break;        }    }    //再建一个新的数组,蕴含6个元素    int[] scores2 = new int[6];    //拷贝旧数组从0开始    // 到pos地位的数到对应新数组同样下标中    for(int i = 0; i < pos; i++)    {        scores2[i] = scores[i];    }    scores2[pos] = input;    //拷贝旧数组从pos+1开始    // 到旧数组长度地位的数到对应新数组同样下标中    for(int i = pos+1; i < scores2.length; i++)    {        scores2[i] = scores[i-1];    }   for(int i = 0;  i<scores2.length; i++)   {       System.out.print(scores2[i]+" ");   }
### 字符逆序输入将 一组乱序的字符进行排序进行升序和逆序输入  ![image.png](http://upload-images.jianshu.io/upload_images/468490-3af982fb57629829.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)1.创立数组存储原字符序列。2.利用Array类的sort( )办法对数组进行排序,并循环输入。3. 从最初一个元素开始,将数组中的元素逆序输入。
char[] charArray2 = {'a','c','u','b','e','p','f','z'};    //字符串能够看成是字符数组    String str = "abcefpuz";    System.out.println(charArray2.length);    System.out.println(charArray2);    Arrays.sort(charArray2);    System.out.println(charArray2);    for(int i = charArray2.length - 1; i >= 0; i--)    {        System.out.print(charArray2[i]);    }
在上一个练习的根底上改良:    向上一个练习中失去的升序字符序列中插入一个新的字符,要求插入之后字符序列仍放弃有序![image.png](http://upload-images.jianshu.io/upload_images/468490-15d0dc92ea4bddd3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)### 字符串数组>定义一个字符串数组,查找某个字符串在数组中呈现的次数

String[] array = {"zhangsan","lisi","wangwu","lisi"};

    String name = "wangwu";    int count = 0;//count计数,数字num在数组中呈现的次数    for(int i = 0; i < array.length; i++)    {        if(array[i].equals(name))        {            count++;        }    }    System.out.println(count);
### 作业1.歌手打分: 在歌唱较量中,共有10位评委进行打分,在计算歌手得分时,去掉一个最高分,去掉一个最低分,而后残余的8位评委的分数进行均匀,就是该选手的最终得分。输出每个评委的评分,求某选手的得分。2.当初有如下一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}; 要求将以上数组中的0项去掉,将不为0的值存入一个新的数组,生成新的数组为