JAVA学习笔记(三)

九、字符串

在java中,定义字符串变量用 String (注意首字母大写)。

使用format方法,以便于更方便的输出字符串:

 int year=2018;
        int month=03;
        int day=05;
        String other="参加报告会";
        
        String formatString="%d年,%d月,%d日,日程:%s";
        System.out.format(formatString, year,month,day,other);

* %c (unicode(统一码)字符)、%b (Boolean值)、%e (科学计数法的浮点数)、%x (16进制整数)

equals 比较方法(只能用于比较字符串相等):

String other="参加报告会";
        String theother="写博客";
       
        //boolean equals(int year int month);
       System.out.println(other.equals(theother));

用equalslgnoreCase方法比较字符串时,忽略字符串字母大小写。

    System.out.println(other.equalsIgnoreCase(theother));

1、字符串的查找

 System.out.println(other.charAt(day));//int day=2,若选择返回的位置(从0计算)超出字符串,编码框不会显示错误,但运行结果会显示错误
System.out.println(other.indexOf("加"));//会返回“加”这个字符在字符串中的位置(从0计算),若字符未出现则返回-1
System.out.println(other.indexOf("告",2));//从第二个位置开始检索“告”字,并且返回“告”字在整个字符串中的位置

  同理,lastIndexOf(String str) 查找字符最后出现的位置,返回int型位置;boolean startsWith(String prefix,int toffset) 查找字符从指定指引开始的子字符串是否以“prefix”前缀开始。

2、字符串的替换

System.out.println(other.replace(other,theother));//将other字符串替换为theother并输出
  System.out.println(other.replace("报告会",theother));//也可以用于替换子字符串

3、字符串的截取

  System.out.println(other.substring(day));//返回从(int) day位置开始的子字符串

  substring(int beginIndex,int endlndex) 可以返回从begin位置到end位置的子字符串;trim()用于删除原字符串的前后空格,并且返回新的字符串。

4、使用 StringBuffer 操作字符串

   StringBuffer con=new StringBuffer("W");//初始化
        con.append("国");//将字符接到StringBuffer后面
        con.append("立");
        con.append("武汉大学");
        con.reverse();//在StringBuffer中的字符串反转
        System.out.println(con.toString());
        System.out.println(con.length());//计算字符长度
 
//最后的输出结果为:学大汉武立国W
                   7     

* StringBuffer 与 String 的区别:每次改变String都会生成一个新的String,在频繁更改字符对象的情况下应该用StringBuffer

十、静态(static)方法

 可以用“类名.属性”的方式访问类名中的属性

public class text { 

	public static int sum=0; //则访问sum就用 text.sum

若将sum设置为私有变量,又想访问其数值结果,可以通过“类名.方法”调用查看

public class text { 

	private static int sum=0;
	
	public static int getsum(){
		return sum;
	} //访问sum结果用 text.getsum
	
  这里我们将getsum声明为静态(static)方法,则它只能调用其他static方法,只能访问static数据,且不能以任何方式引用this、super

十一、泛型

泛型所操作的数据类型被定义为一个参数,通过泛型可以在一个程序中同时对不同类型(int、char、double等)数据进行操作

public class defin<T>{ //T为泛型标示,可以写任意标识号
		private T key;
		
		public void setkey(T key){
			this.key=key; 
		}
		
		public T getkey(){
			return key;
		}
	}

//一个java文件中可以有多个class,但是第二个class不能用public修饰

//由于之前的text文件代码太多,所以博主想把新学的测试程序放到一个新的java class中运行,却发现无法点击run as,经查找发现原来在java中,代码必须有main函数才能跑起来......

//一个java程序中只能有一个main来提供输出,要想继续做一个main函数输出,必须再新建一个class

使用这种方法传入key的数据类型时,不能简单的用int,要使用数据类型的包装类Interger。方法为:

public class gen{
	  public static void main(String[] args){
		  defin<Integer>m= new defin<Integer>();
		  m.setkey(5);//传递int参数
		  
		  defin<Double>n=new defin<Double>();
		  n.setkey((double) 6);//传递double参数

element在java中代表一个任意的对象,在XML的概念中代表一个节点。例如:

public static <T> void printArray(T[] objects) { //这个泛型函数用于输出对象的类名称
        if (objects != null) {  //objects代表向泛型传递的数据对象,对象不为空时,将对象类名称输出
            for(T element : objects){  //foreach循环语句,for(元素类型t 元素变量x:遍历对象obj){元素变量x语句}
                System.out.printf("%s",element);  
            }  
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值