4、数组

一.数组

(1)定义数组
语法:数据类型[] 数组名;
数据类型 数组名[];

(2)指定数组大小:
语法:数组名=new 数据类型[size];

(3)给数组赋值
语法:数组名[index]=值;
注意:数组index索引下标从0开始

(4)取出数组的某个值
语法:数组名[index];

(5)数组的属性
语法:数组名.length;
作用:求得数组的长度

二、示例

2.1 示例:简单定义数组并赋值

 1 package com.test;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         //1.定义数组
 6         int[] score;
 7         
 8         //2.给数组指定大小
 9         score=new int[5];
10         
11         //3.给数组赋值
12         score[0]=15;
13         score[1]=26;
14         score[2]=36;
15         score[3]=46;
16         
17         //4.取出数组中的值
18         int num=score[1];
19         System.out.println(num);
20         
21     }
22 
23 }

2.2 示例:循环给数组赋值和取值

 1 package com.test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Test1 {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         Scanner input=new Scanner(System.in);
12         System.out.print("请指定数组的大小:");
13         int size=input.nextInt();
14         
15         //1.定义并确定数组大小
16         int[] score=new int[size];
17         //通过数组名.length获取数组的空间大小
18         System.out.println("数组的空间大小:"+score.length);
19     
20         //2.循环给数组赋值
21         for (int i = 0; i < score.length; i++) {
22             System.out.print("请输入第:"+(i+1)+"个值");
23             score[i]=input.nextInt();
24             //score[i]=(int) (Math.random()*10);
25             System.out.println("完成第"+(i+1)+"次赋值");
26         }
27         
28         //3.循环取值
29         for (int i = 0; i < score.length; i++) {
30             System.out.println("第"+i+"位置上的值为:"+score[i]);
31         }
32 
33     }
34 
35 }

 2.3 示例:用foreach循环打印数组

 1 package com.test;
 2 
 3 import java.util.Scanner;
 4 
 5 
 6 public class Test2 {
 7     public static void main(String[] args) {
 8         Scanner input=new Scanner(System.in);
 9         int num[]=new int[]{8,4,54,36,35,8};
10         int sum=0;
11         //for(数组的数据类型或集合的数据类型  循环增量:数组或集合的名称)
12         for (int i : num) {
13             System.out.println(i);
14         }
15         
16         /*System.out.println("请输入一个数字");
17         int shuzi=input.nextInt();*/
18         
19         /*for(int i=0;i<num.length;i++){
20             if(num[i]==shuzi){
21                 System.out.println("对应的索引下标:"+i);
22                 System.out.println("数组中包含此数为"+num[i]);
23             }
24             sum+=num[i];
25         }
26         */
27         
28         
29         System.out.println("数组的和是:"+sum);
30     }
31 
32 }

2.4 示例:请数组最大值

 1 package com.test;
 2 /**
 3  * 求最大值
 4  * @author Dell
 5  *
 6  */
 7 public class Test {
 8     public static void main(String[] args) {
 9         int[] score=new int[5];
10         score[0]=23;
11         score[1]=20;
12         score[2]=2;
13         score[3]=30;
14         score[2]=18;
15         int max=score[0];
16         for (int i = 1; i < score.length; i++) {
17             if(score[i]>max){
18                 max=score[i];
19             }
20         }
21         System.out.println("最大值为:"+max);
22     }
23 
24 }

2.5 示例:求数组最小值

 1 package com.test;
 2 /**
 3  * 求最小值
 4  * @author Dell
 5  *
 6  */
 7 public class Test1 {
 8     public static void main(String[] args) {
 9         int[] score=new int[5];
10         score[0]=99;
11         score[1]=100;
12         score[2]=82;
13         score[3]=63;
14         score[4]=88;
15         
16         int min=score[0];
17         for (int i = 1; i < score.length; i++) {
18             if(min>score[i]){
19                 min=score[i];
20             }
21         }
22         System.out.println("最大值为:"+min);
23     }
24 }

2.6 示例: 3.在原有降序数组中插入值,完后还是降序

 1 package com.test;
 2 /**
 3  * 插入一个值,插入完之后是降序
 4  * @author Dell
 5  *
 6  */
 7 public class Test3 {
 8     public static void main(String[] args) {
 9         int[] score=new int[6];
10         score[0]=99;
11         score[1]=98;
12         score[2]=97;
13         score[3]=96;
14         score[4]=18;
15         int index=0;//定义要插入的所有位置
16         int num=6; //要插入的值
17         //确定要插入的位置
18        for (int i = 0; i < score.length; i++) {
19           if(num>score[i]){
20               index=i;
21               break;
22           }
23        }
24        
25        //循环将元素后移
26        for (int j = score.length-1; j >index; j--) {
27           score[j]=score[j-1];
28        }
29        
30        //插入元素
31        score[index]=num;
32        
33        //插入完成之后
34        for (int k = 0; k < score.length; k++) {
35         System.out.println(score[k]);
36     }
37     }
38 
39 }

2.7 示例:冒号排序:从小到大

 1 package com.test;
 2 
 3 public class Test4 {
 4 
 5     /**
 6      * 冒号排序:从小到大排列
 7      */
 8     public static void main(String[] args) {
 9          int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
10             int temp=0;  
11             for(int i=0;i<a.length-1;i++){  
12                 for(int j=0;j<a.length-1-i;j++){  
13                 if(a[j]>a[j+1]){  
14                     temp=a[j];  
15                     a[j]=a[j+1];  
16                     a[j+1]=temp;  
17                 }  
18                 }  
19             }  
20             for(int i=0;i<a.length;i++)  
21                 System.out.println(a[i]);     
22         }  
23 
24     }

2.8 冒号排序:从大到小

 1 package com.test;
 2 
 3 public class Test5 {
 4 
 5     /**
 6      * 冒号排序:从大到小排列
 7      */
 8     public static void main(String[] args) {
 9          int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
10             int temp=0;  
11             for(int i=0;i<a.length-1;i++){  
12                 for(int j=0;j<a.length-1-i;j++){  
13                 if(a[j]<a[j+1]){  
14                     temp=a[j];  
15                     a[j]=a[j+1];  
16                     a[j+1]=temp;  
17                 }  
18                 }  
19             }  
20             for(int i=0;i<a.length;i++)  
21                 System.out.println(a[i]);     
22         }  
23 
24     }

2.9 示例 :Arrays类的使用

 1 package com.test;
 2 
 3 import java.util.Arrays;
 4 
 5 /**
 6  * Arrays类的使用
 7  * @author pc
 8  *
 9  */
10 public class Test6 {
11     public static void main(String[] args) {
12         int[] score=new int[5];
13         score[0]=99;
14         score[1]=16;
15         score[2]=97;
16         score[3]=88;
17         score[4]=60;
18         //完成数组升序排序
19         Arrays.sort(score);
20         
21         for (int i : score) {
22             System.out.println(i);
23         }
24         
25         //将数组变成字符串
26         System.out.println(Arrays.toString(score));
27 
28     }
29 
30 }

2.10 示例 :二维数组的使用

 1 package com.test;
 2 
 3 public class Test7 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         int[][] xy=new int[2][3];
10         xy[0][0]=90;
11         xy[0][1]=70;
12         xy[0][2]=50;
13         xy[1][0]=80;
14         xy[1][1]=80;
15         xy[1][2]=40;
16         
17     }
18 
19 }

 

posted @ 2016-07-05 22:07  红酒人生  阅读(387)  评论(0编辑  收藏  举报