Java基础之数组

一维数组的正确写法:

第一种:

int[] a = new int[5]; //or int a[] = new int[5];

第二种:

int[] b;
b=new int[5];

第三种:

int[] c={1,2,3,4,5}; 
第四种:
int[] d=new int[]{1,2,3,4,5};

二维数组的正确写法:

第一种:

int[][] a = new int[5][4]; //or int a[][] = new int[5][4];

第二种:

int[][] b;
b=new int[5][4];

第三种:

int[][] c={{1,2,5},{3,5,8},{7,41,56},{6,5,7},{89,56,74}}; 

第四种:

int[][] d=new int[][]{{1,2,5},{3,5,8},{7,41,56},{6,5,7},{89,56,74}};


数组中使用Arrays

arrays中常用的方法:

1.排序:

Arrays.sort(数组名)

案例:

String[] hobbies = { "sports", "game", "movie" };
		// 使用Arrays类的sort()方法对数组进行排序
		Arrays.sort(hobbies);
        System.out.println("排序后的数字");
		// 使用Arrays类的toString()方法将数组转换为字符串并输出
		System.out.println( Arrays.toString(hobbies));


2.将数组转换成字符串

Arrays.toString(数组名)

案例:

//2.Arrays类中toSting方法将数组转换成字符串
		int[] a = new int[]{89,26,100,36,45};
		System.out.println("将数字转换成字符串后的结果"+ Arrays.toString(a));


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值