Java数组 及应用范例

来源:互联网 发布:python 打包 发布网站 编辑:程序博客网 时间:2024/06/13 03:52
/*数组 数组时一组相关数据的集合,一个数组实际上是一组变量,数据可以分为一维数组、二维数组和多维数组。1,一维数组一维数组可以存放上千万个数据,并且这些数据的类型是完全相同的。在Java中使用数组必须经过声明数组和分配内存给数据两个步骤,这两个步骤的语法结构如下:数据类型  数组名[] =  new 数据类型[] 数据类型[]  数组名 =  new 数据类型[] 比如 int[] score = new int[3]具体明细步骤是:1,int[] score = null // 声明整数型数组score                2,score = new int[]   // 将数组分配内存空间,其元素个数是3.之所以第一步=NULL,因为数组时引用数据类型,对于引用数据类型来说,默认值是nul,表示暂时还没有任何执行的内存空间,后来java升级后可以不用给数据默认值。通常情况下我们简化两步为一步:int[] score = new int[]处理数组数组的元素类型和数组的大小都是确定的,所以当处理数组元素时候,我们通常使用基本循环或者 foreach 循环2,多维数组多维数组可以看成是数组的数组,比如二维数组就是一个特殊的一维数组,其每一个元素都是一个一维数组,例如定义方式:String str[][] = new String[3][4];String str[][][] = new String[3][4][5];拿二维数组来说,它就是一个二维表格     *声明二维数组:有两行,列数待定,数组结构 = { { }, { } }     String s[][] = new String[2][];      *确定每行的元素个数,第一行有2个元素,第二行有3个元素,     *数组结构 = {{"E1", "E2"}, {"E1", "E2", "E3"}}     s[0] = new String[2];     s[1] = new String[3];*/

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------数组的静态初始化

package lz;public class ArrayDemo01 {public static void main (String [] args){int score[] = {9,8,6,5,85,96,75,59};for (int x = 0;x < score.length;x++){System.out.println("score["+x+"] = "+score[x]);}}}
控制台输出

score[0] = 9
score[1] = 8
score[2] = 6
score[3] = 5
score[4] = 85
score[5] = 96
score[6] = 75
score[7] = 59

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------求数组中的最大值和最小值

package lz;public class ArrayDemo02 {public static void main(String [] args) {int score[] = {9,8,6,5,85,96,75,59};int max = 0;int min = 0;max = min = score[0];for(int x = 0;x <score.length;x++) {if(score[x]>max) {max = score[x];}if(score[x]<min) {min = score[x];}}System.out.println("最高成绩:" + max);System.out.println("最低成绩:" + min);}}
控制台输出结果:

最高成绩:96
最低成绩:5

//3,让数组按照从大到小排序输出package lz;public class ArrayDemo03 {public static void main(String [] args) {int score[] = {9,8,6,5,85,96,75,59};for(int i = 1; i <score.length; i++ ) {for(int j = 0; j <score.length; j++) {if(score[i] > score[j]) {int y = score[i];score[i] = score[j];score[j] = y;}}}for(int i = 0; i <score.length; i++ ) {System.out.print(score[i]+"\t");}}}
-----

//4,让数组按照从大到小排序输出,算法实现过程--冒泡算法package lz;public class ArrayDemo04 {public static void main(String [] args) {int score[] = {9,8,6,5,85,96,75,59};for(int i = 1; i <score.length; i++ ) {for(int j = 0; j <score.length; j++) {if(score[i] > score[j]) {int y = score[i];score[i] = score[j];score[j] = y;}}System.out.println("第"+i+"次排序结果:");for(int j = 0; j <score.length; j++) {System.out.print(score[j]+"\t");}System.out.println("");}System.out.println("最终结果:");for(int i = 0; i <score.length; i++ ) {System.out.print(score[i]+"\t");}}}
输出结果:
第1次排序结果:
9 5 8685 96 75 59
第2次排序结果:
9 8 5685 96 75 59
第3次排序结果:
9 8 6585 96 75 59
第4次排序结果:
85 9 865 96 75 59
第5次排序结果:
96 85 986 5 75 59
第6次排序结果:
96 85 7598 6 5 59
第7次排序结果:
96 85 75599 8 6 5
最终结果:
96 85 75599 8 6 5

//二维数组
package lz;public class ArrayDemo05{public static void main(String[] args){int score[][] ={{12,34,25},{1,2,3,56,89},{2,4,6,5,4,8}};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("");}}}
输出结果:

12 34 25
1 2 356 89
2 4 65 4 8