@wangyupu
2020-06-02T08:49:05.000000Z
字数 1992
阅读 29
java基础
理解数组的作用
掌握数组的定义
掌握数组的基本使用及常用操作
掌握数组常用操作
获取最大、最小值
数字排序
插入数字
数组是一个变量,存储相同数据类型的一组数据
声明一个变量就是在内存空间划出一块合适的空间
声明一个数组就是在内存空间划出一串连续的空间
数组基本要素
标识符
数组元素.
元素下标:从0开始
元素类型
数组长度固定不变,避免数组越界
使用数组四步走
1.声明数组
a = new int[5];
2.分配空间.
a01l=8;
3.赋值
a [0]= a[0l* 10;
4.处理数据
声明数组:告诉计算机数据类型是什么
int[ ] score1;
//lava成绩
int score2[ ];
//C#成绩
String[ ] name;
//1学生姓名
语法
数据类型数组名[];
数据类型[]数组名;
声明数组时不规定数组长度
分配空间:告诉计算机分配几个连续的空间
scores = new int[30];
avgAge = new int[6];
name = new String[30];
声明数组并分配空间
数据类型[]数组名= new数据类型[大小] ;
数组元素根据类型不同,有不同的初始值
方法1:边声明边赋值
int[ ] scores = {89, 79, 76};
int[ ] scores = new int[ ]{89, 79, 76};
不能指定数组长度
方法2:动态地从键盘录入信息并赋值
Scanner input = new Scanner(System.in);
for(int.i=0;i<30;i++){
scores[i] = input.nextlnt();
int [] scores = {60, 80, 90, 70, 85};
double avg;
avg = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4])/5;
int [] scores = {60, 80, 90, 70, 85};
int sum= 0;
double avg;
for(int i= 0; i< scores.length; it+){
for(int score:scores){
sum = sum + scores[i];
sum += score;
}
avg = sum/ scores.length;
score = new int[5] ;
栈内存
堆内存
根据打擂台的规则
max= stu[O] ;
if (stu[1]>max ){
max=stu [1] ;
if (stu[2]>max ){
max=stu [2] ;
使用循环来解决
if (stu[3]>max ){
max=stu [3] ;
}
.....
将成绩保存在数组中
通过比较找到插入位置
该位置元素往后移一-位
插入新成绩
public class test2 {
/**
* @param args
*/
public static void main(String[] args) {
int list[] = {1,9,50,8,9,10,88,99,70,10};
int max = list[0];
for(int i = 0;i<list.length;i++){
if(list[i]>max){
max = list[i];
}
}
System.out.println("max==="+max);
}
}
import java.util.Scanner;
public class test4 {
public static void main(String[] args) {
String [][] subusers = new String[2][3];
/* subusers[0][0] = "和开箱";
* String [][] s = {{"","",""},{"","",""}};
System.out.println(subusers[1][2]);
*/
Scanner sc = new Scanner(System.in);
for(int i = 0;i<subusers.length;i++){
System.out.println("***********************");
System.out.println("开始录入第"+(i+1)+"行学生");
for(int j = 0;j<subusers[i].length;j++){
System.out.println("姓名:");
subusers[i][j] = sc.next();
}
}
System.out.println("-----------------------------");
for(int i = 0;i<subusers.length;i++){
System.out.println("第"+(i+1)+"行学生");
for(int j = 0;j<subusers[i].length;j++){
System.out.println(subusers[i][j]);
}
}
}
}