您好,欢迎来到星星旅游。
搜索
您的当前位置:首页从头认识java-4.8 数组的初始化(1)

从头认识java-4.8 数组的初始化(1)

来源:星星旅游
从头认识java-4.8 数组的初始化(1)

这一章节我们来讨论一下数组的初始化。 1.数组是一个对象。

[java] view plaincopy

1. package com.ray.ch01; 2.

3. public class Test {

4. public static void main(String[] args) { 5. int[] a = {};

6. System.out.println(a instanceof Object); 7. } 8. }

输出: true

2.把数组赋值给变量,其实是把数组的引用赋值给变量。

[java] view plaincopy

1. package com.ray.ch04; 2.

3. import java.util.Arrays; 4.

5. public class Test {

6. public static void main(String[] args) { 7. int[] a1 = { 1, 2, 3, 4, 5 }; 8. int[] a2; 9. a2 = a1;

10. System.out.println(a2.equals(a1)); 11. for (int i = 0; i < a2.length; i++) { 12. a2[i] += 1; 13. }

14. System.out.println(Arrays.toString(a1)); 15. }

16. }

输出: true [2, 3, 4, 5, 6]

从上面的结果可以看见,a1、a2指向同一个对象,当改变a2时,a1也同时改变。

数组不单可以放基础类型,还可以放对象。

[java] view plaincopy

1. package com.ray.ch04; 2.

3. public class Test {

4. public static void main(String[] args) {

5. Integer[] a = { new Integer(1), new Integer(2) }; 6. } 7. }

3.读取长度超过数组长度时,抛异常 改动一下上面的代码

[java] view plaincopy

1. package com.ray.ch04; 2.

3. import java.util.Arrays; 4.

5. public class Test {

6. public static void main(String[] args) { 7. int[] a1 = { 1, 2, 3, 4, 5 }; 8. int[] a2; 9. a2 = a1;

10. System.out.println(a2.equals(a1));

11. for (int i = 0; i <= a2.length; i++) {//改动这里,多了一个=号 12. a2[i] += 1; 13. }

14. System.out.println(Arrays.toString(a1)); 15. } 16. }

输出: true

Exception in thread \"main\" java.lang.ArrayIndexOutOfBoundsException: 5

4.除了上面显性赋值之外,我们还可以使用new来创建数组,但是必须写清楚数组的长度

[java] view plaincopy

at com.ray.ch04.Test.main(Test.java:12)

1. package com.ray.ch04; 2.

3. public class Test {

4. public static void main(String[] args) { 5. int[] a2 = new int[5];//必须写清楚长度 6. } 7. }

这里有可能会有疑问,为什么int也可以使用new,因为它后面加上了“[]”,它代表的不是基础类型的int,而是代表一个放置int类型的数组,数组是对象,所以可以使用new 5.赋值

如果是基础类型,会直接把数据发放到数组里面,如果是类,则会把引用放到数组里面。 基础类型:

[java] view plaincopy

1. package com.ray.ch04; 2.

3. import java.util.Arrays; 4. import java.util.Random; 5.

6. public class Test {

7. public static void main(String[] args) { 8. int[] a2 = new int[5];

9. Random random = new Random(50); 10. for (int i = 0; i < a2.length; i++) { 11. a2[i] = random.nextInt(); 12. }

13. System.out.println(Arrays.toString(a2)); 14. } 15. }

输出:

[-1160871061, -1727040520, -1657178909, -765924271, -1625295794] 对象:

[java] view plaincopy

1. package com.ray.ch04; 2.

3. import java.util.Arrays; 4.

5. public class Test {

6. public static void main(String[] args) { 7. Book[] a2 = new Book[5];

8. for (int i = 0; i < a2.length; i++) { 9. a2[i] = new Book(); 10. }

11. System.out.println(Arrays.toString(a2)); 12. } 13. } 14.

15. class Book { 16. }

输出:

[com.ray.ch04.Book@61de33, com.ray.ch04.Book@14318bb, com.ray.ch04.Book@ca0b6, com.ray.ch04.Book@10b30a7, com.ray.ch04.Book@1a758cb]

总结:这一章节简单讨论了数组的几个注意点。

这一章节就到这里,谢谢。 ----------------------------------- 目录

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- stra.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务