讲座2-一维数组查找统计、应用举例
- 格式:pptx
- 大小:341.65 KB
- 文档页数:28
⼀维数组的使⽤1.什么是数组?数组是⼀种特殊的类型,数组的"地址"是⾸地址,他的值也是⾸地址。
虽然值相同但是类型是不同的。
1.1如何定义⼀个数组/**** @author sadfoo @date 2018年8⽉31⽇* @tips:数组申明、遍历*/public class TestArray {public static void main(String[] args) {// 数组申明或者定义有2种⽅式:// 数组申明String[] names;// 第⼀种:静态初始化,初始化数组与给元素赋值同时进⾏names = new String[] { "数组001", "数组002", "数组003" };// 数组申明int score[];// 第⼆种:动态初始化,初始化数组与给元素赋值分开进⾏score = new int[4];// 通过数组元素下⾓标调⽤,数组从0开始,n-1结束(n表⽰数组的长度)score[0] = 87;score[1] = 88;score[3] = 99;// 数组的长度,通过数组的length属性来调⽤System.out.println("names的分配空间长度为:" + names.length);System.out.println("score的分配空间长度为:" + score.length);// 如何遍历数组元素// NO1:直接打印脚标遍历(累赘)System.out.println("我是names第⼀种数组遍历⽅式:");System.out.println(names[0]);System.out.println(names[1]);System.out.println(names[2]);System.out.println("我是score第⼀种数组遍历⽅式:");System.out.println(score[0]);System.out.println(score[1]);System.out.println(score[3]);// NO2:循环⽅法遍历(优选)System.out.println("我是names第⼆种数组遍历⽅式:");for (int i = 0; i < names.length; i++) {System.out.println(names[i]);}System.out.println("我是score第⼆种数组遍历⽅式:");for (int i = 0; i < score.length; i++) {System.out.println(score[i]);}}}遇到问题:数组下标越界异常1.2默认初始化值// 对于基本数据类型:byte、short、int、long⽽⾔,初始默认值为:0 byte[] bt = new byte[6];// 假设赋⼀个值bt[0] = 90;bt[5] = 99;for (int i = 0; i < bt.length; i++) {System.out.println("byte默认值为:" + bt[i]);}// 对于基本数据类型:float、double⽽⾔,初始默认值为:0.0float[] ft = new float[7];// 假设赋⼆个值ft[1] = 12f;ft[3] = 12f;for (int i = 0; i < ft.length; i++) {System.out.println("float默认值为:" + ft[i]);}// 对于基本数据类型:char⽽⾔,初始默认值为:空格char[] cr = new char[8];for (int i = 0; i < cr.length; i++) {System.out.println("char默认值为:" + cr[i]);}// 对于基本数据类型:boolean⽽⾔,初始默认值为:falseboolean[] bl = new boolean[9];for (int i = 0; i < bl.length; i++) {System.out.println("boolean默认值为:" + bl[i]);}// 对于引⽤类型的变量构成的数据⽽⾔,初始默认值为:null// 数组申明String[] sr = new String[10];sr[0] = "AA";sr[1] = "BB";sr[3] = "DD";for (int i = 0; i < sr.length; i++) {System.out.println("引⽤类型默认值为:" + sr[i]);}1.3数组练习题import java.util.Scanner;/**** @author sadfoo @date 2018年9⽉1⽇* @tips:练习⼀维数组的使⽤*/public class TestStudentScore {@SuppressWarnings("resource")public static void main(String[] args) {/*** 从键盘读⼊学⽣成绩,找出最⾼分,并输出学⽣成绩等级, 成绩>=最⾼分-10:等级为'A' ,成绩>=最⾼分-20:等级为'B', * 成绩>=最⾼分-30:等级为'C' ,其余:等级为D*/// 创建scanner对象,并从键盘获取学⽣的个数nScanner sc = new Scanner(System.in);System.out.println("请输⼊学⽣的个数:");int count = sc.nextInt();// count⽤来记录学⽣的个数// 根据输⼊的学⽣个数n,创建⼀个长度为n的int型数组int[] scores = new int[count];int maxScore = 0;// 依次从键盘获取n个学⽣的成绩,并赋给相应的数组元素,并获取n个学⽣的最⾼分System.out.println("请依次输⼊"+count+"个学⽣的成绩:");for (int i = 0; i < scores.length; i++) {int score = sc.nextInt();// 依次从键盘获取学⽣的成绩scores[i] = score;if (scores[i] > maxScore) {maxScore = scores[i];}}// 遍历学⽣成绩的数组,并根据学⽣成绩最⾼分的差值赋予相应的等级并输出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);}}}/**** @author sadfoo @date 2018年9⽉1⽇* @tips:求⼀个⼆维数组⾥所有数之和*/public class TestGetSum {public static void main(String[] args) {// 创建⼀个⼆维数组并赋值(根据情况采⽤静态还是动态赋值)int[][] m = new int[][] { { 1, 2, 3 }, { 4, 5 }, { 6 } };// 创建sum记录总和int sum = 0;//遍历数组for (int i = 0; i < m.length; i++) {for (int j = 0; j < m[i].length; j++) {System.out.println(m[i][j] + "\t");sum += m[i][j];}System.out.println();}System.out.println("所有⼆维数组的和值尾:"+sum);}}。
要统计一维数组中指定整数的个数,可以使用循环遍历数组,并检查每个元素是否与指定整数相等。
以下是一个简单的Java程序,演示如何实现此功能:
java
public class CountDistinctElementsInArray {
public static void main(String[] args) {
int[] arr = {6, 10, 5, 4, 9, 120, 4, 6, 10};
int target = 6; // 要统计的整数
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
count++;
}
}
System.out.println("数组中指定整数(" + target + ")的个数为:" + count);
}
}
在这个程序中,我们定义了一个整数数组arr,并指定要统计的整数为6。
然后,我们使用一个循环遍历数组中的每个元素,如果当前元素等于指定整数,则将计数器count加1。
最后,输出计数器的值即可。