Java创建数组


Ⅰ. 一维数组

1. 声明

(1)int

int[] test_int;

(2)String

String[] test_String;

(3)自定义类

MyClass[] test_mc;

(4)对象数组

Integer[] test_Integer;

2. 初始化

A. 使用关键字new进行定义

类型标识符[] 数组名 = new 类型标识符[数组长度];

(1)int

产生一个具有10个单元,类型为 int 的数组对象,所有单元的初值为0

int[] test_int = new int[10];

int[] test_int;
test_int = new int[10];
(2)String

产生一个具有10个单元,类型为 String 的数组对象,所有单元的初值为null

String[] test_String = new String[10];

String[] test_String;
test_String = new String[10]; // 注意:不要写成 new String(10)
(3)自定义类

产生一个具有10个单元,类型为 MyClass 的数组对象,所有单元的初值为null

MyClass[] test_mc = new MyClass[10];

MyClass[] test_mc;
test_mc = new MyClass[10];
(4)对象数组

产生一个具有10个单元,类型为 Integer 的数组对象,所有单元的初值为null

Integer[] test_Integer = new Integer[10];

Integer[] test_Integer;
test_Integer = new Integer[10];
B. 直接在声明的时候进行初始化

注意:这种定义方式只能在一行代码中,不能分开。

(1)int
int[] test_int = {1,2,3};
(2)String
String[] test_String = {"ab","cd","ef"};

String[] test_String = {new String("ab"),new String("cd"),new String("ef")};
(3)自定义类
MyClass test_mc1 = new MyClass();
MyClass test_mc2 = new MyClass();
MyClass test_mc3 = new MyClass();
MyClass[] test_mc = {test_mc1,test_mc2,test_mc3};
(4)对象数组
Integer[] test_Integer = {3,5};

Integer[] test_Integer = {new Integer(3), new Integer(5)};
C. 采用如下方法定义及初始化
(1)int
int[] test_int = new int[] {1,2,3};
(2)String
String[] test_String = new String[] {"ab","cd","ef"};

String[] test_String = new String[] {new String("ab"), new String("cd"), new String("ef")};
(3)自定义类
MyClass test_mc1 = new MyClass();
MyClass test_mc2 = new MyClass();
MyClass test_mc3 = new MyClass();
MyClass[] test_mc = new MyClass[] {test_mc1,test_mc2,test_mc3};
(4)对象数组
Integer[] test_Integer = new Integer[] {1,2,3};

Integer[] test_Integer = new Integer[] {new Integer(1), new Integer(2), new Integer(3)};

3. 使用foreach语句遍历一维数组

(1)int
public class MyClass {
	public static void main(String[] args) {
		int[] test_int = new int[10];
		test_int[0] = 1;
		for(int x: test_int) {
			System.out.println(x + " ");
		}
	}
}

输出:
在这里插入图片描述

(2)String
public class MyClass {
	public static void main(String[] args) {
		String[] test_String = {"ab","cd","ef"};
		for(String x: test_String) {
			System.out.print(x + " ");
		}
	}
}

输出:
在这里插入图片描述

(3)自定义类
public class MyClass {
	public int value = 1;
	public static void main(String[] args) {
		MyClass test_mc1 = new MyClass();
		MyClass test_mc2 = new MyClass();
		MyClass test_mc3 = new MyClass();
		MyClass[] test_mc = {test_mc1,test_mc2,test_mc3};
		for(MyClass x: test_mc) {
			System.out.print(x.value + " ");
		}
		System.out.println();
		for(MyClass x: test_mc) {
			System.out.print(x + " ");
		}
	}
}

输出:
在这里插入图片描述

(4)对象数组
public class MyClass {
	public int value = 1;
	public static void main(String[] args) {
		Integer[] test_Integer = {new Integer(3), new Integer(5)};
		for(Integer x: test_Integer) {
			System.out.print(x + " ");
		}
	}
}

输出:
在这里插入图片描述


Ⅱ. 二维数组

1. 声明

(1)int

int[][] test_int;

(2)String

String[][] test_String;

(3)自定义类

MyClass[][] test_mc;

(4)对象数组

Integer[][] test_Integer;

2. 初始化

数组名 = new 类型说明符[数组长度][];
数组名 = new 类型说明符[数组长度][数组长度];

对于没有初始化的维度,其值为null:

int arr[][];
arr = new int[3][4];

其相当于下述4条语句:

arr = new int[3][]; // 创建一个有3个元素的数组,且每个元素也是一个数组
arr[0] = new int[4]; // 创建arr[0]元素的数组,它有4个元素
arr[1] = new int[4]; // 创建arr[1]元素的数组,它有4个元素
arr[2] = new int[4]; // 创建arr[2]元素的数组,它有4个元素

其等价于:

arr = new int[3][];
for(int i=0;i<3;++i) arr[i] = new int[4];

对于二维数组,当只定义一维数组时,另一维的维数可以不一样,也就是说不一定是规则的矩阵形式。如:

int[][] arr;
arr = new int[3][];
arr[0] = new int[3];
arr[1] = new int[2];
arr[2] = new int[1];

3. 使用foreach语句遍历二维数组

for(int[] arr1: arr){
	for(int x: arr1){
		System.out.print(x + " ");
	}
	System.out.println();
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值