JAVA集合 ==> JAVA基础练习题 - 集合练习十道题

1.产生10个1-100的随机数,并放到一个数组中,把数组中大于等于10的数字放到一个list集合中,并打印到控制台。

public class Test {
    public static void main(String[] args){
        Random random = new Random();   // 随机数
        int[] arr = new int[10]; // 十个数的数组
        for (int i = 0; i < 10; i++) {  // 数组中放入随机数
            arr[i] = random.nextInt(100) + 1; // 要求1-100的随机数,所以要 +1
        }
        List list = new ArrayList(); // 创建一个集合
        for (int i = 0; i < arr.length; i++) { // 循环数组,判断数组元素是否大于10
            if (arr[i] >= 10){  //如果大于十,放入集合中
                list.add(arr[i]);
            }
        }
        System.out.println(Arrays.toString(arr)); // 打印数组
        System.out.println(list); // 打印集合
        /* 结果 :
            [8, 53, 10, 20, 72, 95, 70, 80, 2, 29]
            [53, 10, 20, 72, 95, 70, 80, 29]
         */
    }
}

 


2.已知数组存放一批QQ号码,QQ号码最长为11位,最短为5位String[] strs = {"12345","67891","12347809933","98765432102","67891","12347809933"}。 将该数组里面的所有qq号都存放在LinkedList中,将list中重复元素删除,将list中所有元素分别用迭代器和增强for循环打印出来。

public class Test {
    public static void main(String[] args){
        String[] strs = {"12345","67891","12347809933","98765432102","67891","12347809933"}; // 已知qq号
        List list = new LinkedList(); // 创建集合
        for (int i = 0; i < strs.length; i++) {// 循环strs数组的元素
            list.add(strs[i]); // 把数组的元素遍历到集合中
        }
        // 判断元素是否重复,双循环,外循环控制元素和后面的元素依次对比,内循环取出元素,如果元素相同,则删除
        int a = list.size(); // 设置a为list集合的长度
        for (int i = 0; i < a; i++) {
            String str = (String) list.get(i); // 设置一个数,跟后面的数进行对比
            for (int j = i + 1; j < a; j++) {
                String str2 = (String) list.get(j);
                if (str.equals(str2)){  // 如果两个数相同,则删除后面的数
                    list.remove(j);
                    a--;    // 同时,数组长度-1
                }
            }
        }
        System.out.println(list);
    }
}

 

 


3.

分别用Comparable和Comparator两个接口对下列四位同学的成绩做降序排序,如果成绩一样,
那在成绩排序的基础上按照年龄由小到大排序。

姓名(String)年龄(int)分数(float)
liusan       20          90.0F
lisi            22          90.0F
wangwu       20          99.0F
sunliu       22          100.0F

编写一个Student类用来实现Comparable<Student>接口,并在其中重写CompareTo(Student o)方法 
在主函数中使用Comparable 与 Comparetor分别对ArrayList进行排序.

public class Test {
    public static void main(String[] args){
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student("liusan", 20, 90.0f));
        set.add(new Student("lisi", 22, 90.0f));
        set.add(new Student("wangwu", 20, 99.0f));
        set.add(new Student("sunliu",22, 100.f));
        System.out.println("姓名\t年龄\t分数");
        for(Student s : set){
            System.out.println(s.getName() +" \t" + s.getAge() + "\t\t" + s.getScore());
        }
    }
}



public class Student implements Comparable<Student> {

    private String name;
    private int age;
    private float score;


    @Override
    public int compareTo(Student o) {
        if (this.getScore() == o.getScore()){
            return this.getAge() - o.getAge();
        }
        return (int) (o.getScore() - this.getScore());
    }

    public Student(String name, int a
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值