java 数组使用和定义

来源:互联网 发布:nginx 录播保存record 编辑:程序博客网 时间:2024/04/29 09:16
java数组定义及使用

3.1数组

数组是一组相关数据的集合,数组按照使用可以分为一维数组、二维数组、多维数组

有点:
不使用数组定义100个整形变量:int1,int2,int3;;;;;;
使用数组定义 int i[100]


数组定义:int i[100],只是一个伪代码,只能表示含义的。


3.2一维数组

可以存放上千万个数据,并且这些数据的类型是完全是相同的。

要使用java的数组,必须经过两个步骤:(1)声明数组,(2)分配内容给该数组,这两个步骤的语法如下:



声明形式一:

◇声明一维数组: 数据类型 数组名[] = null;
◇分配内存给数组:数组名 = 买受人数据类型[长度]

声明形式二:
◇声明一维数组: 数据类型[]  数组名  = null;

[java] view plaincopyprint?
  1. public class ArrayDemo01{  
  2.     public static void main(String args[]){  
  3.         int score[] = null;    //声明数组  
  4.         score = new int[3];    //为数组开辟空间  
  5.     }  
  6. }  


变量的命名规则: 第一个单词的首字母小写,之后的每个单词的首字母大写:studentName;

详细解释:

"
栈堆
int score[] = null ;  ->null 表示引用数据类型的默认值
int score[];   此时score尚未指向具体的空间,所以score内容未知,无法直接使用。
为数组开辟空间
score = new int[3];
栈内存
堆内存
"

验证只开辟空间却不赋值时数组默认值是多少?

[java] view plaincopyprint?
  1. public class ArrayDemo01{  
  2.     public static void main(String args[]){  
  3.         int score[] = null;    //声明数组  
  4.         score = new int[3];    //为数组开辟空间  
  5.         System.out.println("score[0] = " + score[0]);  
  6.         System.out.println("score[1] = " + score[1]);  
  7.         System.out.println("score[2] = " + score[2]);  
  8.     }  
  9. }  


错误显示:

[java] view plaincopyprint?
  1. public class ArrayDemo01{  
  2.     public static void main(String args[]){  
  3.         int score[] = null;    //声明数组  
  4.         System.out.println("score[0] = " + score[0]);  
  5.         System.out.println("score[1] = " + score[1]);  
  6.         System.out.println("score[2] = " + score[2]);  
  7.     }  
  8. }  




如果删除score = new int[3];时显示结果将如下:
Exception int thread "main"  java.lang.NullPointerException
    at ArrayDemo01.main<ArrayDemo01.java:5>


一个堆内存可以被多个栈内存空间使用。

下面使用一个步骤一次性完成:
即:
声明数组的同时分配内存空间

数据类型 数组名[] = new 数据类型[个数]

int score[] = new int[10];

数组中元素的表示方法:

想要访问数组里的元素,可以利用索引来完成。
数组的访问也可通过循环方式进行操作中,循环操作的时候只需要改变期索引[下标]即可。

[java] view plaincopyprint?
  1. public class ArrayDemo01{  
  2.     public static void main(String args[]){  
  3.         int score[] = null;    //声明数组  
  4.         score = new int[3];    //为数组开辟空间  
  5.         System.out.println("score[0] = " + score[0]);  
  6.         System.out.println("score[1] = " + score[1]);  
  7.         System.out.println("score[2] = " + score[2]);  
  8.         for(int x=0;x<3;x++){  
  9.             System.out.println("score["+x+"] = " + score[x]);      
  10.         }  
  11.     }  
  12. }  




访问注意:


假设程序中取出的内容超过了这个下标,如"score[3]",则的程序运行的时候会出现以下的错误提示:
java.lang.ArrayIndexOutOfBoundsException:3

