为了账号安全,请及时绑定邮箱和手机立即绑定
慕课网数字资源数据库体验端
Java入门第三季_学习笔记_慕课网
为了账号安全,请及时绑定邮箱和手机立即绑定

Java入门第三季

陈码农
难度入门
时长 5小时 0分
  • 集合框架
    查看全部
  • 集合的作用
    查看全部
  • 异常抛出
    查看全部
  • public void Iterator(){ Iterator it= coursesToSelect.iterator(); System.out.println("待选课程如下(通过迭代器访问):"); while(it.hasNext()){ Course cr =(Course) it.next(); System.out.println("课程:"+"cr.id"+":"+cr.name); } }//迭代器遍历:Iterator;函数:.hasNext().迭代器依赖于集合存在,不能独立存在。
    查看全部
  • public void divide(int one,int two)throws Exception{ if(two==0) throw new Exception("两数相除,除数不为0"); else System.out.println("结果为:"+one/two);}
    查看全部
  • throw throws-声明抛出异常类-在方法名后面写throws异常列表
    查看全部
  • 异常处理
    查看全部
    0 采集 收起 来源:经验总结

    2017-02-26

  • Map
    查看全部
  • Set中,添加某个对象,无论添加多少次。最终只会保留一个该对象(的引用),并且,保留的是第一次添加的那一个
    查看全部
  • /** * 测试Comparator接口比较方法,实现compare方法 * 支持Collections.sort() */ public class TestComparator implements Comparator<TestComparator>{ private String name ; public TestComparator() { } public TestComparator(String name) { this.name = name ; } public static void main(String[] args) { TestComparator tct1 = new TestComparator("zhangsan"); TestComparator tct2 = new TestComparator("lisi"); TestComparator tct3 = new TestComparator("wangwu"); List<TestComparator> l = new ArrayList<TestComparator>(); l.add(tct1); l.add(tct2); l.add(tct3); System.out.println("-------------------排序前-------------------"); for (TestComparator e : l) { System.out.println("元素:"+e.name); } Collections.sort(l, new TestComparator()); System.out.println("-------------------排序后-------------------"); for (TestComparator e : l) { System.out.println("元素:"+e.name); } } @Override public int compare(TestComparator o1, TestComparator o2) { return o1.name.compareTo(o2.name); } }
    查看全部
  • /* * 实现Comparable接口,并重写compareTo方法 支持Collections.sort() */ public class TestComparable implements Comparable<TestComparable> { private Integer num; public TestComparable(int num) { this.num = num; } public static void main(String[] args) { TestComparable tc1 = new TestComparable(5); TestComparable tc2 = new TestComparable(2); int result = tc1.compareTo(tc2); System.out.println("tc1和tc2比较结果:" + (result > 0 ? "大" : (result < 0 ? "小" : "相等"))); List<TestComparable> l = new ArrayList<TestComparable>(); TestComparable tc3 = new TestComparable(1); l.add(tc1); l.add(tc2); l.add(tc3); System.out.println("-------------------排序前---------------"); for (TestComparable e : l) { System.out.println("元素:"+e.num); } Collections.sort(l); System.out.println("-------------------排序后---------------"); for (TestComparable e : l) { System.out.println("元素:"+e.num); } } @Override public int compareTo(TestComparable o) { return num.compareTo(o.num); } }
    查看全部
  • java中集合框架有: Collection接口(List接口(ArrayList类)、Queue接口(LinkedList类)、Set接口(HashSet类)) Map接口(HashMap类) Collections工具类(进行Collection对象操作,最常用List的sort方法) Arrays工具类(针对数组进行操作) Comparable接口(默认比较规则)必须重写public int compareTo(T o); A对象compareTo(B对象),如果返回正数,说明A对象比B对象大,返回负数,说明A比B小,返回0,说明相等。 Comparator接口(临时比较规则)必须重写int compare(T o1, T o2);boolean equals(Object obj);
    查看全部
  • Collections.sort()能对集合类List进行排序,但是有条件:List对象的元素所在的类必须实现了Comparable接口,所有的基本类型包装类都实现了Comparable接口,所以支持排序
    查看全部
  • Random类的出现,可以替代之前的Math.random方法; Random类有很多方法:nextByte、nextDouble、nextInt、nextDouble、nextBoolean、nextFloat、nextLong,获取各种类型的随机数,但是没有nextShort和nextCharacter。 并且对于nextInt并且支持传入一个int类型参数,作为随机数的取值范围。 常见用法:nextInt对于从某一个String类型的对象中,获取一个随机位的字符很有用, 例如String str = "sdfsfsfsdsafdf"; Random r = new Random();char c = str.charAt(r.nextInt(str.length)); /** * 获取随机字符串 字符串长度为参数length * @return */ private String getRandomString(int length){ String base = "abcdefghijklmnopqrstuvwxyz0123456789"; StringBuilder sbd = new StringBuilder();//使用StringBuilder效率高,因为它没有实现线程安全 Random r = new Random(); for(int i=0;i<length;i++){ Character c = base.charAt(r.nextInt(base.length()));//从base中随机获取一个字符 boolean flag = r.nextBoolean(); if(flag){ c = Character.toUpperCase(c); } sbd.append(c); } return sbd.toString(); }
    查看全部
  • /* * 用Collections的sort方法对String类型List对象进行排序版本2 * 1、创建完List<String>之后,往其中添加十条随即字符串 * 2、每条字符串的长度为10以内的随机整数 * 3、每条字符串的每个字符都为随机生成的字符,字符可以重复 * 4、每条随机字符串不可重复 */ private void testSortStringList2(){ System.out.println("********************下面测试生成随机字符串并排序********************"); List<String> stringList = new ArrayList<String>(); Random r = new Random(); String randomStr; for(int i=0;i<10;i++){ randomStr = getRandomString(r.nextInt(9)+1); do{ randomStr = getRandomString(r.nextInt(9)+1); }while(stringList.contains(randomStr)); stringList.add(randomStr); System.out.println("添加了随机字符串:"+randomStr); } System.out.println("——————————————————————排序前——————————————————————"); for (String s : stringList) { System.out.println("元素:"+s); } Collections.sort(stringList); System.out.println("——————————————————————排序后——————————————————————"); for (String s : stringList) { System.out.println("元素:"+s); } }
    查看全部

举报

0/150
提交
取消
课程须知
此部分为 Java 课程的进阶内容,适合具有一定 Java 基础的伙伴们学习,如果您是新手,建议您移步 《Java入门第一季》 和 《Java入门第二季》,在理解并掌握面向对象相关知识后再回来进修。
老师告诉你能学到什么?
本课程将学习 Java 中的异常处理、集合框架、字符串、常用类等,逐步学习掌握 Java 高级技术。
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!