JAVA视频学习笔记(六)
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
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);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200615092837917.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FzZGVuZQ==,size_16,color_FFFFFF,t_70
public class TestDateSort {
public static void main(String[] args) {
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= 'a' && c <= 'z') {
lCount ++;
} else if (c >='A' && c <= 'Z') {
uCount ++;
} else {
oCount ++;
}
}
*/
/*
String sL = "abcdefghijklmnopqrstuvwxyz";
String sU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i=0; i
/*
for(int i=0; i
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);
}
}
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);
}
}
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);
}
}