黑马程序员--Java学习日记之常见类的功能概述(多练习)

来源:互联网 发布:长沙聚丰网络 编辑:程序博客网 时间:2024/06/01 08:59

   ------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------





一.Scanner类:
 1.实例化方式:Scanner sc = new Scanner(System.in);
 2.成员方法:
  1).获取整数:nextInt();
  2).获取字符串:next()或nextLine();
  3).判断是否能获取一个int值:hasNextInt();
  4).其它的hasNextXxx()自己看一下;
二.String类:
 1.String的特点:字符串是常量,其值不可变的;
 2.构造方法:
  public String()
  public String(byte[] bytes)
  public String(byte[] bytes,int offset,int length)
  public String(char[] value)
  public String(char[] value,int offset,int count)
  public String(String original)
 3.判断功能:
  boolean equals(Object obj)
  boolean equalsIgnoreCase(String str)
  boolean contains(String str)
  boolean startsWith(String str)
  boolean endsWith(String str)
  boolean isEmpty()
 4.获取功能:
  int length():区分数组的length属性
  char charAt(int index)
  int indexOf(int ch)
  int indexOf(String str)
  int indexOf(int ch,int fromIndex)
  int indexOf(String str,int fromIndex)
  String substring(int start)
  String substring(int start,int end)
 5.转换功能:
  byte[] getBytes()
  char[] toCharArray()
  static String valueOf(char[] chs)
  static String valueOf(int i)
  String toLowerCase()
  String toUpperCase()
  String concat(String str)
 6.替换功能
  String replace(char old,char new)
  String replace(String old,String new)
   去除字符串两空格 
  String trim()
   按字典顺序比较两个字符串 
  int compareTo(String str)
  int compareToIgnoreCase(String str)

一.StringBuffer:
 1).StringBuffer是一个容器,可以装一些字符串。
 2).它有容量和长度的概念;
 3).StringBuffer和String的区别:
  1).String不可变的;
  2).StringBuffer是容器,内部是可以改变的;
 4).如果需要对一个字符串经常的修改,要考虑使用StringBuffer
 5).StringBuffer和StringBuilder的区别:
  1).StringBuffer是线程安全的。效率低;
  2).StringBuilder是线程不安全的。效率高;
二.算法:
 1.排序:
  1).冒泡排序; 
  2).选择排序:
   A:判断后立即交换
   B:判断后将索引记录下来,之后再交换;效率高;
 2.查找:
  1).二分查找:
   A.注意范围在不断的更改;
三.Arrays类:
 1.对数组操作的一个工具类。没有构造方法;
 2.public static String toString(int[] a)
   public static void sort(int[] a)
   public static int binarySearch(int[] a,int key)

四.包装类:
 1. 基本数据类型  包装类
  byte   java.lang.Byte
  short   java.lang.Short
  int   java.lang.Integer
  long   java.lang.Long
 
  float   java.lang.Float
  double   java.lang.Double
  
  char   java.lang.Character
  boolean   java.lang.Boolean

一.正则表达式:
 1.字符类;[aeiou]
 2.逻辑运算符:&&,| 例子:[a-z&&[^aeiou]]
 3.预定义字符类:\\d,.,\\w....
 4.限定符:
  +:1次或多次;
  ?: 0次或1次;
  *: 0次或多次;
  {n} : 必须n次;
  {n,}: 至少n次;
  {n,m} : 至少n次,最多m次(包含)
 5.分组:
二.Math类:
 public static int abs(int a):
 public static double ceil(double a)
 public static double floor(double a)
 public static int max(int a,int b) min自学
 public static double pow(double a,double b)
 public static double random()
 public static int round(float a) 参数为double的自学
 public static double sqrt(double a)

三.Random类:
 1.有"种子"的概念:
 2.构造方法:
  Random():
  Random(int n):使用n做种子;
 3.使用相同的种子构造出的Random对象,会产生出相同的随机数序列;
 4.成员方法:
  nextInt():在int范围内的随机数
  nextInt(int m):在0 到 m-1范围内的随机数
 5.Random类生成的随机数具有比Math类的random()方法生成的随机性更强。
