java基础入门-总结(五)Java API

    xiaoxiao2025-02-28  62

    1.String类和StringBuffer类

    (1)字符串的基本操作

    package st.peter.string; public class Test { public static void main(String[] args) { String a = "abcdefgcba"; System.out.println("字符串的长度为:"+a.length()); System.out.println("字符串的第一个字母"+a.charAt(0)); System.out.println("字符c第一次出现的位置"+a.indexOf('c')); System.out.println("字符c最后一次出现的位置"+a.lastIndexOf('c')); } }

    (2)字符串的转换操作

    package st.peter.string; public class Test2 { public static void main(String[] args) { String str = "abcd"; System.out.print("将字符串转化为字符数组后的结果:"); char[] charArray = str.toCharArray(); for(int i=0;i<charArray.length;i++) { if(i!=charArray.length-1) { System.out.print(charArray[i]+","); }else { System.out.println(charArray[i]); } } System.out.println("将字符串转化为大写之后的结果为:"+str.toUpperCase()); } }

    (3)字符串的替换和去空格操作

    package st.peter.string; public class Test03 { public static void main(String[] args) { String s = "itcast"; //字符串替换操作 System.out.println("将it替换成cn.it的结果:"+s.replace("it", "cn.it")); //字符串去除空格操作 String s1 = " i t c a s t "; System.out.println("去除字符串两端空格的操作"+s1.trim()); System.out.println("去除字符串所有字符的操作"+s1.replace(" ", "")); } }

    (4)字符串的判断操作

    package st.peter.string; public class Test04 { public static void main(String[] args) { String s1 = "String"; String s2 = "Str"; System.out.println("判断是否以字符串Str开头:"+s1.startsWith("Str")); System.out.println("判断是否以字符串ng结尾:"+s2.endsWith("ng")); System.out.println("判断是否包含字符串tri:"+s1.contains("tri")); System.out.println("判断字符串是否为空:"+s1.isEmpty()); System.out.println("判断两个字符串是否相等:"+s1.equals(s2)); } }

    (5)字符串的截取和分割

    package st.peter.string; public class Test05 { public static void main(String[] args) { String str = "羽毛球-篮球-乒乓球"; System.out.println("从第五个字符截取到末尾的结果:"+str.substring(4)); System.out.println("从第五个字符截取到第六个字符的结果:"+str.substring(4, 6)); //字符串的分割操作 System.out.println("分割后的字符串数组中的元素依次为:"); String[] strArray = str.split("-"); for(int i=0;i<strArray.length;i++) { if(i != strArray.length-1) { System.out.print(strArray[i]+","); }else { System.out.println(strArray[i]); } } } }

    (6)StringBuffer类

    一个StringBuffer类(字符串缓冲区)。StringBuffer类和String类的最大区别在于他的内容和长度是可以改变的。

    package st.peter.string; public class Test06 { public static void main(String[] args) { System.out.println("1,添加-------------------------------------------------"); add(); System.out.println("2,修改-------------------------------------------------"); alter(); System.out.println("3,删除-------------------------------------------------"); remove(); } private static void remove() { StringBuffer sb = new StringBuffer("abcdefg"); sb.delete(1, 5); System.out.println("删除指定范围的字符串:"+sb); sb.deleteCharAt(2); System.out.println("删除指定位置的字符串:"+sb); sb.delete(0, sb.length()); System.out.println("清空缓冲区的结果"+sb); } private static void alter() { StringBuffer sb = new StringBuffer("abcdef"); sb.setCharAt(1, 'p'); // 修改指定位置的字符串 System.out.println("修改指定位置的字符串:"+sb); sb.replace(1, 3, "qq"); System.out.println("替换指定位置的字符串:"+sb); System.out.println("字符串的翻转操作:"+sb.reverse()); } private static void add() { //定义字符串的缓冲区 StringBuffer sb = new StringBuffer(); sb.append("abcdefg"); System.out.println("append添加的结果:"+sb); sb.insert(2, "123"); System.out.println("insert的添加结果"+sb); } } String类表示的是字符串的常量,一旦创建后,内容和长度都是无法改变的。StringBuffer表示字符容器,其内容和长度都是可以随时改变的。String类覆盖了Object类的equals()方法,而StringBuffer类没有覆盖Object类的equals()方法。String类对象可以用操作符+进行连接,而StringBuffer类对象之间不能。

    2.System类与Runtime类

    getProperties()方法: https://blog.csdn.net/qq_27093465/article/details/53241848

    currentTimeMillis(): https://blog.csdn.net/justinqin/article/details/78758250

    arraycopy(): https://blog.csdn.net/balsamspear/article/details/85069207

    Runtime: https://blog.csdn.net/qq_23315711/article/details/74065731

     

    3.Math类与Random类

    (1)Math类是数学操作类,提供了一系列用于数学运算的静态方法。由于Math类比较简单,因此初学者可以通过查看API文档来学习Math类的具体用法。https://blog.csdn.net/HUXU981598436/article/details/16359807

    (2)java.util包中有一个Random类,可以在指定的取值范围内随机产生数值

    package st.peter.string; import java.util.Random; public class Test07 { public static void main(String[] args) { Random r = new Random();//不传入种子 for(int x=0;x<10;x++) { System.out.println(r.nextInt(100)); } } } package st.peter.string; import java.util.Random; public class Test07 { public static void main(String[] args) { Random r = new Random(20);//创建对象时候传入种子 for(int x=0;x<10;x++) { System.out.println(r.nextInt(100)); } } }

     

    4.包装类

    装箱:将基本数据类型的值转化为引用数据类型

    package st.peter.string; public class Test08 { public static void main(String[] args) { int a = 10; Integer in = new Integer(a); System.out.println(in.toString()); } }

    拆箱:引用数据类型的值转化为基本数据类型

    intValue()方法可以将Integer类型的值转化为int类型

    package st.peter.string; public class Test09 { public static void main(String[] args) { Integer in = new Integer(20); int i = in.intValue(); System.out.println(i); } }

     

    5.Calendar类

    Datel类自JDK 1.1之后被Calendar取代。

    package st.peter.string; import java.util.Calendar; public class Test10 { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH)+1; int day = cal.get(Calendar.DATE); System.out.println("年:"+year+"月:"+month+"日:"+day); } }

     

     

     

     

     

     

     

     

    最新回复(0)