A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wenyu 中级黑马   /  2015-2-14 09:58  /  909 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

-:String类的概述和使用
(1)多个字符组成的一串数据。它可以和字符数组进行相互转换。
(2)构造方法:
  A:public String()
  B:public String(byte[] bytes)
  C:public String(byte[] bytes,int offset,int length)
  D:public String(char[] value)
  E:public String(char[] value,int offset,int count)
  F:public String(String original)
  下面的这一个虽然不是构造方法,但是结果也是一个字符串对象(也常用这种方法)
  G:String s = "hello";
(3)字符串的特点
  A:字符串一旦被赋值,就不能改变。
   注意:这里指的是字符串的内容不能改变,而不是引用不能改变。
  B:字面值作为字符串对象和通过构造方法创建对象的不同
   String s = new String("hello");和String s = "hello"的区别前者
(4)字符串的面试题
  A:==和equals()“==”比较的是地址值,“equals()”比较的是内容
   
  B:字符串的拼接
   String s1 = "hello";
   String s2 = "world";
   String s3 = "helloworld";
   System.out.println(s3 == s1 + s2);
   System.out.println(s3 == "hello" + "world");
(5)字符串的功能
  A:判断功能
   boolean equals(Object obj)
   boolean equalsIgnoreCase(String str)
   boolean contains(String str)
   boolean startsWith(String str)
   boolean endsWith(String str)
   boolean isEmpty()
  B:获取功能
   int 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)
  C:转换功能
   byte[] getBytes()
   char[] toCharArray()
   static String valueOf(char[] chs)
   static String valueOf(int i)
   String toLowerCase()
   String toUpperCase()
   String concat(String str)
  D:其他功能
   a:替换功能
    String replace(char old,char new)
    String replace(String old,String new)
   b:去空格功能
    String trim()
   c:按字典比较功能
    int compareTo(String str)
    int compareToIgnoreCase(String str)

-:StringBuffer
(1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了
    一个字符串缓冲区类。StringBuffer供我们使用。
(2)StringBuffer的构造方法
  A:StringBuffer()
  B:StringBuffer(int size)
  C:StringBuffer(String str)
(3)StringBuffer的常见功能
  A:添加功能
   public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
    public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身

  B:删除功能
   public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
   public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身
  C:替换功能
   public StringBuffer replace(int start,int end,String str):从start开始到end用str替换
  D:反转功能
   public StringBuffer reverse()
  E:截取功能(注意这个返回值不再是其本身)
   public String substring(int start)
   public String substring(int start,int end)


-:数组高级以及Arrays
(1)排序
  A:冒泡排序
   相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处。同理,其他的元素就可以排好。
   
   public static void bubbleSort(int[] arr) {
    for(int x=0; x<arr.length-1; x++) {
     for(int y=0; y<arr.length-1-x; y++) {
      if(arr[y] > arr[y+1]) {
       int temp = arr[y];
       arr[y] = arr[y+1];
       arr[y+1] = temp;
      }
     }
    }
   }
   
  B:选择排序
   把0索引的元素,和索引1以后的元素都进行比较,第一次完毕,最小值出现在了0索引。同理,其他的元素就可以排好。
   
   public static void selectSort(int[] arr) {
    for(int x=0; x<arr.length-1; x++) {
     for(int y=x+1; y<arr.length; y++) {
      if(arr[y] < arr[x]) {
       int temp = arr[x];
       arr[x] = arr[y];
       arr[y] = temp;
      }
     }
    }
   }
(2)查找
  A:基本查找
   
   public static int getIndex(int[] arr,int value) {
    int index = -1;
   
    for(int x=0; x<arr.length; x++) {
     if(arr[x] == value) {
      index = x;
      break;
     }
    }
   
    return index;
   }
  B:二分查找(折半查找)
   
   public static int binarySearch(int[] arr,int value) {
    int min = 0;
    int max = arr.length-1;
    int mid = (min+max)/2;
   
    while(arr[mid] != value) {
     if(arr[mid] > value) {
      max = mid - 1;
     }else if(arr[mid] < value) {
      min = mid + 1;
     }
     
     if(min > max) {
      return -1;
     }
     
     mid = (min+max)/2;
    }
   
    return mid;
   }
-:Integer
(1)为了让基本类型的数据进行更多的操作,Java就为每种基本类型提供了对应的包装类类型
  byte   Byte
  short  Short
  int   Integer
  long  Long
  float  Float
  double  Double
  char  Character
  boolean  Boolean
(2)Integer的构造方法
  A:Integer i = new Integer(100);
  B:Integer i = new Integer("100");
   注意:这里的字符串必须是由数字字符组成
(3)String和int的相互转换
  A:String -- int
   Integer.parseInt("100");
  B:int -- String
   String.valueOf(100);

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马