四.System类:
 1.gc():运行垃圾回收器;
 2.exit(int status):结束虚拟机;
 3.currentTimeMillis():获取当前系统时间的毫秒值;
 4.arraycopy():复制数组;
五.BigInteger类:
 1. public BigInteger add(BigInteger val):加
  public BigInteger subtract(BigInteger val):减
  public BigInteger multiply(BigInteger val):乘
  public BigInteger divide(BigInteger val):除
  public BigInteger[] divideAndRemainder(BigInteger val):除和取模

六.BigDecimal类:
 public BigDecimal add(BigDecimal augend):加
 public BigDecimal subtract(BigDecimal subtrahend):减
 public BigDecimal multiply(BigDecimal multiplicand):乘
 public BigDecimal divide(BigDecimal divisor):除
 public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):除可以指定小数位数及舍入方式;
七.Date类:
 1.构造方法:
  Date():
  Date(long n):使用毫秒构造一个Date
 2.成员方法:
  getTime():获取毫秒值;
  setTime(long n):设置毫秒值
八.SimpleDateFormat类:对日期进行格式化的类;
 1.String转换为Date:
  String str = "2015-5-30";
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  Date date = sdf.parse(str);
 2.Date转换为String:
  Date d = new Date();
  impleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  String str = sdf.format(d);

九.Calendar类
     |--GregorianCalendar类:
      1.构造方法:
   GregorianCalendar()
   GregorianCalendar(int year, int month, int dayOfMonth)
      2.Calendar类的getInstance(),获取一个GregorianCalendar对象。
     |--成员方法:
   get(int field);
   set(int field,int value);
   set(int year,int month,int day);
   add(int field,int value);如果减法,value为负数就可以


集合的引用:


一.Collection接口:
 1.它是List和Set的父接口;
 2.boolean add(E e)
   boolean remove(Object o)
   void clear()
   boolean contains(Object o)
   boolean isEmpty()
   int size()
   ****批量的方法***
   boolean addAll(Collection c)
   boolean removeAll(Collection c)
   boolean containsAll(Collection c)
   boolean retainAll(Collection c)
   *****用于遍历******
   Object[] toArray();
   Iterator(迭代器);
 
二.List接口:
 1.void add(int index,E element)
   E remove(int index)
   E get(int index)
   E set(int index,E element)
   ListIterator listIterator()
 *****用于遍历*******
 使用for循环结合使用Collection的size()和
 List的get()方法
 List list = new ArrayList();
 list.add("aaa");
 list.add("bbb");
 list.add("ccc");
 for(int i = 0;i < list.size(); i++){
  String s = (String)list.get(i);
  System.out.println(s);
 }
 
 2.ListIterator:增加向上遍历的方式;
          Iterator是单向的,只能向下遍历;
三.数据结构:
 1.数组:随机获取元素快,因为使用索引;增、删慢;
 2.链表:查找元素慢;增、删快;
 3.栈:先进后出
 4.队列:先进先出;

一:ArrayList:
 1).没有特有方法,都是使用的父接口中定义的;
   Vector类:
   LinkedList类:
二:泛型:
 1.在定义集合时,可以指定这个集合内只能装什么类型元素,这种方式就是“泛型”
  ArrayList<String> strList = new ArrayList<String>();
  或:
  ArrayList<String> strList = new ArrayList<>();
  或:
  ArrayList<String> strList = new ArrayList();
 2.泛型类:class MyArrayList<T>{}
                 1.T:可以是任何字母;大小写都可以;
                 2.也可以定义多个泛型,中间用逗号隔开;<T,V>
 3.泛型方法:
  class MyArrayList{
   //接收的是什么类型,返回的就是具有这个类型
   public <T> T show(T t){
    return t;
   }
  }
 4.泛型接口:
  interface IA<T>{
   T show(T t);
  }
   子类实现时:
  1.可以继续定义泛型:
   class A <T> implements IA<T>{
    public T show(T t){
    }
   }
  2.可以固定为某个具体类型
   class A  implements IA<String>{
    public String show(String s){
    }
   }
  3.可以将泛型丢弃
   class A implements IA{
    public Object show(Object o){
    }
   }
 5.泛型的通配符:见Demo
  1:<?>:
  2:<? extends E>:
  3:<? super E>:
