Java自己java 映射类定义方法的类名点方法名和String str = "a-b-c-d";

一、String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。二、String类对象的创建字符串声明:String stringN字符串创建:stringName = new String(字符串常量);或stringName = 字符串常量;三、String类构造方法1、public String()无参构造方法,用来创建空字符串的String对象。&1 String str1 = new String();&2、public String(String value)用已知的字符串value创建一个String对象。&1 String str2 = new String("asdf"); 2 String str3 = new String(str2);&3、public String(char[] value)用字符数组value创建一个String对象。
1 char[] value = {"a","b","c","d"};
2 String str4 = new String(value);//相当于String str4 = new String("abcd");
4、public String(char chars[], int startIndex, int numChars)用字符数组chars的startIndex开始的numChars个字符创建一个String对象。
1 char[] value = {"a","b","c","d"};
2 String str5 = new String(value, 1, 2);//相当于String str5 = new String("bc");
5、public String(byte[] values)用比特数组values创建一个String对象。
1 byte[] strb = new byte[]{65,66};
2 String str6 = new String(strb);//相当于String str6 = new String("AB");
四、String类常用方法1、求字符串长度public int length()//返回该字符串的长度
1 String str = new String("asdfzxc");
2 int strlength = str.length();//strlength = 7
2、求字符串某一位置字符public char charAt(int index)//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。
1 String str = new String("asdfzxc");
2 char ch = str.charAt(4);//ch = z
3、提取子串用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:1)public String substring(int beginIndex)//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。2)public String substring(int beginIndex, int endIndex)//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。
1 String str1 = new String("asdfzxc");
2 String str2 = str1.substring(2);//str2 = "dfzxc"
3 String str3 = str1.substring(2,5);//str3 = "dfz"
4、字符串比较1)public int compareTo(String anotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。2)public int compareToIgnore(String anotherString)//与compareTo方法相似,但忽略大小写。3)public boolean equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。4)public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。
1 String str1 = new String("abc");
2 String str2 = new String("ABC");
3 int a = str1.compareTo(str2);//a&0
4 int b = str1.compareTo(str2);//b=0
5 boolean c = str1.equals(str2);//c=false
6 boolean d = str1.equalsIgnoreCase(str2);//d=true
5、字符串连接public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。
1 String str = "aa".concat("bb").concat("cc");
2 相当于String str = "aa"+"bb"+"cc";
6、字符串中单个字符查找1)public int indexOf(int ch/String str)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。2)public int indexOf(int ch/String str, int fromIndex)//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。3)public int lastIndexOf(int ch/String str)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。4)public int lastIndexOf(int ch/String str, int fromIndex)//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
1 String str = "I am a good student";
2 int a = str.indexOf('a');//a = 2
3 int b = str.indexOf("good");//b = 7
4 int c = str.indexOf("w",2);//c = -1
5 int d = str.lastIndexOf("a");//d = 5
6 int e = str.lastIndexOf("a",3);//e = 2
7、字符串中字符的大小写转换1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串
1 String str = new String("asDF");
2 String str1 = str.toLowerCase();//str1 = "asdf"
3 String str2 = str.toUpperCase();//str2 = "ASDF"
8、字符串中字符的替换1)public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。2)public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
1 String str = "asdzxcasd";
2 String str1 = str.replace('a','g');//str1 = "gsdzxcgsd"
3 String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"
4 String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"
5 String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh"
9、其他类方法1)String trim()//截去字符串两端的空格,但对于中间的空格不处理。
1 String str = " a sd ";
2 String str1 = str.trim();
3 int a = str.length();//a = 6
4 int b = str1.length();//b = 4
2)boolean statWith(String prefix)或boolean endWith(String suffix)//用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。
1 String str = "asdfgh";
2 boolean a = str.statWith("as");//a = true
3 boolean b = str.endWith("gh");//b = true
3)regionMatches(boolean b, int firstStart, String other, int otherStart, int length)//从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。4)contains(String str)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。
1 String str = "student";
2 str.contains("stu");//true
3 str.contains("ok");//false
5)String[] split(String str)//将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。
1 String str = "asd!qwe|zxc#";
2 String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";
五、字符串与基本类型的转换1、字符串转换为基本类型java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:1)public static byte parseByte(String s)2)public static short parseShort(String s)3)public static short parseInt(String s)4)public static long parseLong(String s)5)public static float parseFloat(String s)6)public static double parseDouble(String s)例如:
1 int n = Integer.parseInt("12");
2 float f = Float.parseFloat("12.34");
3 double d = Double.parseDouble("1.124");
2、基本类型转换为字符串类型String类中提供了String valueOf()放法,用作基本类型转换为字符串类型。1)static String valueOf(char data[])2)static String valueOf(char data[], int offset, int count)3)static String valueOf(boolean b)4)static String valueOf(char c)5)static String valueOf(int i)6)static String valueOf(long l)7)static String valueOf(float f)8)static String valueOf(double d)例如:
1 String s1 = String.valueOf(12);
2 String s1 = String.valueOf(12.34);
3、进制转换使用Long类中的方法得到整数之间的各种进制转换的方法:Long.toBinaryString(long l)Long.toOctalString(long l)Long.toHexString(long l)Long.toString(long l, int p)//p作为任意进制
阅读(...) 评论()java课后习题答案_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
java课后习题答案
&&java课后习题答案
你可能喜欢JAVA期末试题及答案_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
JAVA期末试题及答案
阅读已结束,下载本文需要
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩37页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢java中StringBuffer类的常用方法怎么用?_百度知道
java中StringBuffer类的常用方法怎么用?
(1)append()
(2)charcharAt(int n)
(3)StringBufferinert(int index,String str)
(4)StringBufferreverse( )
(5)StringBufferdelete(int startindex, int endindex)
(6)StringBufferreplace(int startindex, int endindex,String str)
我有更好的答案
//返回下标为1的字符&此处是&nbsp.reverse();处插入新的字符串&nbspStringBuffer&nbsp.insert(1,&d&&););&d&&此时变为&Hedllo&&nbsp.delete(1,2),&//删除字符串&&此时变为Hllo&worldsb.replace(3,4,&new&此时变成&&//在&1&此时变成&&//替换字符串&&从&eworld&);&&&//在sb尾部追加一个字符串;&sb.append(&&nbsp.charAt(1)&;);&StringBuffer(&Hello&//反转字符&sb&&&&&=&new&&Hello&&dlrow&olldeHsb
怎么输出呢?
System.out.println(sb.toString());
采纳率:62%
16&&nbsp。&参数的字符串表示形式追加到此序列。&&StringBuffer&append(Object&int&len)&&&&&&&&&&&将&nbsp。&c)&&nbsp。&nbsp。&&&&&&int&capacity()&&&&&&&&&&&返回当前容量;&char&&&&&返回指定索引处的字符(统一代码点);&&nbsp。&&返回此序列中指定索引处的&&nbsp。&obj)&&&&&&&&StringBuffer&&参数的字符串表示形式。&&StringBuffer&append(String&&StringBuffer&&&&StringBuffer(int&nbsp。&append(char[]&str)&&&&&&&&&&&将&&&&&&long&&&&追加&&&&&&nbsp。&appendCodePoint(int&codePoint)&&&&&&&&&&&将&char&nbsp,其初始容量为&&&&&StringBuffer&int&参数的字符串表示形式追加到此序列;&&&&将指定的字符串追加到此字符序列;str)&&&&构造一个其中不带字符的字符串缓冲区;&&&&&&&&nbsp。&boolean&参数的字符串表示形式追加到序列。&&StringBuffer&append(char&&&&&数组参数的字符串表示形式追加到此序列。&&StringBuffer&append(char[]&&nbsp,&&&&值;&&个字符;index)&将&float&&&构造一个字符串缓冲区,并将其内容初始化为指定的字符串内容。&&方法摘要&&StringBuffer&append(boolean&b)&&&&&&&&&&&将&&&&&&&nbsp。&capacity)&&&&&&&&&&&构造一个不带字符;&&nbsp,&&Object&append(StringBuffer&sb)&&&追加到此序列中,但具有指定初始容量的字符串缓冲区。&StringBuffer(String&数组参数的子数组的字符串表示形式追加到此序列。&&StringBuffer&append(double&&&&&&&StringBuffer&char&StringBuffer&&str)&&&index)&&&&int&codePointBefore(int&将&double&参数的字符串表示形式追加到此序列;&&d)&&&charAt(int&&append(long&lng)&&&&&&&&&&&将&&将指定的&&&将&char&参数的字符串表示形式追加到此序列;&&&int&codePointAt(int&&&StringBuffer&index)&int&codePoint&参数的字符串表示形式追加到此序列;char&append(float&&nbsp构造方法摘要&f)&&&参数的字符串表示形式追加到此序列。&&StringBuffer&append(int&i)&&&&&&&&&&&将&StringBuffer()&&&&&&&&&返回指定索引前的字符(统一代码点)。&&int&codePointCount(int&beginIndex,&int&endIndex)&&&&&&&&&&&返回此序列指定文本范围内的统一代码点。&&StringBuffer&delete(int&start,&int&end)&&&&&&&&&&&移除此序列的子字符串中的字符。&&StringBuffer&deleteCharAt(int&index)&&&&&&&&&&&移除此序列指定位置的&char。&&void&getChars(int&srcBegin,&int&srcEnd,&char[]&dst,&int&dstBegin)&&&&&&&&&&&将字符从此序列复制到目标字符数组&dst。&&int&indexOf(String&str)&&&&&&&&&&&返回第一次出现的指定子字符串在该字符串中的索引。&&int&indexOf(String&str,&int&fromIndex)&&&&&&&&&&&从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。&&StringBuffer&insert(int&offset,&boolean&b)&&&&&&&&&&&将&boolean&参数的字符串表示形式插入此序列中。&&StringBuffer&insert(int&offset,&char&c)&&&&&&&&&&&将&char&参数的字符串表示形式插入此序列中。&&StringBuffer&insert(int&offset,&char[]&str)&&&&&&&&&&&将&char&数组参数的字符串表示形式插入此序列中。&&StringBuffer&insert(int&index,&char[]&str,&int&offset,&int&len)&&&&&&&&&&&将数组参数&str&的子数组的字符串表示形式插入此序列中。&&StringBuffer&insert(int&dstOffset,&CharSequence&s,&int&start,&int&end)&&&&&&&&&&&将指定&CharSequence&的子序列插入此序列中。&&StringBuffer&insert(int&offset,&double&d)&&&&&&&&&&&将&double&参数的字符串表示形式插入此序列中。&&StringBuffer&insert(int&offset,&float&f)&&&&&&&&&&&将&float&参数的字符串表示形式插入此序列中。&&StringBuffer&insert(int&offset,&int&i)&&&&&&&&&&&将&int&参数的字符串表示形式插入此序列中。&&StringBuffer&insert(int&offset,&long&l)&&&&&&&&&&&将&long&参数的字符串表示形式插入此序列中。&&StringBuffer&insert(int&offset,&Object&obj)&&&&&&&&&&&将&Object&参数的字符串表示形式插入此字符序列中。&&StringBuffer&insert(int&offset,&String&str)&&&&&&&&&&&将字符串插入此字符序列中。&&int&lastIndexOf(String&str)&&&&&&&&&&&返回最右边出现的指定子字符串在此字符串中的索引。&&int&length()&&&&&&&&&&&返回长度(字符数)。&&&StringBuffer&replace(int&start,&int&end,&String&str)&&&&&&&&&&&使用给定&String&中的字符替换此序列的子字符串中的字符。&&StringBuffer&reverse()&&&&&&&&&&&将此字符序列用其反转形式取代。&&void&setCharAt(int&index,&char&ch)&&&&&&&&&&&将给定索引处的字符设置为&ch。&&void&setLength(int&newLength)&&&&&&&&&&&设置字符序列的长度。&&String&substring(int&start)&&&&&&&&&&&返回一个新的&String,它包含此字符序列当前所包含的字符子序列。&&String&substring(int&start,&int&end)&&&&&&&&&&&返回一个新的&String,它包含此序列当前所包含的字符子序列。&&String&toString()&&&&&&&&&&&返回此序列中数据的字符串表示形式。
为您推荐:
其他类似问题
stringbuffer的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。java编程 定义一个接口Person,声明相关的属性和方法,再用Teacher类和Student_百度知道
java编程 定义一个接口Person,声明相关的属性和方法,再用Teacher类和Student
编写测试类创建10个Teacher和10个Student类实例,声明相关的属性和方法,再用Teacher类和Student类去继承这个接口java编程定义一个接口Person,显示输出相关属性。(学生有学号
我有更好的答案
&nbsp.=&strN } public&& } public&String&GetSex() {
return&bSex&;女&;));
SSetName(String&&nbsp,String&bSex) {
&String& private&nbsp.strName);
S+&&男&&;{} public&Student(String&name?&void&&&strStudentN&//学号 private&String&&&&strA&
strStudentNumber&String&String&strNSetSex(boolean&&void&nbsp.bSex&?&&男&quot.&=&strAtrue&strStuNum) {
strStudentNumber&&&void&:&&女&&&
bSex&&nbsp.com/zhidao/wh%3D600%2C800/sign=cecaa113bbe5/7af40ad162d9f2d32c5cb5d4aeec8a136327ccb8.jpg" esrc="http://d.strName); public&&//true=男,false=女 private&=&void&&&strStudentN } public&&&&}class&Person{ public&&&nbsp.println(& } public&void&ShowInf() {
System.out.println(&姓名;&&SetAddress(String&strAdr) {
strAddress&Student&implements&Person{ private&&(this:&&性别:&&+&&&nbsp.hiphotos.baidu.com/zhidao/pic/item/7af40ad162d9f2d32c5cb5d4aeec8a136327ccb8;GetName();&nbsp:&void&=& } public&GetStudentNumber() {
strAddress&nbsp,String&nbsp.ShowInf();&SetName(String&abstract&住址:&&+&nbsp.this.strStudentNumber);
System.equals(&男&String&)&; } public&String&GetAddress() {
return&strAddress.println(&quot.bSex&StuNum,String&adr) {
strName&=&strStuN } public&boolean&&&bSGetName() {
return&strN } public&strName) {
this.strName&&=&( public&abstract&void&SetSex(boolean&bSex);&//真表示男;=&StuNum,假表示女 public&abstract&String&GetSex().学号:&&+&String&nbsp.com/zhidao/wh%3D450%2C600/sign=77a7352890dda144da5c64b6af40ad162d9f2d32c5cb5d4aeec8a136327ccb8.jpg" />interface&nbsp://d;abstract&&& } public&void&SetStudentNumber(String&&//返回“男”或“女” public&&nbsp?&&nbsp.jpg" target="_blank" title="点击查看大图" class="ikqb_img_alink"><img class="ikqb_img" src="http://d.=&bS:&false);&//住址 public&Student()&&abstract&nbsp<a href="this.strAddress); }}class&Teacher&implements&Person{ private&String&&&&strN private&boolean&&&bS&&&&&&&&&&&//true=男,false=女 private&String&&&&strWorkN&&//工号 private&String&&&&strP&&//职称。等级:正高级、高级、一级、二级、三级 public&Teacher()&{} public&Teacher(String&name,String&sex,String&WrkNm,String&prf) {
strName&=&
bSex&=&(sex.equals(&男&)&?&true&:&false);
strWorkNumber&=&WrkNm;
strProfession&=& } public&void&SetName(String&strName) {
this.strName&=&strN } public&String&GetName() {
return&strN } public&void&SetSex(boolean&bSex) {
this.bSex&=&bS } public&String&GetSex() {
return&bSex&?&&男&&:&&女&; } public&void&SetWorkNumber(String&WrkNm) {
strWorkNumber&=&WrkNm; } public&String&GetWorkNumber() {
return&strWorkN } public&void&SetProfession(String&prf) {
strProfession&=& } public&String&GetProfession() {
return&strP } public&void&ShowInf() {
System.out.println(&姓名:&&+&this.strName);
System.out.println(&性别:&&+&(this.bSex&?&&男&&:&&女&));
System.out.println(&工号:&&+&this.strWorkNumber);
System.out.println(&职称:&&+&this.strProfession); }}public&class&Main{ public&static&void&main(String[]&args) {
Teacher[]&teacher&=&new&Teacher[10];
Student[]&student&=&new&Student[10];
int&i;&&&&&&&&teacher[0]&=&new&Teacher(&张三&,&男&,&1001&,&一级&);&&&&&&&&teacher[1]&=&new&Teacher(&李四&,&女&,&1031&,&高级&);&&&&&&&&teacher[2]&=&new&Teacher(&王五&,&女&,&1012&,&高级&);&&&&&&&&teacher[3]&=&new&Teacher(&赵六&,&男&,&1231&,&正高级&);&&&&&&&&teacher[4]&=&new&Teacher(&李树&,&男&,&1051&,&一级&);&&&&&&&&teacher[5]&=&new&Teacher(&刘诗&,&女&,&1022&,&二级&);&&&&&&&&teacher[6]&=&new&Teacher(&孙义&,&男&,&1101&,&二级&);&&&&&&&&teacher[7]&=&new&Teacher(&诸葛聪&,&男&,&1001&,&高级&);&&&&&&&&teacher[8]&=&new&Teacher(&王丽&,&女&,&1001&,&一级&);&&&&&&&&teacher[9]&=&new&Teacher(&张美&,&女&,&1001&,&一级&); &&&&student[0]&=&new&Student();&&&&&&&&student[0]&=&new&Student(&梁星&,&男&,&2015100&,&地址1&);&&&&&&&&student[1]&=&new&Student(&茹薇&,&女&,&2015122&,&地址2&);&&&&&&&&student[2]&=&new&Student(&昕可&,&女&,&2015322&,&地址3&);&&&&&&&&student[3]&=&new&Student(&龙震&,&男&,&2015264&,&地址4&);&&&&&&&&student[4]&=&new&Student(&彬栋&,&男&,&2015210&,&地址5&);&&&&&&&&student[5]&=&new&Student(&梅柔&,&女&,&2015031&,&地址6&);&&&&&&&&student[6]&=&new&Student(&辰康&,&男&,&2015004&,&地址7&);&&&&&&&&student[7]&=&new&Student(&鸿斌&,&男&,&2015079&,&地址8&);&&&&&&&&student[8]&=&new&Student(&初昭&,&女&,&2015298&,&地址9&);&&&&&&&&student[9]&=&new&Student(&雪静&,&女&,&2015233&,&地址10&);
for&(i&=&0;&i&&&10;&i&++)
System.out.println(&&--老师信息--&&);
teacher[i].ShowInf();
System.out.println(&&--学生信息--&&);
student[i].ShowInf();
} }}.println(&quot
采纳率:62%
为您推荐:
其他类似问题
&#xe675;换一换
回答问题,赢新手礼包&#xe6b9;
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。

我要回帖

更多关于 js定义类的对象和方法 的文章

 

随机推荐