zoukankan      html  css  js  c++  java
  • 数组, 数字, 字符串的处理

    public class Text2
    {
        public static void main(String[] args){
            // 常量池
            
            String s1 = "abc";
            String s2 = "abc";
            System.out.println(s1==s2);   // true //s1和s2指向的是同一个对象,都指向同一个内存地址
    
            String s3 = new String("abc");  //在内存空间重新开辟一块区域
            String s4 = new String("abc");  //在内存空间重新开辟一块区域
            System.out.println(s3==s4);   //false //s3和s4指向不一样,指向的内存地址不一样
    
            System.out.println(s3.equals(s4));  // true //比较的是两个字符串s3和s4的内容,都为abc
            
        }
    }

    包装类:

      Integer.parseInt();

      byte---Byte
      short---Short
      int---Integer
      long---Long

      float---Float
      double---Double

      boolean---Boolean

      char---Character

    public class Text2
    {
        public static void main(String[] args){
    
            //包装类,都有相对应的方法
            System.out.println(Integer.MIN_VALUE); 
            System.out.println(Integer.MAX_VALUE);
            System.out.println(Byte.MIN_VALUE);    //-128
            System.out.println(Byte.MAX_VALUE);    //127
            System.out.println(Long.MIN_VALUE);   
            System.out.println(Long.MAX_VALUE);
            System.out.println(Short.MIN_VALUE);   //-32768
            System.out.println(Short.MAX_VALUE);   //32767
    
            System.out.println(Float.MIN_VALUE);
            System.out.println(Float.MAX_VALUE);
            System.out.println(Double.MIN_VALUE);
            System.out.println(Double.MAX_VALUE);
    
            System.out.println(Integer.parseInt("56"));     //56,在这里需要注意对应的方法名
            System.out.println(Float.parseFloat("56"));     //56.0
            System.out.println(Float.parseFloat("12.34"));  //12.34
            
            
        }
    }

    字符串的处理:

    字符串之间的equals

    String str = "..............";
    str.length();
    str.trim();
    str.charAt(int i);
    str.contains(CharSequence s);
    str.startsWith(String s);
    str.endsWith(String s);
    replace(char o, char n);
    replace(CharSequence o, CharSequence n);
    split(String s);
    toUpperCase();
    toLowerCase();
    valueOf(any args);
    str.indexOf(String s);
    str.lastIndexOf(String s);
    str.substring(int i);
    str.substring(int a, int b);

    public class Text2
    {
        public static void main(String[] args){
                //0123456789012345678901234 String str
    = " a new world a new start "; System.out.println(str.length()); //输出值为25,包括两边的空格,即返回整个字符串的长度 System.out.println(str.trim()); //输出值为 “a new world a new start”,即是去掉字符串两边的空格 System.out.println(str.trim().length()); //输出值为23,即是去掉字符串两边的空格后返回整个字符串的长度 System.out.println(str.charAt(3)); //输出值为 n ,即是取出字符串中指定索引位置的字符 System.out.println(str.contains("new")); //输出值为 true,即是判断一个字符串中是否包含所指定的另一个字符串, //如果包含,就为true,不包含,即为false System.out.println(str.contains("abc")); //输出值为 false System.out.println(str.startsWith("qq")); //输出值为 false,即是判断某个字符串是否以另一个字符串开头, //这段字符串以空格开始,而不是以qq开始,所以结果为false System.out.println(str.endsWith("qq")); //输出值为 false,即是判断某个字符串是否以另一个字符串结尾, //这段字符串以空格结尾,而不是以qq结尾,所以结果为false System.out.println(str.replace('a', 'b')); //输出值为 " b new world b new stbrt ", //即是把字符串里面的a 全部替换为 b System.out.println(str.replace("new", "old")); //输出值为 “ a old world a old start ” //即是 替换字符串中指定的字符或字符串 String[] strs = str.split(" "); //注意和 String[] strs = str.split(" ", 3); 的区别,只分割成三部分 System.out.println(strs.length); //输出值为 7;最后一个空格省略不计 for(String s : strs){ System.out.println(s); } System.out.println(str.toUpperCase());// 将字符串转换成大写,即" A NEW WORLD A NEW START " System.out.println(str.toLowerCase());// 将字符串转换成小写,即" a new world a new start" System.out.println(String.valueOf(6));// toString(); System.out.println(str.indexOf("new"));// 子字符串在字符串中第一次出现的位置,输出值为 3 System.out.println(str.lastIndexOf("new"));// 子字符串在字符串中最后一次出现的位置,输出值为 15 System.out.println(str.substring(5)); //截取字符串包含索引为5的字符,输出值为 w world a new start
     System.out.println(str.substring(5, 8)); //从第一个数字开始截取, 直到第二个数字, 但是不包含第二个数字的位置的字符,,即从第5个到第7个,输出值为 w w  } }

    练习题:

    public class Lianxi1 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
                      
            String str = "像勇士这样的球队,只有防守一松懈,他们才能抓住机会,打完了三场,爵士还是没找到应对勇士的办法";
    
            //1, 写代码找出关键字"球队","机会"所在字符串str的索引位置, 找出字符串中最后一个"勇士"的位置, 并输出在控制台上
            
            int a = str.indexOf("球队");
            int b = str.indexOf("机会");
            int c = str.lastIndexOf("勇士");
            
            System.out.println(a);  //6
            System.out.println(b);  //23
            System.out.println(c);  //41
            //2, 定义int型变量m, 取值为第一题中所有索引值的和
            
            int m = a + b + c;
            System.out.println(m);  //70
                    
            //3, 在控制台上输出m作为char型时显示的内容
            
            System.out.println((char)m);  //F
            
            //4, 写代码实现将str字符串用","分割成数组, 并输出索引值为4的值
            
            System.out.println(str.split(",")[4]);  //爵士还是没找到应对勇士的办法
            
            //5, 写代码实现将str字符串中"爵士"用"勇士"代替, "勇士"用"爵士"代替, 并将结果输出到控制台上(禁用replace方法)
            
            /*
            String[] newstr = str.split("");
            for( int i = 0; i < newstr.length;i++){
                if("爵".equals(newstr[i])){
                    newstr[i] = "勇";
                }else if("勇".equals(newstr[i])){
                    newstr[i] = "爵";
                }
                System.out.print(newstr[i]);  //第一种输出方式
            }
            */
            String[] newstr = str.split("");
            String temp = "";   //临时变量
            for( int i = 0; i < newstr.length;i++){
                if("爵".equals(newstr[i])){
                    newstr[i] = "勇";
                }else if("勇".equals(newstr[i])){
                    newstr[i] = "爵";
                }
                temp += newstr[i];
            }
            System.out.println(temp);  //第二种输出方式
            
            //6, 编写代码从str字符串中取一部分在控制台上打印这样一句话: 勇士抓住机会,找到应对办法
            
            int s1 = str.indexOf("勇士");
            System.out.print(str.substring(s1,s1+2));
            int s2 = str.indexOf("抓住机会");
            System.out.print(str.substring(s2,s2+4));
            int s5 = str.indexOf(",");
            System.out.print(str.substring(s5,s5+1));
            int s3 = str.indexOf("找到应对");
            System.out.print(str.substring(s3,s3+4));
            int s4 = str.indexOf("办法");
            System.out.print(str.substring(s4,s4+2));
            
    
        }
    
    }
    //写一段代码, 可以取出任意qq邮箱地址中的qq号码
    public class Lianxi2 {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            String qqemail = "12345678@qq.com";           
            System.out.println(qqemail.substring(0,qqemail.indexOf("@")));
            //输出值为:  12345678

          String[] qqs = qqemail.split("@");
          System.out.println(qqs[0]);

        }
    }
    //使用for和if打印一个空心正方形
    public class Lianxi3 {
    
        public static void main(String[] args) {
            int n = 5;
    
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < n; j++) {
                    if(i==0||i==4) {
                        System.out.print("* ");//print 代表不用换行
                    } else {
                        if(j==0||j==4) {
                            System.out.print("* ");   
                        } else {
                            System.out.print("  ");
                        }
                    }
                }
                System.out.println();
            }
            
            /*for( int i=0; i<5; i++){
            if(i>0&&i<4){   //先判断 i的值是否在1-5这个区间,
                for(int j=0;j<5;j++){
                    if(j==0||j==4){
                        System.out.print("*"+" ");}
                    else{
                        System.out.print(" "+" ");}
                }
            }else{
                for( int l=0; l<5;l++){
                    System.out.print("*"+" ");
                }
            }
            System.out.println();
            }*/
        }
    }
     //使用for循环打印一个菱形
    
    public class Lianxi4 {
    
        public static void main(String[] args) {
            int n = 4;
            for(int i=0;i<n;i++) {
                for(int j=0;j<n-1-i;j++) {
                    System.out.print(" ");
                }
                for(int k=0;k<(2*i+1);k++) {
                    System.out.print("*");
                }
                System.out.println();
            }
            for(int q=0;q<n-1;q++) {
                for(int w=0;w<q+1;w++) {
                    System.out.print(" ");
                }
                for(int e=0;e<2*n-3-2*q;e++) {
                    System.out.print("*");
                }
                System.out.println();
            }
            System.out.println();
    
        }
    
    }
    //使用for循环打印一个空心菱形
    
    public class Lianxi5 {
    
        public static void main(String[] args) {
            int n = 4;
            for(int i=0;i<n;i++) {
                for(int j=0;j<n-1-i;j++) {
                    System.out.print(" ");
                }
                for(int k=0;k<(2*i+1);k++) {
                    if(k==0||k==2*i) {
                        System.out.print("*");
                    } else {
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
            for(int q=0;q<n-1;q++) {
                for(int w=0;w<q+1;w++) {
                    System.out.print(" ");
                }
                for(int e=0;e<2*n-3-2*q;e++) {
                    if(e==0||e==2*n-4-2*q) {
                        System.out.print("*");
                    } else {
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        }
    }
  • 相关阅读:
    Win7操作系统防火墙无法关闭的问题 无法找到防火墙关闭的地方的解决的方法
    【微信】微信获取TOKEN,以及储存TOKEN方法,Spring quartz让Token永只是期
    OC内存管理总结,清晰明了!
    下次自己主动登录(记住password)功能
    linux删除svn版本号库
    Python中可避免读写乱码的一个强慷慨法
    Tomcat源代码阅读#1:classloader初始化
    iOS关于图片点到像素转换之杂谈
    hdu 3804树链剖分+离线操作
    cdn缓存
  • 原文地址:https://www.cnblogs.com/sutao/p/7182468.html
Copyright © 2011-2022 走看看