一轮明月半空中
今夜无雨
______________________________________那么,开始上课吧....应该到数组了,望同好订正___________
java的数据类型分为两大部分:基本数据类型 和 引用数据类型(数组..类..接口.)
引用传递的就是一个内存的使用权,一块内存空间,可能有多个人同时使用
数组:是一组相关变量的集合.
int i[100] 是伪代码,只是表示下含义
public class Test17{
public static void main(String args[]){
int score[] = null; //声明数组
score = new int[3]; //为数组开辟空间,大小为3
}
}
数组元素通过"数组名称[下标]"的形式保存.
例:public class Test18{
public static void main(String args[]){
int score[] = new int[4];
System.out.println("数组的长度为: " + score.length);
}
}
例:
public class Test19{
public static void main(String args[]){
int score[] = {422,32,532,6,223,42,25,525};
for(int i=0;i<score.length;i++){
System.out.println("score[" + i + "] = " + score[i]);
}
}
}
..............求数组中的最大值和最小值
public class Test20{
public static void main(String args[]){
int score[] = {2342,231,1231,55123,32131,513131,123213,32145213,23131334};
int max =0;
int min =0;
max = min = score[0];
for(int i=0;i<score.length;i++){
if(score[i]>max){
max = score[i];
}
if(score[i]<min){
min = score[i];
}
}
System.out.println("数组中的最大值为: " + max + ";最小值为: " + min );
}
}
_______给数组排序,之冒泡排序
public class Test21{
public static void main(String args[]){
int score[] = {7,6,8,2,5,1,3,4,9,0};//定义一个数组为其静态赋值
for(int i=1;i<score.length;i++){
for(int j=0;j<score.length;j++){
if(score[i]<score[j]){
int temp = score[i];
score[i] = score[j];
score[j] =temp;
}
}
}
for(int i=0;i<score.length;i++){
System.out.println(score[i]);
}
}
}
二维数组:
动态赋值: int score[][]=new int[2][5];
静态赋值: int score[][]={{2,4},{1,32,12},{4,2,5},{12,44,2}};
二维数组的输出:
public class Test22{
public static void main(String args[]){
int score[][]={{2,4},{1,32,12},{4,2,5},{12,44,2}};
for(int i=0;i<score.length;i++){
for(int j=0;j<score[i].length;j++){
System.out.print(score[i][j] + "\t");
}
System.out.println();
}
}
}
______________________________________________嗯,结束了...晚安.___________________