[java] view plaincopyprint?
  1. public class ArrayDemo01{  
  2.     public static void main(String args[]){  
  3.         int score[] = null;    //声明数组  
  4.         score = new int[3];    //为数组开辟空间  
  5.         System.out.println("score[0] = " + score[0]);  
  6.         System.out.println("score[1] = " + score[1]);  
  7.         System.out.println("score[2] = " + score[2]);  
  8.         for(int x=0;x<3;x++){  
  9.             System.out.println("score["+x+"] = " + score[x]);      
  10.         }  
  11.         System.out.println("score[3] = " + score[3]);  
  12.     }  
  13. }  



Excepiton  in thread "main"  java.lang.ArrayIndexOutOf BoundException : 3
        at ArrayDemo01.main<ArrayDemo01.java:11>



数组索引超出绑定,就是表示数组越界。


数组中默认的内容都是0,也可以通过下标的方式为数组中的内容赋值。


[java] view plaincopyprint?
  1. public class ArrayDemo02{  
  2.     public static void main(String args[]){  
  3.         int score[] = null;    //声明数组  
  4.         score = new int[3];    //为数组开辟空间  
  5.   
  6.         for(int x=0;x<3;x++){     //为每个元素赋值  
  7.             score[x] = x*2+1;   //每一个值都是奇数  
  8.         }  
  9.   
  10.         for(int x=0;x<3;x++){  
  11.             System.out.println("score["+x+"] = " + score[x]);      
  12.         }  
  13.     }  
  14. }  





取得数组的长度
可以使用“数组名称.length”来完成操作。


[java] view plaincopyprint?
  1. public class ArrayDemo03{  
  2.     public static void main(String args[]){  
  3.         int score[] = null;  
  4.         score = new int[3];  
  5.         System.out.println("数组长度为:" + score.length);      
  6.     }  
  7.   
  8. }  




3.3数组的静态初始化

之前所看到的数组,所采用的方式都是动态初始化,即:所有的内容在数组声明的时候并不具体的制定,而都是以默认值的形式以出现。
静态初始化就是指在数组定义之后,直接制定好期内容。




操作格式:
数据类型 数组名[] = {初值0,初值1,....,初值n}

[java] view plaincopyprint?
  1. public class ArrayDemo04{  
  2.     public static void main(String args[]){  
  3.         int score[] = {91,92,93,94,95,96};        //使用静态初始化的方式初始数组。  
  4.         for(int x=0;x<score.length;x++){        //循环输出  
  5.             System.out.println("score["+x+"] = " + score[x]);  
  6.         }  
  7.     }  
  8. }  



3.4观察两道范例:
3.4.1求出最大和最小值:

给出一个数组求出最大值和最小值

[java] view plaincopyprint?
  1. public class ArrayDemo05{  
  2.     public static void main(String args[]){  
  3.         int score[] = {67,89,87,69,90,100,75,90};  //静态初始化数组  
  4.         int max = 0;  
  5.         int min = 0;  
  6.         max = main = score[0] ;                   //把第一个元素的内容赋值给max和min  
  7.         for(int x = 0;x<score.length;x++){        //循环求出最大值和最小值  
  8.             if(score[x]>max){  
  9.                 max = score[x];  
  10.             }  
  11.             if(score[x]<min){  
  12.               
  13.                 min = score[x];  
  14.             }  
  15.         }  
  16.         System.out.println("最高成绩:" + max);  
  17.         System.out.println("最低成绩:" + min);  
  18.     }  
  19. }  



3.4.2排序

在数组中,排序是一个比较常见的算法。
通过一个简单的范例,来了解排序的基本操作。

[java] view plaincopyprint?
  1. public class ArrayDemo06{  
  2.     public static void main(String args[]){  
  3.         int score[] = {67,89,87,69,90,100,75,90};  
  4.         for(int i=0;i<score.length;i++){  
  5.             for(int j=0;j<score.length;j++){  
  6.                 if(score[i]<score[j]){            //交换位置,这里关系到到底是从大到小还是从小到大!  
  7.                     int temp = score[i];            //中间变量  
  8.                     score[i] = score[j];  
  9.                     score[j] = temp;  
  10.                 }  
  11.             }  
  12.         }  
  13.         for(int j=0;j<score.length;j++){            //循环输出  
  14.             System.out.print(score[j]+"\t");  
  15.         }  
  16.     }  
  17. }  



