栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java初学数组练习题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java初学数组练习题

1.需求说明:有一个存储手机名称的数组,数据如下:String[] phones = {"null","iPhone4革新","iPhone4变化不大","iPhone5销量好","null","iPhone6高大上"};

要求:删除“iPhone5销量好”的元素,删除后把后面的数据依次往前推。

        String[] 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工具类来实现如下功能;需求:①:将一组乱序的字符进行排序;②:进行升序和逆序输出。

        char[] 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}

        int[] 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+"次");
            }
        }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032794.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号