2017

来源:互联网 发布:大数据技术体系图 编辑:程序博客网 时间:2024/05/01 02:53
java 引用  https://www.zhihu.com/question/31203609
c++引用和java引用   http://blog.csdn.net/waitforfree/article/details/51030013
java的引用和c++的指针更像

1 Scanner(jdk 5之后)
(1)improt java.util.Scanner;
   Scanner sc = new Scanner(System.in);//标准的输入流,对应着键盘录入。
   System类下有一个静态的字段:
             public static final InputStream in;
   InputStream is = System.in;
  例如: 
  class Demo(){
         public static final int x =10;
         public static final Student s =new Student();
}
   int y = Demo.x;
   Student s = Demo.s;
--------------------
   因此用的构造方法:
      Scanner(InputStream source)
---------------------
(2)nextInt()  一个基本用法
   int x = sc.nextInt();
   System.out.println("x:"+x);
(3) 基本格式
  public boolean hasNextXxx();判断是否是某种类型的元素
  public Xxx  nextXxx(); 获取该元素
  举例:用int类型的方法举例
  public boolean hasNextInt();
  public int nextInt();
  
  注意:InputMismatchException:输入的和你想要的不匹配
 
  //获取数据 int类型
  Scanner sc = new Scanner(System.in);
  if(sc.hasNextInt()){
        int x =sc.nextInt();
        System.out.println("x:"+x);
     }else{
        Systme.out.println("你输入的数据有误!")
   }
      
(4) 常用的两个方法:
    public int nextInt():获取一个int类型的值
    public String nextLine():获取一个String类型的值
     Scanner sc = new Scanner(System.in);
      int a = sc.nextInt();
      int b = sc.nextInt();
      String s1 = sc.nextLine();
      String s2 = sc.nextLine();
     a:获取一个int类型的值,再获取一个int类型的值
     b:获取一个string类型的值,再获取一个string类型的值
     c: 获取一个string类型的值,再获取一个int类型的值
   出现问题:
     d:先获取一个int数值,在获取一个字符串时,出现了一个小问题。
      int a = sc.nextInt();
      String s1 = sc.nextLine();
      System.out.println("a:"+a+",s1"+s1);
      //打算输入10 a 结果只输入了10 程序就输出了 输出10
   如何解决?
     A:先获取一个数值后,再创建一个新的键盘录入对象获取字符串。
              (但会创建许多对象)
     B:把所有的数据都先按照字符串获取,然后要什么,就对应的转换成什么
                   String line =sc.nextLine();

2 String类  最常见的类。。。没有之一。。。

**可以直接赋值,java语法就这么规定的。 
因为String类太常用了,这样直接赋值,避免多次创建内容相同的String对象,节省空间,提高效率。 

A:字符串:就是由多个字符组成的一串数据,也可以看成是字符数组
  通过查看API,我们可以知道
        a:字符串字面值"abc"也可以看成是一个字符串对象。
        b:字符串是常量,一旦被赋值,就不能被改变。

B:构造方法 6种
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)     //把字符串常量值转成字符串

C:字符串的方法
      public int length():返回此字符串长度

D: 练习
    byte[] bys = {97,98,99,100,101};
    String s2 = new String(bys);
    System.out.println("s2:"+s2);
    System.out.println("s2.length():"+s2.length());
     //输出 abced 5
     //把字节数组转换成字符串 ,97对应的ASCII值就是a
   
    //把字节数组的一部分转成字符串 例如 得到字符串bcd
    String s3 = new String(bys,1,3);
    System.out.println("s3:"+s3);
    System.out.println("s3.length():"+s3.length());
    // 输出bcd 3
    
3 String 的特点 一旦被赋值就不能被改变

  但是,字符串直接赋值,会先去字符串常量池里面去找,如果有就直接返回,没有,就创建并返回。。。
  一旦被赋值就不能被改变。。。。值不能变,并不是说引用不能变
 
  String s = "hello"
  s+= "world";
  System.out.println("s:"+s)//helloworld

  //无法改变hello 以及world 的值



***4 面试题
   String s1 = new String("hello")
   String s2 = "hello";  //这两个的区别
**--------------------
==:比较引用类型比较的是地址值是否相同
equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。
---------------------
   System.out.println(s1 == s2);//false
   System.out.println(s1.equals(s2));//true
---------------------
  有区别,前者会创建2个对象,后者创建1个对象。

