您的位置:首页 > 编程语言 > Java开发

java基础11、12

2014-04-06 14:35 435 查看
1.Object类
(1)是定义在java.lang包下的,是所有类的超类。所有类都直接或者间接的继承自Object类。
父类:超类,根类,基类。
子类:派生类。
(2)要掌握的方法:
public String toString():返回对象的字符串表示形式。
默认情况下的组合:类的全路径名称+@+对象的哈希值的十六进制表示。
这种做法对我们来说,一般没有意义,所以,建议重写。
重写做法:一般是把所有的成员变量组合成一个字符串返回。
实在不愿意自己写,自动生成。
public boolean equals(Object obj):比较对象的地址值是否相同。
默认情况下,比较的是对象的地址值是否相同。
如果有自己的特殊需求,请重写Object类的该方法。
怎么重写呢?
public boolean equals(Object obj) {
if(this == obj) {
return true;
}

if(!(obj instraceof Student)) {
return false;
}

Student s = (Student) obj;
return this.name.equals(s.name) && this.age == s.age;
}

如果有多个成员变量,把多个比较用&&连接即可。
引用类型用equals()方法。
基本类型用==号即可。
实在不愿意自己写,自动生成。
(3)面试题:
==和equals()的区别?
==:
可以比较基本类型,也可以比较引用类型。
比较基本类型,比较的是值是否相同。
比较引用类型,比较的是地址值是否相同。
equals:
只能比较引用类型。
默认情况下,比较的是地址值是否相同。
如果想比较内容,请自己重写Object类的equals()方法。

1:Scanner的概述
(1)Scanner是JDK5以后出现的方便我们从键盘接受数据的类。
(2)Scanner的构造格式:
Scanner sc = new Scanner(System.in);

System.in 是System类下面有一个静态的成员变量in。它的类型是InputStream。

 代表的是标准键盘输入流。也就是键盘录入数据。
Scanner是对其进行了封装,提供了各种转换功能。方便我们获取到想要的数据类型的数据。
(3)要掌握的两个功能:
A:返回int类型
public int nextInt()
B:返回String类型
public String nextLine()

注意事项:
先int,在String会有问题。
解决方案:
重新建立Scanner对象。
把所有的数据都看成是String类型,将来转换为int类型。

2:String类的概述和使用
(1)由多个字符组成的一串数据。
(2)构造方法:
A:String s = new String();
B:String s = new String(byte[] bys);
C:String s = new String(byte[] bys,int startIndex,int count);
D:String s = new String(char[] chs);
E:String s = new String(char[] chs,int startIndex,int count);
F:String s = new String(String s2);
G:String s = "hello";

长使用:
BCDEG
(3)面试题:
A:字符串一旦被赋值就不能被改动。
注意:这里的改动指的是字符串的内容,而不是字符串对象的引用。
B:String s = new String("hello");和String s = "hello";有区别吗?是什么呢?
有。
前者创建了两个对象。
后者创建了一个对象。

C:看程序,写结果
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1==s2); //false
System.out.println(s1.equals(s2)); //true

String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3==s4); //false
System.out.println(s3.equals(s4)); //true

String s5 = "hello";
String s6 = "hello";
System.out.println(s5==s6); //true
System.out.println(s5.equals(s6)); //true
D:看程序,写结果
String s7 = "hello";
String s8 = "world";
String s9 = "helloworld";
System.out.println(s9==s7+s8); //false
System.out.println(s9=="hello"+"world");//true

变量就直接造,常量先找,如果有就使用,否则就造。 (4)字符串的常见功能:(补齐中文)
A:判断功能
boolean equals(Object obj):判断与指定内容是否相等
boolean equalsIgnoreCase(String str)判断与指定字符串内容是否相等,忽略大小写
boolean contains(String str):判断这个字符串是否包含指定字符串
boolean startsWith(String str):判断该字符串是否以指定字符串开头
boolean endsWith(String str):判断该字符串是否以指定字符串结尾
boolean isEmpty():判断指定字符串内容是否为空

B:获取功能
int length():获取字符串长度
char charAt(int index):获取指定索引的字符
int indexOf(int ch):获取指定字符第一次出现的索引
int indexOf(String str);获取指定字符串第一次出现的索引值
int indexOf(int ch,int fromIndex):获取指定字符从fromIndex位置开始,第一次出现的索引
int indexOf(String str,int fromIndex):获取指定字符串从fromIndex位置开始,第一次出现的索引
String substring(int start):获取从start位置开始到字符串结尾的新字符串
String substring(int start,int end):获取从start位置开始到end位置不包括end位置的元素的新字符串

