黑马程序员技术交流社区
标题: 基础班课堂笔记11-12天 [打印本页]
作者: 曹操001 时间: 2015-6-21 00:49
标题: 基础班课堂笔记11-12天
(3)面试题:
==和equals()的区别?
==:
比较基本类型:比较的是基本类型的值是否相同。
比较引用类型:比较的是引用类型的地址值是否相同。
equals():
比较引用类型,默认比较的是引用类型的地址值是否相同。
如果类重写了该方法,那就得按照重写后的规则进行比较。
5、Scanner注意问题
1、Scanner中方法
next() -- 查找并返回来自此扫描器的下一个完整标记。
nextLine() -- 此扫描器执行当前行,并返回跳过的输入信息。
next() 和 nextLine() 区别:
next()会将空格键、Tab键或Enter键等视为分隔符或结束符,不能得到带空格的字符串。
Scanner scanner = new Scanner(System.in);
String string = scanner.next()
while(true){
System.out.println(string);
string = scanner.next();
}
nextLine()仅将Enter键作为结束符,返回Enter键前的所有字符,可以得到带空格。
2、String类——构造方法(掌握)
1、字符串概念
由多个字符组成的一串数据。
2、构造方法
创建字符串对象的几种方式
String s = new String(); | 通过字节数组创建字符串对象 |
String s = new String(byte[] bys); | 通过字节数组创建字符串对象常用 |
String s = new String(byte[] bys,int index, int length) | 通过字节数组一部分创建字符串对象 |
String s = new String(char[] chs) | 通过字符数组创建字符串对象常用 |
String s = new String(char[] chs, int index, int length); | 通过字符数组一部分创建字符串对象 |
String s = new String(String); | 通过给构造方法传入字符串创建字符字符串对象 |
String s = “helloworld” | 直接给字符串对象赋值常用 |
3、字符串的特点及面试题
1、特点及注意事项
字符串一旦被赋值,就不能改变。 注意:字符串的值不能改变,引用是可以改变的。
2、面试题
a:String s = new String("hello")和String s = "hello"的区别。
答:new String(“hello”)在内存中创建了1个或两个对象,为什么..
“hello”在内存中创建了0个或一个对象,为什么…
b:请写出结果:
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3==s4);
System.out.println(s3.equals(s4));
String s5 = "hello";
String s6 = "hello";
System.out.println(s5==s6);
System.out.println(s5.equals(s6));
c : ""和null的区别
最本质的区别是否在内存中开辟内存空间,"'会开辟内存空间,而null不会,在开发的时候要养成良好的习惯用null
4、String类——成员方法(掌握-通过Eclipse&API会用即可)
1、判断功能
boolean equals(Object obj) | 判断字符串的内容是否相同,区分大小写 |
boolean equalsIgnoreCase(String str) | 判断字符串的内容是否相同,忽略大小写 |
boolean contains(String str) | 判断字符串对象是否包含给定的字符串 |
boolean startsWith(String str) | 判断字符串对象是否是以给定的字符串开始 |
boolean endsWith(String str) | 判断字符串对象是否是以给定的字符串结束 |
boolean isEmpty() | 判断字符串对象是否为空,注意是数据 |
3、转换功能
byte[] getBytes() | 把字符串转换成字节数组 |
char[] toCharArray() | 把字符串转换成字符数组 |
static String copyValueOf(char[] chs) | 把字符数组转换成字符串 底层调用new String(char[] chs) |
static String valueOf(char[] chs) | 把字符数组转换成字符串 底层调用new String(char[] chs) |
static String valueOf(任意类型 变量名) | 把任意类型转换成字符串 |
String toLowerCase() | 把字符变成小写原字符串不变 |
String toUpperCase() | 把字符编程大写原字符串不变 |
String concat(String str) | 字符串链接原字符串不变 |
作者: 曹操001 时间: 2015-6-21 00:50
下面的这个是第13天的课堂笔记
1:数组操作(理解)
查找
--普通查找:数组无序
--二分查找(折半查找):数组有序
代码:
public static int getIndex(int[] arr,int value)
{
int maxIndex = arr.length-1;
int minIndex = 0;
int midIndex = (maxIndex+minIndex)/2;
while(arr[midIndex]!=value)
{
if(arr[midIndex]>value)
{
maxIndex = midIndex - 1;
}
else if(arr[midIndex]<value)
{
minIndex = midIndex + 1;
}
if(minIndex > maxIndex)
{
return -1;
}
midIndex = (maxIndex+minIndex)/2;
}
return midIndex;
}
2:Arrays工具类的使用(掌握)
(1)Arrays是针对数组操作的工具类。
(2)成员方法:
public static String toString(数组):把数组变成字符串。
public static void sort(数组):对数组进行排序。
public static int binarySearch(int[] arr,int value):二分查找
3:System的方法(掌握)
(1)系统类,提供了静态的变量和方法供我们使用。
(2)成员方法:
public static void exit(int value):退出jvm,非0表示异常退出。
public static long currentTimeMillis():返回当前系统时间的毫秒值。
和1970 年 1 月 1 日午夜之间的时间差
4:StringBuffer(掌握)
(1)字符个数可以发生改变的字符串类。字符串缓冲区类。
(2)构造方法:
A:StringBuffer()
B:StringBuffer(int capacity)
C:StringBuffer(String str)
(3)成员方法:(自己补齐)
A:添加功能
B:删除功能
C:替换功能
D:截取功能
E:反转功能
(4)案例:
字符串反转。
5:基本类型包装类(掌握)
(1)基本类型的数据我们只能使用值,不能做更多的操作。为了方便我们操作,
java就把每种基本类型进行了包装。提供方法供我们使用。
(2)基本类型和包装类的对应关系
byte
short
int Integer
long
float
double
char Character
boolean
(3)Integer构造方法
A:Integer i = new Integer(int num);
B:Integer i = new Integer(String s);
注意:s必须是一个由数字字符组成的字符串。
(4)String和int类型的转换
A:String -- int
Integer:
public static int parseInt(String s)
B:int -- String
Integer:
public static String toString(int i)
String:
public static String valueOf(int i)
(5)JDK5以后的新特性
A:自动装箱 基本类型--引用类型
B:自动拆箱 引用类型--基本类型
举例:
Integer i = 100;
i += 200;
(6)面试题:byte常量池
(7)案例:
把字符串中的数字字符排序。
需求:"23 98 16 38 42"
结果:"16 23 38 42 98"
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |