5.8引用传递及基本应用
引用传递:就是指将堆内存空间的使用权交给多个栈内存。
5.9 this关键字
this 的作用,表示类中的属性,表示当前对像,可以使用this调用构造方法
5.10 static关键字
5.11 代码快
5.12 对像数组
5.13 内部类
习题:
package com.java.oop.day3;
public class IndexOfTextDemo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//在字符串中找出相同的字符
String str = "want you to know noe thing";
Integer count = 0 ;
//equals+substring实现
for(int i = 0 ; i < str.length();i++){
String getstr=str.substring(i,i+1);
if(getstr.equals("o"))
{
count++;
}
}
System.out.println("o出现"+count+"次");
//indexOf+sqlit方法实现
count = 0;
String st [] = str.split(" ");//分出来
for(int i = 0 ; i < st.length;i++){//显示数组信息
if(st[i].indexOf("n") >= 0)//查找数组中是否是n
{
count++;//有就加1
}
}
System.out.println("n出现"+count+"次");
}
}
package com.java.oop.day3;
public class StringOperation {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 字符串操作
// 1,从字符串中“JAVA 技术班学习班 200703026” 中提取开班日期
getStringDemo1();
getStringDemo2();
// 2,将“MLDN JAVA”字符串中的JAVA 替换成J2EE
getReplaceDemo1();
}
// 1,从字符串中“JAVA 技术班学习班 200703026” 中提取开班日期
public static void getStringDemo1(){
String str = "Java 技术班学习班 200703026";
String st [] = str.split(" ");
System.out.println("提取开班日期:"+st[2]);
}
public static void getStringDemo2(){
String str = "Java 技术班学习班 200703026";
String st = str.substring(12);
System.out.println("提取开班日期:"+st);
}
// 2,将“MLDN JAVA”字符串中的JAVA 替换成J2EE
public static void getReplaceDemo1(){
String str = "NLDN JAVA";
// 方法1
// String st = str.replace("JAVA", "J2EE");
str = str.replaceAll("JAVA", "J2EE");
// 方法2
// String st = str.replaceFirst("JAVA", "J2EE");
// 方法3
// String st = str.substring(0,5)+"J2EE";
// 方法4
//String st []= str.split(" ");
// 方法5
/* int i = 0;
while (i < st.length){
if(i > 0){
st[i]=" J2EE";
}
str = st[0]+st[1];
i++;
}*/
System.out.println("NLDN JAVA 替换成J2EE:"+str);
// 去除字符串中的空子符
String string = "20070326 MLDN 老师";
String st[] = string.split(" ");
String isEmpty = "";
int i = 0;
while (i < st.length){
isEmpty += st[i].trim();
i++;
}
System.out.println("去除字符串中的空子符:"+isEmpty);
}
}