码迷,mamicode.com
首页 > 编程语言 > 详细

20165310java_blog_week6

时间:2018-04-08 22:25:04      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:getchar   offset   添加   引用   https   旋转   hashmap   学习   获取   

2165310 《Java程序设计》第6周学习总结

教材学习内容总结

String

  • 构造
    • String str=new String()
    • String (char a[])
    • String (char a[],int startIndex,int count)
  • 并置
    • 常量的并置是常量(可以用==进行比较)
    • 变量的并置是新对象(不能用==只能用equals()进行比较
  • 常用方法
    • public int length():获得String对象的字符序列长度。
    • public boolean equals(String s) :比较字符序列是否相同。
    • 比较
      1. public boolean startsWith(String s):比较字符序列前缀是否为指定字符序列。
      2. public boolean endsWith(String s):比较字符序列后缀是否为指定字符序列。
      3. public int compareTo(String s):按字典序列与指定字符序列s比较大小。
    • 索引
      1. public boolean contains(String s):判断是否包含指定 字符序列
      2. public int indexOf(String s):返回s首次出现的位置
      3. public int lastIndexOf(String s):返回s最后出现的位置
      4. public int indexOf(String s,int startpoint):返回指定索引开始位置之后的,s首次出现的位置
    • 从String获得String新对象
      1. public String substring(int startpoint):返回复制指定位置到最后位置的字符序列所得到的新序列
      2. public String substring(int start,int end):返回复制指定开始位置到指定结束位置的字符序列所得到的新序列
  • 字符串与基本指定数据类型的相互转化
    • 基本数据类型转化为String类:public static String valueOf(<基本数据类型> n)
    • String类型转化为基本数据类型:<基本数据类型> n=<相应类>.parse<相应数据类型>(s)
  • 对象的字符串表示:public String toString():返回创建对象类名@对象的引用的字符串,可重写。
  • 字符串与字节数组、字节数组
    • 字符串转化为字符数组
      1. public void getChars(int start,int end,char c[],int offset):从数组C的offset处存放String类字符序列sart到end-1处的字符。
      2. public char[] toCharArray():返回一个长度相等的数组。
    • 字符数组转化为字符串:见String类构造
    • 字符串转化为字节数组
      1. public byte[] getBytes():使用默认字符编码,存放String的字符序列到字节数组中,并返回引用。
      2. public byte[] getBytes(String charsetName):使用指定字符编码,存放String的字符序列到字节数组中,并返回引用。(可能返回UnsupportedEncodingException异常)
    • 字符串转化为字符数组
      1. String(byte[]):用指定字节数组构造一个String对象。
      2. String(byte[],int offset,int length):从字节数组offset位置取length个字节构造String对象。
  • 替换(利用正则表达式
    • public String repalceAll(String regex,String replacement):用replacement字符序列替换regex匹配的序列。
  • 分解(利用正则表达式
    • public String[] split(Sring regex):存放按照regex分割的单词在String数组中。(注意前缀是否匹配影响数组存放

      StringBuff

  • 构造
    • StringBuff()
    • StringBuff(int size)
    • StringBuff(String s)
  • 获得相关信息
    • length():获取字符序列长度
    • capacity():获得实体容量
  • 常用方法:
    • append方法
      1. StringBuff append(String s):追加s到StringBuff中。
      2. StringBuff append(int n):n转化为String类,追加到StringBuff中。
      3. StringBuff append(Object o):追加o的字符序列表示到StringBuff中。
    • public char charAt(int n):得到StringBuff位置n处的字符。
    • public void setChar(int n,char ch):用ch替换StringBuff位置n处的字符。
    • StringBuff insert(int index,String str):在index位置处插入str。
    • public StringBuff reverse():翻转StringBuff字符序列。
    • StringBuff replace(int startIndex,int endIndex,String str):用str替换startIndex到endIndex-1处的字符序列。

      分解字符序列

  • StringTokenizer(不可用正则表达式
    • 构造方法:
      1. StringTokenizer(String s):默认分隔标记分析s。
      2. StringTokenizer(String s,String delim):指定分隔标记delim分析s。
    • 常用方法:
      1. hasMoreTokens():查看计数变量是否大于一。
      2. nextTokens():逐个获取String对象单词。
      3. countTokens():获得计数变量的值。
  • Scanner(可用正则表达式)
    • 构造方法:Scanner scanner=new Scanner(str)
    • 常用方法
      1. useDelimiter(正则表达式):利用正则表达式解析str。
      2. hasnext():判断最后一个单词是否被返回。
      3. next():依次返回单词。
      4. nextInt()或者nextDouble:依次返回单词并进行类型转换为int或者double型。

        正则表达式:

        在假期实验楼学习和书本p186到188中都有,已经整理过不多加赘述。

        Random

  • 构造方法:
    • public Random()
    • public Random(seed)
  • 常用方法
    • nextInt(int n):返回0-n之间的某个整数。
    • nextDouble():返回0-1.0之间的随机数。
    • nextDouble():返回0-1.0之间的随机数。
    • nextBoolean():随机返回true或者false。

      Console

  • 构造
    • 引入java.io包
    • Console cons=System.comsole()
  • 键盘键入
    • char[] passwd=cons.readPasswoerd()

      泛型

  • 泛型类声明:class 名称<泛型列表>
  • 声明对象:同样多加一个<泛型列表>

    链表

  • 构造:LinkedList<String>mylist=new LinkList<String>()
  • 常用函数
    • public boolean add(E element):链表末尾添加数据为element的新结点。
    • public boolean add(int index,E element):链表指定位置添加数据为element的新结点。
    • public boolean clear():删除链表所有结点,使成为空链表。
    • public boolean remove(E element):删除链表首次出现element的结点。
    • public E remove(int index):删除指定位置结点。
    • public int indexOf(E element):返回首次出现element的位置,没有返回-1。
    • public int lastIndexOf(E element):返回最后出现element的位置,没有返回-1。
    • public E set(int index,E element):替换index位置结点数据为element。
    • public int size():返回链表长度,即结点个数。
    • public boolean contains(Object element):判断是否有结点数据为element。
    • public boolean addFirst(E element):链表头添加数据为element的新结点。
    • public boolean addLast(E element):链表末尾添加数据为element的新结点。
    • public E getFirst:得到第一个结点中的数据。
    • public E getLast:得到最后结点中的数据。
    • public E removeFirst(int index):删除第一个结点,并返回这个结点数据。
    • public E removeLast(int index):删除最后一个结点,并返回这个结点数据。
    • public Object clone():得到一个克隆列表。
  • 迭代器:
    • 构造: Iterator <String> iter=list.iterator()
    • iter.hasNext()
    • iter.next()
  • 排序和查找:
    • public static sort(List<E>list):元素升序排列
    • int binarySearch(List<T>list,T key,CompareTo<T> c):折半查找是否含有参数key
  • 洗牌与翻转
    • public static void shuffle<List<E> list):洗牌
    • static void rotate(List<E> list,int distance):旋转
    • `public static void reserve<List<E> list):翻转

      堆栈

  • 创建:Stack<E>
  • 压栈:public E push(E item)
  • 弹栈:public E pop()
  • 判断是否空栈:public boolean empty()
  • 获取栈顶数据:public E peek()
  • 返回数据索引:public int search(Object data)

    散列映射

  • 构造:HashMap<String,Student> hashtable=HashSet<String,Student>()
  • 常用方法
    • public void clear():清空
    • public Object clone():返回当前散列映射的一个克隆
    • public boolean containsKey(Object key):判断是否有键/值对使用了参数指定的键
    • public boolean containsValue(Object value):判断是否有键/值对使用了参数指定的值
    • public V get(Object key):返回用key做键的键/值对中的值
    • public boolean isEmpty():判断是否不含任何键/值对
    • public V remove(Object key):删除key做键的键/值对,返回对应的值
    • public int size():返回散列映射大小,即键/值对数目

      树集

  • 构造:TreeSet<E>
  • 常用方法:
    • public boolean add(E o):添加结点,数据为参数o
    • public void clear():删除所有结点
    • public void contains(Object o):判断是否包含指定参数的对象
    • public E first():返回第一个结点中的数据
    • public E last():返回最后一个结点中的数据
    • public boolean isEmpty():判断是否空集
    • public boolean remove(Object key):删除存储指定参数的最小结点
    • public int size()`:返回结点数目
  • 树映射
    • TreeMap<K,V,>

      教材学习中的问题和解决过程

  • Mac中一个字符占三个字节,一开始以为占两个字节,导致运行结果是乱码
  • 解决过程:和结对学习的小组成员共同讨论,查找,发现问题后改正。
    技术分享图片
    技术分享图片

    代码托管

    技术分享图片
    技术分享图片
    技术分享图片

    上周考试错题总结

  • 测试完成后补充

20165310java_blog_week6

标签:getchar   offset   添加   引用   https   旋转   hashmap   学习   获取   

原文地址:https://www.cnblogs.com/atbaoi/p/8747572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!