C:转换功能
byte[] getBytes():将字符串转换为字节数组
char[] toCharArray():将字符串转换为字符数组
static String copyValueOf(char[] chs)将字符数组转换为字符串
static String valueOf(char[] chs):把字符数组转换为字符串
static String valueOf(int i):将基本数据类型转换为字符串
String toLowerCase():将字符串转换为小写
String toUpperCase():将字符串转换为大写
String concat(String str):将指定字符串连接到该字符串末尾

D:其他功能
String replace(char old,char new):将字符串中的字符用指定字符替代
String replace(String old,String new):将字符串中的某段字符串用指定字符串替代

String[] split(String regex):将字符串以指定的字符串标记切割为字符数组

String trim():去掉字符串开头和结尾处的空格

int compareTo(String str):比较字符串与指定字符串的字典位置,大于该字符串则返回正数,小于该串则返回负数,想等则返回0
int compareToIgnoreCase(String str) :比较字符串与指定字符串的字典位置,不区分字母大小写大于该字符串则返回正数,小于该串则返回负数,相等则返回0

案例:

A:模拟用户登录
//用户类:
package cn.jiang.test0322;

public class User {
//用户帐号
private String userName;
//用户密码
private String userPassWord;

public User() {}

public User(String userName, String userPassWord) {
this.userName = userName;
this.userPassWord = userPassWord;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserPassWord() {
return userPassWord;
}

public void setUserPassWord(String userPassWord) {
this.userPassWord = userPassWord;
}

public boolean isEquals(String userName,String userPassWord){
return this.userName.equals(userName)
&&this.userPassWord.equals(userPassWord);
}
}
测试类:
package cn.jiang.test0322;

import java.util.Scanner;

public class Test2 {
public static void main(String[] args) {
//获取键盘录入
Scanner sc=new Scanner(System.in);
//创建用户对象
User us=new User();
System.out.println("请先注册、再登陆!");
System.out.println("请输入一个帐号:");
us.setUserName(sc.nextLine());
System.out.println("请输入一个密码:");
us.setUserPassWord(sc.nextLine());
System.out.println("注册成功!");
System.out.println("请登录!");

for(int i=2;i>=0;i--){
System.out.println("请输入您的帐号:");
String name=sc.nextLine();
System.out.println("请输入您的密码:");
String word=sc.nextLine();
if(us.isEquals(name, word)){
System.out.println("登录成功!");
break;
}else{
if(i==0){
System.out.println("登录失败!");
break;
}else{
System.out.println("登录失败!您还有"+i+"次机会!");
}
}
}
}
}

B:遍历字符串
import java.util.Scanner;
class StringErgodic{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str=sc.nextLine();
for(int i=0;i<str.length();i++){
System.out.print(str.CharAt(i)+" ");
}
}
}
C:统计字符串中大写字母,小写字母以及数字字符出现的次数
import java.util.Scanner;
class StringCount{
public static void main(String[] args){
int UpperCount=0;
int LowerCount=0;
int NumCount=0;
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串(只包含大小写字母和数字):");
String str=sc.nextLine();
for(int i=0;i<str.length();i++){
char ch=str.CharAt(i);
if(ch>='A'&&ch<='Z'){
UpperCount++;
}else if(ch>='a'&&ch<='z'){
LowerCount++;
}else if(ch>='0'&&ch<='9'){
NumCount++;
}
}
System.out.println("大写个数:"+UpperCount+",小写个数:"+LowerCount+",数字个数:"+NumCount);
}
}
D:把一个字符串的首字母变成大写,其他的全部小写
import java.util.Scanner;
class StringSwitch{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str=sc.nextLine();
String str1=str.substring(0,1);
String str2=str.substring(1);
System.out.println(str1.toUpperCase()+str2.toLowerCase());
}
}
E:统计大串中小串出现的次数
class StringMinString{
public static void main(String[] args){
String maxStr="hanbasdnbafllgnbahjnbakqqqqlnbaxi";
String minStr="nba";
int count=getCount(maxStr,minStr);
System.out.println("出现的次数为:"+count);
}
public staic int getCount(String maxStr,String minStr){
int count=0;
int index=0;
while((index=maxStr.indexOf(minStr))!=-1){
count++;
maxStr=maxStr.substring(index+minStr.length());
}
return count;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息