四:静态导入:
 1.如果需要很频繁的使用某个类中某些静态方法,可以一次性导入这些静态方法,
          或者导入某个静态方法,导入后,使用这些方法时,不用类名,直接使用即可;
    import static java.lang.Math.abs;
    import static java.lang.Math.*;
五:可变参数:
 1.一个方法如果不确定需要多少个参数,可以定义为:可变参数;
 2.可变参数,跟数组是一样的,所以不能"数组"的方法;
 3.在方法内部,对于可变参数,就是用数组的方式去操作;
 4.在编译后,可变参数的声明,就变成了数组的声明;
 5.调用可变参数的方法:可以不传递实参;
六:Arrays类的asList()方法;
 1.能够返回具有形参类型的集合对象,内部已经封装了所有的实参;
七:集合的嵌套:
 1.一个集合内可以嵌套另一个集合;
 2.遍历时要注意,每一层从集合中取出的是什么样的集合;


异常:

一.异常处理:
 1.基本语法:try...catch
 2.多catch:try...catch...catch...catch
   注意:每个catch的异常类型可以是"平级的",不分先后顺序;
  可以可以是"子父关系",父类的必须放在最后;
 3.JDK7的多catch语句:try...catch(异常类型1 |异常类型2 | 异常类型3  e)
   注意:一定要是"平级关系",不能是"子父关系"
 4.finally语句:
  A:try...catch...finally
  B:特点:无论在try中是否发生异常,都会被执行;
 5.throws和throw:
  A:throws
   1).用在方法声明处;
   2).后面写类名;可以有多个;
   3).运行时:
    调用处可以不捕获,也可以捕获;
      非运行时:
    调用处必须捕获;
  B:throw:
   1).用在方法体内;
   2).后面写对象的引用;只能有一个;
   3).运行时:
    a).可以不用声明throws
    b).调用处可以处理,可以不处理;
      非运行时:
    a).方法必须声明throws这个类型的异常;
    b).调用处必须处理;
  
 6.Throwable的几个常用方法:
  1).getMessage():获取异常信息;
  2).toString():调用重写的toString()方法
  3).printStackTrace():打印堆栈异常;
 7.自定义异常:
  1).我们可以根据我们的业务逻辑,编写我们自己的异常类;
  2).自定义一个类,继承自Exception或其它子类即可;
  3).通常会添加一个带String参数的构造方法:指定异常信息;
 8.子类重写父类方法时:
  无论父类方法是否抛出异常,子类方法都可以不抛出任何异常;
  也可以抛出任何的运行时异常;
  如果父类抛出的是"非运行时异常",子类不能抛出比父类更多的"非运行时异常"
 
 
二.File类:
 1.构造方法:
  public File(String pathname)
  public File(String parent,String child)
  public File(File parent,String child)
 2.创建功能
  public boolean createNewFile():
  public boolean mkdir()
  public boolean mkdirs()
 3.删除功能
  public boolean delete()
 4.重命名功能
  public boolean renameTo(File dest)
 5.判断功能
  public boolean isDirectory()
  public boolean isFile()
  public boolean exists()
  public boolean canRead()
  public boolean canWrite()
  public boolean isHidden()
 6.基本获取功能
  public String getAbsolutePath()
  public String getPath()
  public String getName()
  public long length()
  public long lastModified()
 7.高级获取功能
  public String[] list()
  public File[] listFiles()



0 0
原创粉丝点击