码迷,mamicode.com
首页 > 编程语言 > 详细

Java数组(1):数组与多维数组

时间:2018-02-02 00:49:04      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:打印   gate   blog   gpo   ini   mic   使用   for   方法   

我们对数组的基本看法是,你可以创建它们,通过使用整型索引值访问它们的元素,并且他们的尺寸不能改变。

但是有时候我们需要评估,到底是使用数组还是更加灵活的工具。数组是一个简单的线性序列,这使得元素访问非常快速。但是为这种速度所付出的代价是数组大小被固定,并且在其生命周期中不可改变。

无论使用哪种类型的数组,数组标识符只是一个引用。基本类型数组直接保存基本类型的值,对象数组保存一个指向其他对象的引用。

 1 class Apple {
 2     private static int count = 1;
 3 
 4     public Apple() {
 5         System.out.println("Apple" + count++);
 6     }
 7 }
 8 
 9 public class ArrayOptions {
10     public static void main(String[] args) {
11         Apple[] a; // 创建一个Apple类型的数组引用
12         Apple[] b = new Apple[5]; // 创建一个Apple类型的数组对象,但是都指向null
13         System.out.println("b: " + Arrays.toString(b)); // b: [null, null, null, null, null]
14         Apple[] c = new Apple[4];
15         for (int i = 0; i < c.length; i++)
16             c[i] = new Apple(); // Apple1 Apple2 Apple3 Apple4
17         // Aggregate initialization:
18         Apple[] d = {new Apple(), new Apple(), new Apple()}; // Apple5 Apple6 Apple7
19         // Dynamic aggregate initialization:
20         a = new Apple[]{new Apple(), new Apple()}; // Apple8 Apple9
21         System.out.println("a.length = " + a.length); // a.length = 2
22         System.out.println("b.length = " + b.length); // b.length = 5
23         System.out.println("c.length = " + c.length); // c.length = 4
24         System.out.println("d.length = " + d.length); // d.length = 3
25         a = d;
26         System.out.println("a.length = " + a.length); // a.length = 3
27 
28         int[] e; // 创建一个基本类型的数组引用
29         int[] f = new int[5]; // 创建一个Apple基本数组对象,初始化值都为0
30         System.out.println("f: " + Arrays.toString(f)); // f: [0, 0, 0, 0, 0]
31         int[] g = new int[4];
32         for (int i = 0; i < g.length; i++)
33             g[i] = i * i;
34         int[] h = {11, 47, 93};
35         System.out.println("f.length = " + f.length); // f.length = 5
36         System.out.println("g.length = " + g.length); // g.length = 4
37         System.out.println("h.length = " + h.length); // h.length = 3
38         e = h;
39         System.out.println("e.length = " + e.length); // e.length = 3
40         e = new int[]{1, 2};
41         System.out.println("e.length = " + e.length); // e.length = 2
42     }
43 }

多维数组

 1 import java.util.Arrays;
 2 
 3 class Apple {
 4     private static int count = 1;
 5 
 6     public String toString() {
 7         return "Apple" + count++;
 8     }
 9 }
10 
11 public class MutiArray {
12     public static void main(String[] args) {
13         // (1) 使用Arrays.deepToString()可以打印多维数组
14         int[][] a = {{1, 2, 3}, {4, 5, 6}};
15         long[][][] b = new long[2][2][3];
16         System.out.println(Arrays.deepToString(a)); // [[1, 2, 3], [4, 5, 6]]
17         System.out.println(Arrays.deepToString(b)); // [[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]]
18 
19         // (2) 多维数组值的设定可以用下面方法,并且数组中构成矩阵的每个向量都可具有任意长度(粗糙数组)
20         int[][] c = new int[3][];
21         for (int i = 0; i < c.length; i++) {
22             c[i] = new int[i + 1];
23             for (int j = 0; j < c[i].length; j++) {
24                 c[i][j] = j + 1;
25             }
26         }
27         System.out.println(Arrays.deepToString(c)); // [[1], [1, 2], [1, 2, 3]]
28 
29         // (3) 用类似方式还可以处理非基本类型的(包装类和对象类)数组
30         Apple[][] d = {{new Apple(), new Apple()}, {new Apple(), new Apple(), new Apple()}};
31         Integer[][] e = {{1, 2}, {3, 4, 5}, {6, 7}};
32         System.out.println(Arrays.deepToString(d)); // [[Apple1, Apple2], [Apple3, Apple4, Apple5]]
33         System.out.println(Arrays.deepToString(e)); // [[1, 2], [3, 4, 5], [6, 7]]
34     }
35 }

 

Java数组(1):数组与多维数组

标签:打印   gate   blog   gpo   ini   mic   使用   for   方法   

原文地址:https://www.cnblogs.com/storml/p/8401593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!