1.需求说明:有一个存储手机名称的数组,数据如下:String[] phones = {"null","iPhone4革新","iPhone4变化不大","iPhone5销量好","null","iPhone6高大上"};
要求:删除“iPhone5销量好”的元素,删除后把后面的数据依次往前推。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20String[] phones = {"null","iPhone4革新","iPhone4变化不大","iPhone5销量好","null","iPhone6高大上"}; int count = 0; for (int i = 0; i < phones.length; i++) { if (phones[i]=="iPhone5销量好"){ count++; } if (count==1){ for (int j = i; j < phones.length-1; j++) { phones[j] = phones[j+1]; } break; } } String[] new_phones = new String[phones.length-1]; for (int i = 0; i < phones.length-1; i++) { new_phones[i] = phones[i]; } phones = new_phones; System.out.println(Arrays.toString(phones));
2.使用Arrays工具类来实现如下功能;需求:①:将一组乱序的字符进行排序;②:进行升序和逆序输出。
复制代码
1
2
3
4
5
6
7
8
9char[] arr = {'a','c','u','b','e','p','f','z'}; Arrays.sort(arr); System.out.println("升序排序:"+Arrays.toString(arr)); System.out.print("逆序排序为:"); for (int i = arr.length-1; i >= 0; i--) { System.out.print(arr[i]+" "); }
3.统计一个数组当中重复的元素个数,重复的元素分别是谁,重复元素的重复了多少次
数组int[] arr = {20,10,10,30,15,30,100,100,20,0,30,15,0,0,100,100,50,50}
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18int[] arr = {20,10,10,30,15,30,100,100,20,0,30,15,0,0,100,100,50,50}; Arrays.sort(arr); for (int i = 0; i < arr.length; i++) { int count = 0; int repeat = -1; for (int j = 0; j < arr.length; j++) { if (arr[i] == arr[j]){ count++; repeat = j; } } i = repeat; if (count>1){ System.out.println("重复的元素有:"+arr[i]+" 重复了"+count+"次"); } }
最后
以上就是文艺万宝路最近收集整理的关于Java初学数组练习题的全部内容,更多相关Java初学数组练习题内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复