为了让大家可以更好的观察到每次的执行,下面在每次排序的时候都输出原始内容。



[java] view plaincopyprint?
  1. public class ArrayDemo07{  
  2.     public static void main(String args[]){  
  3.         int score[] = {67,89,87,69,90,100,75,90};  
  4.         for(int i=0;i<score.length;i++){  
  5.             for(int j=0;j<score.length;j++){  
  6.                 if(score[i]<score[j]){            //交换位置,这里关系到到底是从大到小还是从小到大!  
  7.                     int temp = score[i];            //中间变量  
  8.                     score[i] = score[j];  
  9.                     score[j] = temp;  
  10.                 }  
  11.             }  
  12.             System.out.println("第" + i +"次排序的结果:\t");  
  13.             for(int j=0;j<score.length;j++){  
  14.                 System.out.print(score[j]+"\t");  
  15.             }  
  16.             System.out.println("");  
  17.         }  
  18.         for(int i=0;i<score.length;i++){            //循环输出  
  19.             System.out.print(score[i]+"\t");  
  20.         }  
  21.     }  
  22. }  



冒泡排序,自己观察运行的过程,来分析代码是如何操作的.

3.5二维数组


二维数组的声明方式和一维数组类似,内存的分配也是一样是用new这个关键字。
其声明与分配内存的格式如下所示:
1.动太初始化:
数据类型 数组名[][]
数组名 = new 数据类型[行的个数][列的个数];
2.声明加初始化
数据类型  数组名[][] = new 数据类型[行的个数][列的个数]
3.二维数组静态初始化
数据类型 数组名[][] = {{第0行初值},{第0行初值},.....{第n行初值}}


如:
int score[][] = null;        //生命
score = new int[3][4];    //分配空间

int score = new int[3][4];


[java] view plaincopyprint?
  1. public  class ArrayDemo08{  
  2.     public static void main(String args[]){  
  3.         int score[][] = new int[4][3];                //声明并实例化二维数组  
  4.   
  5.         score[0][1] = 30;  
  6.         score[1][1] = 30;  
  7.         score[2][2] = 30;  
  8.         score[3][1] = 30;  
  9.         score[1][1] = 30;  
  10.         for(int i=0;i<score.length;i++){  
  11.             for(int j=0;j<score[i].length;j++){  
  12.                 System.out.print(score[i][j] + "\t");  
  13.             }  
  14.             System.out.println();                    //换行  
  15.         }  
  16.     }  
  17. }  





[java] view plaincopyprint?
  1. public class ArrayDemo09{  
  2.     public static void main(String args[]){  
  3.         int score[][] = {{67,61},{78,61,83},{99,100,98,66,95}};            //静态初始化完成,每行的数组元素个数不一样  
  4.       
  5.         for(int i=0;i<score.length;i++){  
  6.             for(int j=0;j<score[i].length;j++){  
  7.                 System.out.println(score[i][j] + "\t");  
  8.             }  
  9.             System.out.println("");  
  10.         }  
  11.     }  
  12. }  




3.6多维数组

三维数组的声明为int score[][][],而四维数组为int  score[][][][].....,以此类推


[java] view plaincopyprint?
  1. public class ArrayDemo10{  
  2.     public static void main(String args[]){  
  3.         int score[][] = {  
  4.         {  
  5.             {5,1},{6,7}  
  6.         },  
  7.         {  
  8.             {9,4},{8,3}  
  9.         }  
  10.         };            //静态初始化完成,每行的数组元素个数不一样  
  11.       
  12.         for(int i=0;i<score.length;i++){  
  13.             for(int j=0;j<score[i].length;j++){  
  14.                 for(int k=0;k<score[i][j].length;k++){  
  15.                 System.out.println("score[" + i + "]["+ j +"]["+ k +"] = " + score[i][j][k] + "\t");  
  16.                 }  
  17.             }  
  18.             System.out.println("");  
  19.         }  
  20.     }  
  21. }  
0 0
原创粉丝点击