欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA-基础-数组

程序员文章站 2023-11-15 19:29:58
一、数组概念 数组是长度固定内容可变的存储多个同一类型数据的容器。 二、定义数组 方式一: 这种方式定义的数组,只定义了长度而没有指定初始值,则初始值采用默认值。 数值类型为0,char型为 ‘a’,boolean型为false,引用类型为null。 方式二: 这种方式定义的数组,指定了数组的初始值 ......

一、数组概念

    数组是长度固定内容可变的存储多个同一类型数据的容器。

 

二、定义数组

    方式一:

      JAVA-基础-数组

      这种方式定义的数组,只定义了长度而没有指定初始值,则初始值采用默认值。

      数值类型为0,char型为 ‘a’,boolean型为false,引用类型为null。

    方式二:

      JAVA-基础-数组

      这种方式定义的数组,指定了数组的初始值,而数组的长度等于元素的个数。

    方式三:

      JAVA-基础-数组

      这种方式定义的数组,指定了数组的初始值,而数组的长度等于元素的个数。

   案例:

    

 1 import java.util.scanner;
 2 /**
 3     数组 - 定义
 4 */
 5 public class demo07{
 6     public static void main(string args[]){
 7         //1.定义数组方式一
 8         
 9         int [] nums1 = new int[5];
10         int nums2 [] = new int[5];
11         char [] cs = new char[5];
12         boolean [] bs = new boolean[5];
13         string [] strs = new string[5];
14         system.out.println(nums1[0]);
15         system.out.println(cs[0]);
16         system.out.println(bs[2]);
17         system.out.println(strs[3]);
18         
19 
20         //2.定义数组方式二
21         /*
22         int [] nums = new int[]{1,3,5,7,9};
23         */
24 
25         //3.定义数组方式三
26         /*
27         int [] nums = {2,4,6,8,10};
28         */
29     }
30 }

三、数组的基本操作

  1.获取数组中的元素

    通过 数组引用[元素的索引]就可以访问到数组中的元素。所谓的索引就是元素在数组中的位置,索引由0开始,最大的索引是  数组的长度-1 。

  

1             //1.获取数组中的元素
2             int []  nums = {1,3,5,7,9};
3             system.out.println(nums[0]);
4             system.out.println(nums[3]);
5             system.out.println(nums[4]);

  2.获取数组的长度

    通过访问  数组的 length属性  可以得到数组的长度。

     1 //2.获取数组的长度 2 int [] nums = {1,3,5,7,9}; 3 system.out.println(nums.length); 

   3.遍历数组

    

 1             //3.遍历数组 - 普通for
 2             int []  nums = {1,3,5,7,9,11,13};
 3             for(int i=0;i<nums.length;i++){
 4                     system.out.println(nums[i]);
 5             }
 6             //4.遍历数组 - 增强for循环
 7             int []  nums = {1,3,5,7,9};
 8             for(int x : nums){
 9                     system.out.println(x);
10             }

  4.修改数组中的元素

    

1             //5.修改数组中的元素
2             int [] nums = {1,3,5,7,9};
3             nums[2] = 100;
4             for(int x : nums){
5                     system.out.print(x+" ");
6             }

 

 四、数组常用应用

  a.  获取数组中  最大 / 最小 的值

 1             //6.获取数组中 最大值/最小值
 2             int [] nums = {-23,-5,-3,-234,-2};
 3             if(nums.length > 0){
 4                     int max = nums[0];
 5                     for(int x : nums){
 6                             if(x>max){
 7                                     max = x;
 8                             }
 9                     }
10                     system.out.println(max);
11             }else{
12                     system.out.println("数组为空!");
13             }

 

  b. 查找数组中元素所在位置

    

1             //7.查找指定元素所在的位置
2             int [] nums = {2,3,65,23,6,3,78,9};
3             int find = 3;
4             for(int i=0;i<nums.length;i++){
5                     if(find == nums[i]){
6                             system.out.println("数字"+find+"出现在数组"+i+"位");
7                     }
8             }

  c. 反转数组

    

1             //8.反转数组  0--4  1--3 2 -- 2  3 -- 1 4 --0
2             int nums [] = {2,3,65,23,6};
3             int nums2 [] = new int[nums.length];
4             for(int i=0;i<nums.length;i++){
5                     nums2[nums.length - 1 - i] = nums[i];
6             }
7             for(int x : nums2){
8                     system.out.println(x);
9             }

 

 

完整案例:

    

 1 import java.util.scanner;
 2 /**
 3     数组的应用
 4 */
 5 public class demo08{
 6     public static void main(string args[]){
 7         //1.获取数组中的元素
 8         /*
 9         int []  nums = {1,3,5,7,9};
10         system.out.println(nums[0]);
11         system.out.println(nums[3]);
12         system.out.println(nums[4]);
13         */
14 
15         //2.获取数组的长度
16         /*
17         int []  nums = {1,3,5,7,9};
18         system.out.println(nums.length);
19         */
20 
21         //3.遍历数组 - 普通for循环
22         /*
23         int []  nums = {1,3,5,7,9};
24         for(int i=0;i<nums.length;i++){
25             system.out.println(nums[i]);
26         }
27         */
28         //4.遍历数组 - 增强for循环
29         /*
30         int []  nums = {1,3,5,7,9};
31         for(int x : nums){
32             system.out.println(x);
33         }
34         */
35         //5.修改数组中的元素
36         /*
37         int [] nums = {1,3,5,7,9};
38         nums[2] = 100;
39         for(int x : nums){
40             system.out.print(x+" ");
41         }
42         */
43         /*
44         //6.获取数组中 最大值/最小值
45         int [] nums = {-23,-5,-3,-234,-2};
46         if(nums.length > 0){
47             int max = nums[0];
48             for(int x : nums){
49                 if(x>max){
50                     max = x;
51                 }
52             }
53             system.out.println(max);
54         }else{
55             system.out.println("数组为空!");
56         }
57         */
58         //7.查找指定元素所在的位置
59         /*
60         int [] nums = {2,3,65,23,6,3,78,9};
61         int find = 3;
62         for(int i=0;i<nums.length;i++){
63             if(find == nums[i]){
64                 system.out.println("数字"+find+"出现在数组"+i+"位");
65             }
66         }
67         */
68         //8.反转数组  0--4  1--3 2 -- 2  3 -- 1 4 --0
69         /*
70         int nums [] = {2,3,65,23,6};
71         int nums2 [] = new int[nums.length];
72         for(int i=0;i<nums.length;i++){
73             nums2[nums.length - 1 - i] = nums[i];
74         }
75         for(int x : nums2){
76             system.out.println(x);
77         }
78         */
79     }
80 }