***4+ 面试题2
   A:
      String s1 = new String("hello");
      String s2 = new String("hello");
      System.out.println(s1 == s2);      //false
      System.out.println(s1.equals(s2));  //true
  
      String s3 = new String("hello");
      String s4 = "hello";
      System.out.println(s3 == s4);      //false
      System.out.println(s3.equals(s4));  //true

      String s5 = "hello";
      String s6 = "hello";
      System.out.println(s5 == s6);       //true
      System.out.println(s5.equals(s6));  //true
   B:
      String s1 = "hello";
      String s2 = "world";
      String s3 = "helloworld";
      System.out.println(s3 == s1 +s2);       //true
      System.out.println(s3.equals(s1+s2));    //true
      System.out.println(s3 == "hello" + "world");       //false  这个做错了,应该是true
      System.out.println(s5.equals("hello"+"world"));  //true

   **字符串如果是变量相加,先开空间,再拼接

   **字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。


***5 String的判断功能 6种
    boolean equals(Object obj):比较字符串内容是否相同
    boolean equalsIgnoreCase(String str):比较字符串内容是否相同,忽略大小写
    boolean contains(String str):判断大字符串中是否包含小字符串
    boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
    boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
    boolean isEmpty():判断字符串内容是否为空

    注意:字符串内容为空,和字符串对象为空。
          String s = "";
          String s = null;

***6 String 的获取功能 8种
   
   索引,开头第一个字符为0。  
   找不到的返回索引为 -1。
  
   int length():获取字符串的长度
   char charAt(int index):获取指定索引位置的字符
   int indexOf(int ch):返回指定字符在此字符串中第一次出现的索引----为什么这里是int类型而不是char类型?----原因是'a'和97其实都可以代表'a'
   int indexOf(String str):返回指定字符串在此字符串第一次出现的索引
   int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现的索引
   int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现的索引
   String substring(int start):从指定位置开始截取字符串,默认到末尾结束------包含start这个字符
   String substring(int start,int end):从指定位置开始到指定位置结束截取字符串-----包括start,但是不包括end索引

7  字符串练习
(1)字符串的遍历
  需求:遍历获取字符串中的每一个字符
  分析:
         A:如何能够拿到每一个字符呢?
           char charAt(int index)
         B: 怎么知道字符到底有多少个呢?
            int length()
    
    for(int x=0;x<s.length();x++){
          //char ch = s.charAt(x);
          //System.out.println(ch);
            System.out.prinln(s.charAt(x));
}
(2)统计大小写以及数字字符的个数
    案列:"Hello123World"
    结果: 大写字符:2个
           小写字符:6个
           数字字符:3个
    分析: 
           A:定义三个统计变量
               bigCount = 0
               smallCount = 0
               numberCount = 0
           B:遍历字符串,得到一个字符
               length()和charAt()结合
           C:判断该字符到底是属于那种类型的
                通过ASCII码表
                  0 48
                  A 65
                  a 97
         虽然,我们按照数字的这种比较是可以的,但是,还有更加简单的
                         char ch = s.charAt(x);
                         if(ch>='0'&&ch<='9') numberCount++
                         if(ch>='a'&&ch<='z') smallCount++
                         if(ch>='A'&&ch<='Z') bigCount++

***8 String 的转换功能 7种
    byte[] getBytes():把字符串转换成字节数组
    char[] toCharArray():把字符串转换成字符数组
    static String valueOf(char[] chs):把字符数组转换成字符串
    static String valueOf(int i):把int类型的数据转成字符串
     //注意:String类的valueOf方法可以把任意类型的数据转成字符串
    String toLowerCase():把字符串转成小写------本身没变
    String toUpperCase():把字符串转成大写------本身没变
    String concat(String str):把字符串拼接
----------------------------
    String s="JavaSE"
    byte[] bys = s.getBytes();   //打印出 74 97 118 97 83 69
    char[] chs = s.toCharArray();//打印出 J a v a S E
    String ss = String.valueOf(chs);// 打印出 JavaSE
    int i = 100;
    String sss = String.valueOf(i); //打印出100 不是整型了,而是字符串类型

    System.out.println(s.tolowerCase()); //javase
    System.out.println(s);               //JavaSE
    System.out.println(s.toUpperCase()); //JAVASE

9 字符串练习题2
A:把一个字符串的首字母转成大写,其余为小写(只考虑英文大小写字母字符)
  例如:helloWORLD
  结果:Helloworld
   //定义一个字符串
     String s = "helloWORLD";
   //先获取第一个字符
     String s1 = s.substring(0,1);
   //获取除了第一个字符以外的字符
     String s2 =s.substring(1);
   //把首字符转成大写
     String s3 = s1.toUpperCase();
   //把其余转成小写
     String s4 = s2.tolowerCase();
   // 两个拼接
     String s5 = s3.concat(s4);
   ---------------
   //优化后的代码
   String result = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
   System.out.println(result);

