加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JAVA-基础-数组

发布时间:2020-12-15 05:27:20 所属栏目:Java 来源:网络整理
导读:一、数组概念 数组是长度固定内容可变的存储多个同一类型数据的容器。 ? 二、定义数组 方式一: 这种方式定义的数组,只定义了长度而没有指定初始值,则初始值采用默认值。 数值类型为0,char型为 ‘a’,boolean型为false,引用类型为null。 方式二: 这种

一、数组概念

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

?

二、定义数组

    方式一:

      

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

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

    方式二:

      

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

    方式三:

      

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

?  案例:

    

 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,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,9}; 3 System.out.println(nums.length);?

?  3.遍历数组

    

 1             //3.遍历数组 - 普通for
 2             int []  nums = {1,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,9};  8             for(int x : nums){  9  System.out.println(x); 10             }

  4.修改数组中的元素

    

1             //5.修改数组中的元素
2             int [] nums = {1,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,65,23,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,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,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,9}; 18  System.out.println(nums.length); 19         */
20 
21         //3.遍历数组 - 普通for循环
22         /*
23  int [] nums = {1,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,9}; 31  for(int x : nums){ 32  System.out.println(x); 33  } 34         */
35         //5.修改数组中的元素
36         /*
37  int [] nums = {1,9}; 38  nums[2] = 100; 39  for(int x : nums){ 40  System.out.print(x+" "); 41  } 42         */
43         /*
44  //6.获取数组中 最大值/最小值 45  int [] nums = {-23,-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,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,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 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读