이 섹션에서는 내가 마음 매핑이 쉽게 액세스 할 수 있도록 요약 사용, 더
마인드 매핑
위의 코드를 다시 내가 사용을 설명하기 위해, 쉽게 이해하기
코드 구현
A, 객체 클래스 일반적인 방법은 구현하기
public class MyTest {
public static void main(String[] args) {
Object obj = new Object();
Object obj1 = new Object();
boolean b = obj.equals(obj1);//默认情况下比较的是对象的引用是否相同。
System.out.println(b);
int i = obj.hashCode();//返回该对象的哈希码值。
System.out.println(i);
Class a = obj.getClass();//返回此 Object 的运行时类。
System.out.println(a);
String s = obj.toString();//返回该对象的字符串表示。
System.out.println(s);
}
}
이 두 클래스 스캐너
스캐너 키보드 입력을 얻기 위해 사용되며, 이는 상대적으로 간단하다 스캐너 클래스 코드 hasNextXxx () 및 nextXxx () 메소드를 구현.
public class MyTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//nextXXX() 获取各种类型的数据
// hasNextxxx() 判断是否录入的是该种数据
System.out.println("请输入一个整数");
boolean b = sc.hasNextInt();
if (b) {
int i = sc.nextInt();
System.out.println(i);
} else {
System.out.println("你输入的不是整数类型");
}
sc.close();//释放资源
}
}
코드를 사용하여 세 문자열 클래스 구현 방법
(1)에있어서 판정 기능
public class MyTest {
public static void main(String[] args) {
boolean b = "abc".equals("ABC");//这种比较是区分大小写的
boolean b1 = "abc".equalsIgnoreCase("ABC"); //忽略大小写的比较
System.out.println(b);
System.out.println(b1);
boolean b2 = "abcdfdfdfdfdfd".contains("abc123");//判断一字符串,有没有包含在另一个字符串里面
System.out.println(b2);
boolean g = "abcdefg".endsWith("");//判断字符串是否以传递进来的字符串结尾
boolean ab = "abvdfefe".startsWith("a");//判断字符串是否以传递进来的字符串开头
System.out.println(g);
System.out.println(ab);
boolean empty = "".isEmpty();//判断是否为空串
System.out.println(empty);
if (" ".length() == 0) {
System.out.println("是空串");
}
}
}输出:
false
true
false
true
true
true
2 취득 기능
public class MyDemo {
public static void main(String[] args) {
int length = "abcd".length();//获取字符串长度
System.out.println(length);
char c = "abc".charAt(2); //根据索引获取单个字符
System.out.println(c);
int e = "aesfasdfasdfdsafe".indexOf('e');//查找该字符在字符串中第一次出现的索引
//如果找不到就会返回 -1 有时我们会拿 -1 作为判断条件
int asd = "asdfadsfasdfsadfasdfdsa".indexOf("asd3333");
System.out.println(e);
System.out.println(asd);
//lastIndexOf 从后往前检索,该字符第一次出现的索引
int a = "asfasdfasfdasdfas".lastIndexOf("a");
System.out.println(a);
int f = "asfasfdsafdsafdsaf".indexOf('f', 6);//从指定索引处查找该字符
System.out.println(f);
String substring = "明天是圣诞节".substring(0);
substring = "明天是圣诞节".substring(3);
substring = "明天是圣诞节".substring(3, 5);//根据首尾索引来截取字符串,含头,不含尾
System.out.println(substring);
}
}
3, 변환
①、 public class MyDemo {
public static void main(String[] args) {
byte[] byes = {97, 101, 98, 99, 100};
String s = new String(byes); //把一个字节数组转换成字符串
s = new String(byes, 0, 3); //把一个字节数组的一部分转成字符串 参2 起始索引 参数3 你转几个
System.out.println(s);
char[] chars = {'a', 'A', '你', '好'};
String s1 = new String(chars);//把一个字符数组转成一个字符串
s1 = new String(chars, 2, 2); //把一个字符数组的一部分,转成字符串 参数2 起始索引,参数3 你转几个
System.out.println(s1);
byte[] bytes = "abcde".getBytes();//把字符串转换为字节数组。
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
System.out.println("-----------------------");
byte[] bytes1 = "圣诞节".getBytes();
String st = new String(bytes1);
System.out.println(st);
System.out.println("---------------------------");
//把一个字符串,转换成字符数组
char[] cha = "圣诞快乐".toCharArray();//把字符串转换为字符数组。
for (int i = 0; i < cha.length; i++) {
System.out.println(cha[i]);
}
String st2 = new String(cha);
System.out.println(st2);
}
}
②、public class MyDemo2 {
public static void main(String[] args) {
//valueOf() 可以将很多类型转成字符串
String s = String.valueOf(100);
System.out.println(s);
String s1 = String.valueOf(new char[]{'a', 'b', 'c'});
System.out.println(s1);
System.out.println("------------------");
String s2 = "abc".toUpperCase();//把一个字符串转成大写
System.out.println(s2);
String s3 = "ABV".toLowerCase();//把一个字符串转成小写
System.out.println(s3);
String str2 = "abc" + "ccc" + "ddd" + "ffff";
String concat = "abc".concat("ccc").concat("ddd").concat("fffff");//连接字符串
System.out.println(concat);
}
}