day9常用类

Scanner 类

用于获取键盘录入的数据
public String nextline 

通过scanner获取字符串数据
public class sacnner {
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		String line=sc.nextLine();
		
		System.out.println("line:"+line);
	}
}

如何查看源码

ctrl+xxx

无参构造

alt + /

带参构造

alt +shift +s +o

object类

根类,所有类的继承该类

直接输出对象名,输出底层调用的是该对象的toString()方法,
建议所有子类重写该方法

tostring方法

toString() 方法用于返回该对象的字符串表现。

equals

查看两个对象是否相等

object类中的object比较的是对象的地址是否相等
如果想比较对象的内容是否相等,必须自己重写方法

		stu s1=new stu("hehe",30);
		stu s2=new stu("hehe",30);
		System.out.println(s1.equals(s2));

equals解析

@Override
	public boolean equals(Object obj) {
			
		if (this == obj)//此时this代表s1  object代表s2
			return true;//此时s1和s2的地址值不相同,继续往下执行
		
		if (obj == null)
			return false;//此时查看s1是不是为空,不为空,继续执行
		
		
		if (getClass() != obj.getClass())
			return false;//此时查看的是两个对象是否来自同一个类,此时应该为true,继续执行
		
		
		stu other = (stu) obj;//向下转型,other代表了s2
		/
		if (age != other.age)//此时判断是的this.age(就是s1.age)判断两个对象的age是否相同,,因为age都是30继续往下执行
			return false;
		
		if (name == null) {//此时查看s1的name是否为空,不为空不执行
			if (other.name != null)
				return false;
			
		} else if (!name.equals(other.name))//此时equals()比较的是字符串内容否相同
			return false;
		return true;//最后返回的值

string类

string代表字符串类
字符串的本质就是字符串组
public String(String original)  将字符串封装成字符串对象
public String(char[] value)将字符串组的数据封装成字符对象
public String(char[] value,int offset, int count)将字符串的一部分数据封装成字符串对象

以下几个都不常用:

	String s1=new String ("hello");
	System.out.println(s1);
	
	char[] value={'h','e','l','l','o'};
	String s2=new String(value);
	System.out.println(s2);
	
	
	String s3=new String(value,0,3);
	System.out.println(s3);
结果
hello
hello
hel

常用

String s4="hello";
System.out.println(s4);

String创建对象的特点

a,通过构造方法创建对象
b,通过直接赋值的方式创建对象

通过构造方法创建的字符串对象存在堆内存
通过直接赋值的方式创建的字符串对象是方法区的常量池,为了方便字符串的复用

public class test {
	public static void main(String[] args) {
	String s1=new String ("hello");
	System.out.println(s1);
	
	char[] value={'h','e','l','l','o'};
	String s2=new String(value);
	System.out.println(s2);
	
	
	String s3=new String(value,0,3);
	System.out.println(s3);
	
	String s4="hello";
	System.out.println(s4);
	}
}

在这里插入图片描述

格式化输出
举例:int[] arr = {1,2,3};
输出结果:[1, 2, 3]public class denglu {
	public static void main(String[] args) {
		int[] arr={1,2,3};
		
		String result=arrtoString(arr);
		System.out.println(result);
		
		
	}
	 public static String arrtoString(int[] arr){
		String s="";
		s+="[";
		for(int x=0;x<arr.length;x++){
			if(x==arr.length-1){
				s+=arr[x];
			}else{
				s+=arr[x]+", ";
			}		
		}		
		s+="]";
		return s;
	}
}
字符串反转
public class denglu {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String s=sc.nextLine();		
		String result=reverse(s);
		System.out.println(result);
		
	}	
	public static String reverse(String s) {
		String ss="";
		for(int x=s.length()-1;x>=0;x--){
			ss+=s.charAt(x);
		}		
		return ss;
	}
}

Stringbuilder(返回的是对象)

 string本身是不可改变的,它只能赋值一次,每一次内容发生改变,都会生成一个新
 的对象,然后原有的对象引用新的对象,而每一次生成新对象都会对系统性能产生影响
stringbuilder 每次操作都是对自身对象进行操作,而不是生成新的对象,其所占空间
会随着内容的增加而扩充,这样,在做大量的修改操作时,不会因生成大量匿名对象而影响系统性能

Stringbuilder 常用类

append

StringBuilder sb=new StringBuilder();
		sb.append('a');
		sb.append('b');
		sb.append('a').append('b').append('c');
		System.out.println(sb);

reverse

	sb.reverse();

stringbuilder—>string的转换(通过to_string方法)

public class printString {
	public static void main(String[] args) {
		StringBuilder sb=new StringBuilder();
		sb.append('a').append('b').append('c');
		
		String s=sb.toString();
		System.out.println(s);
	}	
}

string—>stringbuilder的转换(通过构造方法)

public class printString {
	public static void main(String[] args) {
		String s="hello";
		StringBuilder sb=new StringBuilder(s);
		System.out.println(sb);
	}
	
}

将数组拼接成一个字符串

public class printString {
	public static void main(String[] args) {
	int[] arr={1,2,3};
	
	System.out.println(pingjie(arr));
	
	
	}
	public static String pingjie(int[] arr){
		StringBuilder sb =new StringBuilder();
		sb.append("[");
		for(int x=0;x<arr.length;x++){
			if(x==arr.length-1){
				sb.append(arr[x]);
			}else
			sb.append(arr[x]).append(", ");
		}		
		sb.append("]");		
		String s=sb.toString();
		return s;			
	}	
}
使用Stringbuilder对字符串进行反转
public class printString {
	public static void main(String[] args) {
	String name="abcd1234";
	System.out.println(fanzhuan(name));	
	}
	public static String fanzhuan(String name){
		return new StringBuilder().append(name).reverse().toString();
		
		
	}
}
运行结果:4321dcba

indexOf() 方法有以下四种形式:

public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

contains
public boolean contains(CharSequence s)当且仅当此字符串包含指定的 char 值序列时,返回 true。 

参数:
s - 要搜索的序列 
返回:
如果此字符串包含 s,则返回 true,否则返回 false 
抛出: 
NullPointerException - 如果 s 为 null
replaceFirst() 
方法使用给定的参数 replacement 替换字符串第一个匹配给定的正则表达式的子字符串。
public String replaceFirst(String regex,
                           String replacement)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值