10 Strin类的其他功能
(1)替换功能:
    String replace(char old,char new)
    String replace(String old,String new)
(2)去除字符串两端空格
   String trim()

   String s1 =" hello world "
   String s2 = s1.trim();
   System.out.println("---"+s1+"---");//--- hello world ---
   System.out.println("---"+s2+"---");//---hello world---
(3)按字典顺序比较两个字符串
   int compareTo(String str)
   int compareToIgnoreCase(String str)

   String s3 = "hello";
   String s4 = "hello";
   String s5 = "abc";
   String s6 = "xyz";
   System.out.println(s3.compareTo(s4)); //0  h-h
   System.out.println(s3.compareTo(s5)); //7  h-a
   System.out.println(s3.compareTo(s6)); //-16 h-x
  
   字符串第一个位置上不同的ascii相减的值

11 String类的compareTo()方法的源码解析
   String s1 = "hello";
   String s2 = "hel";  
   System.out.println(s1.compareTo(s2)); // 2  为什么是2?
 
   回答:当都相同时,返回长度相减
  
   public int compareTo(String anotherString){
      //this -- s1 --"hello"
      //anotherString -- s2 --"hel"
     int len1 = value.length;//this.value.length--s1.toCharArray().length---5
     int len2 =anotherStirng.value.length;//s2.value.length --s2.toCharArray.length---3
     int lim = Math.min(len1,len2); //Math.min(5,3)----lim=3
     char v1[] = value;   //s1.toCharArray()
     char v2[] = anotherString.value; 
     //char v1[] = {'h','e','l','l','o'};
     //char v2[] = {'h','e','l'}
     int k = 0;
     while(k < lim){
      char c1 =v1[k];//c1 ='h'
      char c2 =v2[k];//c2 ='h'
      if(c1 != c2){
            return c1 -c2;
          }
        k++;
   }
   return len1- len2; // 5-3 =2;
}
      
12 字符串练习3
(1) 把数组中的数据按照指定格式拼接成一个字符串
   举例:
         int[] arr ={1,2,3} 
   输出结果:
            "{1, 2, 3}"
     
    //前提是数组已经存在
    int[] arr = {1,2,3};
    //定义一个字符串对象,只不过内容为空
     String s ="";
    //先把字符串拼接一个"{"
     s+="{";
    //遍历int数组,得到每一个元素
     for(int x=0;x<arr.length();x++){
         if(x == arr.length - 1){
              //就直接拼接元素和"}"
                s+=arr[x];
                s+="}";
            }else{
              //就拼接元素和逗号以及空格
                s+=arr[x];
                s+=", ";
          }
     }
      //输出拼接后的字符串
      System.out.println("最终的字符串是:"+s)  //输出{1, 2, 3}
      }
}
(2)字符串反转
   举例:
    输入:"abc"
    输出:"cba"

    Scanner sc =new Scanner(system.in);
    System.out.println("输入一个字符串:");
    String line =sc.nextLine();

    //定义一个新字符串
    String result="";
    //把字符串转成字符数组
    Char[] chs = line.toCharArray();
    //倒着遍历字符串,得到每一个字符
    for(int x=chs.length-1;x>=0;x--){
             result +=chs[x];
        }
    //输出新串
    System.out.println("反转后的结果:"+result);
(3)统计大串中小串出现的次数
  举例:
      字符串 "hehejavaheiheijavahahajavagunjavajavagun"
  输出:java出现了5次

  思路:
      A:定义一个统计变量,初始化值是0。
      B:先获取一次"java"在这个大串中第一次出现的索引
               如果索引值是-1,就说明不存在,返回统计变量
               如果索引值不是-1,就说明存在,统计变量++
      C:把刚才的索引+小串的长度作为起始位置,截取原始大串,得到一个新的字符串,并把该字符串的重新赋值给大串。
      D:回到B即可。
     publc static int getCount(String maxString,String minString){
          //定义一个统计变量,初始化值是0
          int count = 0;
          //先在大串中查找一次小串第一次出现的位置
          int index = maxString.indexOf(minString);
          //索引不是-1,说明存在,统计变量++
          while(index !=-1){
              count++;
          //把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串
          int startIndex = index + minString.length();
          maxString = maxString.substring(startIndex);
          //继续查
          index = maxString.indexOf(minString);      
         }
    return count;
}    
原创粉丝点击