JAVA之数组详解

1.创建数组:

先声明:

数组元素类型 数组名[ ];

数字元素类型 [ ]数组名;

eg:int arr[ ]或者int [ ]arr;

再用new运算符进行内存分配:

数组名 = new 数组元素类型[数组元素个数];

eg:arr = new int[5];

或者int arr[ ] = new int [5];

再或者int [ ] arr = new int [5];

2.初始化以为数组:

eg:

int arr[ ] = new int [ ] {1, 2, 3, 4};

int arr[ ] = {1, 2, 3, 4};

二维数组创建方法类似:

声明:int arr[ ][ ]或者int [ ][ ]arr;

分配空间:arr = new int [2][3]; 或者arr = new int[2][ ]; arr[0] = new int[3];arr[1] = new int[3];

初始化:int arr[ ][ ] = {{1, 3}, {2, 4}};

3.数组的基本操作:

⑴:遍历数组:可以使用foreach语句,这样更为简便。

eg:

package Number;
public class IntFunction
{
	public static void main (String []args)
	{
		int b[][] = new int[][]{{1}, {2, 3}, {4, 5, 6}};
		for(int i = 0; i < b.length; i ++)
			for(int j = 0; j < b[i].length; j++)
				System.out.print(b[i][j]);
		System.out.println();
		for(int x[] : b)
			for(int y : x)
				System.out.print(y);
		System.out.println();
	}
}
/*输出结果:
123456
123456
*/

⑵.填充替换数组元素:

需要通过Arrays类的静态方法fill()来完成对数组的元素替换。

Arrays.fill(char [ ]a, char c):将数组中所有元素都替换为c字符;

Arrays.fill(char [ ]a, int m, int n, char c):将数组中索引值为m到n之间的字符替换为c字符(不包括索引值为m的元素)

eg:

package Number;
import java.util.Arrays;
public class IntFunction
{
	public static void main (String []args)
	{
		char ch[] = new char[]{'A', 'S', 'D', 'F'};
		Arrays.fill(ch, 'R');
		for(char c : ch)
			System.out.print(c + "  ");
		System.out.println();
		char ch_1[] = new char[]{'Q', 'W', 'E', 'R', 'T'};
		Arrays.fill(ch_1, 2, 4, 'G');
		for(char c : ch_1)
			System.out.print(c + "  ");
		System.out.println();
	}
}
/*输出结果:
R  R  R  R  
Q  W  G  G  T  
*/

⑶:对数组进行排序:

Arrays.sort(int [ ]a);

eg:

package Number;
import java.util.Arrays;
public class IntFunction
{
	public static void main (String []args)
	{
		int a[] = new int[]{1, 3, 6, 4, 7, 8, 2};
		Arrays.sort(a);
		for(int x : a)
			System.out.print(x);
		System.out.println();
	}
}
/*输出结果:
1234678
*/

⑷:复制数组:

Arrays.copyOf(int [ ]a, int newlength);

复制数组至指定长度。

newlength:int型常量,指复制后新数组的长度,如果新数组的长度大于数组arr的长度,则需要填充,

根据复制数组的类型确定填充的值,整型数组填充0,字符型数组填充null。

Arrays.copyOfRange(int [ ]a, int m, int n);

将数组的指定长度复制到一个新数组中。(范围为有索引值为m到索引值为n的所有元素,不包括索引值为n的元素)。

eg:

package Number;
import java.util.Arrays;
public class IntFunction
{
	public static void main (String []args)
	{
		int a[] = new int[]{1, 3, 6, 4, 7, 8, 2};
		char c[] = new char[]{'A', 'S', 'D', 'F'};
		double d[] = new double[]{1.2, 2.3, 4.3, 5.5};
		int a1[] = Arrays.copyOf(a, 10);
		int a2[] = Arrays.copyOf(a, 5);
		char c1[] = Arrays.copyOf(c, 6);
		double d1[] = Arrays.copyOf(d, 6);
		for(int x : a1)
			System.out.print(x + "#");
		System.out.println();
		for(int x : a2)
			System.out.print(x + "#");
		System.out.println();
		for(char x : c1)
			System.out.print(x + "#");
		System.out.println();
		for(double x : d1)
			System.out.print(x + "#");
		System.out.println();
		int a3[] = Arrays.copyOfRange(a, 2, 5);
		for(int x : a3)
			System.out.print(x + "#");
		System.out.println();
	}
}
/*输出结果:
1#3#6#4#7#8#2#0#0#0#
1#3#6#4#7#
A#S#D#F#
*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值