`JAVA视频学习笔记-马士兵`
JAVA视频学习笔记(六)
常用类_字符串相关类(20200614~16)
- 字符相关类_String1:
这些类看JAVA API文档,不要看ppt等 。String类在java.lang包里面。String类举例:
public class test {
public static void main(String[] args) {
String s1="hello";
String s2="world";
String s3="hello";
System.out.println(s1==s3);
s1=new String("hello");
s2=new String("hello");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
char c[]={'s','u','n',' ','j','a','v','a'};
String s4=new String(c);
String s5=new String(c,4,4);
System.out.println(s4);//sun java
System.out.println(s5);//java }}
}
}
输出:
true
false
true
sun java
java
- 字符相关类_String2:
这些类看JAVA API文档,不要看ppt等 。String类在java.lang包里面,其常用方法:
public class test
{
public static void main(String[] args)
{
String s1 = "sun java", s2 = "Sun Java";
System.out.println(s1.charAt(1));
System.out.println(s1.length());
System.out.println(s1.indexOf("java"));
System.out.println(s1.indexOf("Java"));
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
String s = "我是程序员,我在学java";
String sr = s.replace('我', '你');
System.out.println(sr);
}
}
 {
Date[] days = new Date[5];
days[0] = new Date(2006, 5, 4);
days[1] = new Date(2006, 7, 4);
days[2] = new Date(2008, 5, 4);
days[3] = new Date(2004, 5, 9);
days[4] = new Date(2004, 5, 4);
Date d = new Date(2006, 7, 4);
String str = String.valueOf(d);
//str = d.toString();
bubbleSort(days);
for(int i=0; i<days.length; i++) {
System.out.println(days[i]);
}
System.out.println(binarySearch(days, d));
}
public static Date[] bubbleSort(Date[] a){
int len = a.length;
for(int i = len-1;i>=1;i--){
for(int j = 0;j<=i-1;j++){
if(a[j].compare(a[j+1]) > 0){
Date temp = a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return a;
}
public static int binarySearch(Date[] days, Date d) {
if (days.length==0) return -1;
int startPos = 0;
int endPos = days.length-1;
int m = (startPos + endPos) / 2;
while(startPos <= endPos){
if(d.compare(days[m]) == 0) return m;
if(d.compare(days[m]) > 0) {
startPos = m + 1;
}
if(d.compare(days[m]) < 0) {
endPos = m -1;
}
m = (startPos + endPos) / 2;
}
return -1;
}
}
class Date {
int year, month, day;
Date(int y, int m, int d) {
year = y; month = m; day = d;
}
public int compare(Date date) {
return year > date.year ? 1
: year < date.year ? -1
: month > date.month ? 1
: month < date.month ? -1
: day > date.day ? 1
: day < date.day ? -1 : 0;
}
public String toString() {
return "Year:Month:Day -- " + year + "-" + month + "-" + day;
}
}
3. 练习:
编写一个程序,输出一个字符串中的大写英文字母数,小写英文字母数以及非英文字母数;
编写一个方法,输出在一个字符串中,指定字符串出现的次数。
import java.util.regex.*;
public class TestString {
public static void main(String[] args) {
//String s = "AaaaABBBBcc&^%adfsfdCCOOkk99876 _haHA";
//int lCount = 0, uCount = 0, oCount = 0;
/*
for(int i=0; i<s.length(); i++) {
char c = s.charAt(i);
if(c >= 'a' && c <= 'z') {
lCount ++;
} else if (c >='A' && c <= 'Z') {
uCount ++;
} else {
oCount ++;
}
}
*/
/*
String sL = "abcdefghijklmnopqrstuvwxyz";
String sU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i=0; i<s.length(); i++) {
char c = s.charAt(i);
if(sL.indexOf(c) != -1) {
lCount ++;
} else if (sU.indexOf(c) != -1) {
uCount ++;
} else {
oCount ++;
}
}
*/
/*
for(int i=0; i<s.length(); i++) {
char c = s.charAt(i);
if(Character.isLowerCase(c)) {
lCount ++;
} else if (Character.isUpperCase(c)) {
uCount ++;
} else {
oCount ++;
}
}
System.out.println(lCount + " " + uCount + " " + oCount);
*/
String s = "sunjavahpjavaokjavajjavahahajavajavagoodjava";
String sToFind = "java";
int count = 0;
int index = -1;
while((index = s.indexOf(sToFind)) != -1) {
s = s.substring(index + sToFind.length());
count ++;
}
System.out.println(count);
}
}
常用类_字符串相关类(20200617)
- StringBuffer类:当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。
和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。
StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。
由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类。
public class Test{
public static void main(String args[]){
StringBuffer sBuffer = new StringBuffer("信息管理中心:");
sBuffer.append("www");
sBuffer.append(".it");
sBuffer.append(".com");
System.out.println(sBuffer);
}
}
输出结果:
常用类_基本数据类型包装类(20200618)
- 基本数据类型包装类:
将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。
常用的操作之一:用于基本数据类型与字符串之间的转换。
基本数据类型与包装类的对应关系
byte → Byte
short → Short
int → Integer
long → Long
float → Float
double → Double
char → Character
boolean→ Boolean
常用类_Math类(20200619)
- Math类:封装了很多与数学有关的属性和方法。
常用类_File类(20200620)
- File类: java.io包下的File类用于描述和创建一个文件或文件夹对象,只能对文件或文件夹做一些简单操作,不能修改文件的内容,功能比较有限。下面是对于File类中常用方法的程序演示。
常用类_枚举类(20200621)
- 枚举类:
public class TestEnum {
public enum MyColor { red, green, blue };
public enum MyDoorOpener {me, mywife};
public static void main(String[] args) {
MyColor m = MyColor.red;
switch(m) {
case red:
System.out.println("red");
break;
case green:
System.out.println("green");
break;
default:
System.out.println("default");
}
System.out.println(m);
}
}