第十三周java作业

作业1: 
使用ArrayList集合,对其添加100个不同的元素: 
1.使用add()方法将元素添加到ArrayList集合对象中; 
2.调用集合的iterator()方法获得Iterator对象,并调用Iterator的hasNext()和next()方法,迭代的读取集合中的每个元素; 
3.调用get()方法先后读取索引位置为50和102的元素,要求使用try-catch结构处理下标越界异常

package week13;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.management.RuntimeErrorException;

public class ConnectionDemo01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<Integer> arrayList = new ArrayList<Integer>();

        for(int i = 0; i < 100 ; i++){
            arrayList.add(new Integer((int) (Math.random() * 1000)));
        }

        System.out.println("迭代器输出结果:");
        Iterator<Integer> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().intValue());
        }

        try {
            System.out.print("调用get()读取索引位置为50的元素:");
            System.out.println(arrayList.get(50));
            System.out.print("调用get()读取索引位置为102的元素:");
            System.out.println(arrayList.get(102));
        } catch (IndexOutOfBoundsException e) {
            // TODO Auto-generated catch block
            System.out.println("数组越界了");
            e.printStackTrace();
        }
    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

运行截图: 
这里写图片描述

作业2: 
选择某种Map集合保存学号从1到15的学员的学号(键)和姓名(值),学号用字符串表示,输入的时候要以学号乱序的方式存入Map集合,然后按照学号从大到小的顺序将Map集合中的元素输出打印。需要自定义Map集合的比较器Comparator,因字符串对象的大小比较是按字典序,而非对应的数值。 
要求:必须使用Map集合的内部排序机制进行排序,不能在外部排序。 

package week13;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

public class MapSort{

    public static void main(String[] args) {

        List<Student> list = new ArrayList<Student>();//用来乱序输出的
        for (int i = 1; i < 16; i++) {
            list.add(new Student(String.valueOf(i), "tjw_"+i));
        }

        TreeMapMethod(list);
    }

    private static void TreeMapMethod(List<Student> list) {
        //treeMap按key来从大到小排序
        TreeMap<String, Student> map = new TreeMap<String, Student>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                int sno1 = Integer.valueOf(o1).intValue();
                int sno2 = Integer.valueOf(o2).intValue();
                return sno2 - sno1;
            }
        });
        int listSize = list.size();
        Student student;//用作接收,省去下边一直new
        System.out.println("乱序输入的顺序:");
        while (listSize != 0) {
            int temp = (int) (Math.random() * listSize);
            System.out.println(list.get(temp));//这里打开注释可以查看乱序存入的顺序
            student = list.get(temp);
            map.put(student.Sno, student);
            list.remove(temp);
            listSize -= 1;
        }

        Collection<Student> studentsCollection = map.values();
        Iterator<Student> iterator = studentsCollection.iterator();
        System.out.println("排好序的输出结果:");
        while (iterator.hasNext()) {
            student = iterator.next();
            System.out.println("学号:"+student.Sno+"  "+"姓名:"+student.name);
        }
    }

}

class Student{

    String Sno;
    String name;
    public Student(String sno, String name) {
        super();
        Sno = sno;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student [Sno=" + Sno + ", name=" + name + "]";
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

运行截图
这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值