常见类的基本使用
来源:互联网 发布:js offsetwidth 属性 编辑:程序博客网 时间:2024/06/03 21:29
String类
(1)多个字符组成的一串数据。
其实它可以和字符数组进行相互转换。
(2)构造方法:
A:public String() B:public String(byte[] bytes) C:public String(byte[] bytes,int offset,int length) D:public String(char[] value) E:public String(char[] value,int offset,int count) F:public String(String original) 下面的这一个虽然不是构造方法,但是结果也是一个字符串对象 G:String s = "hello";
(3)字符串的特点
A:字符串一旦被赋值,就不能改变。 注意:这里指的是字符串的内容不能改变,而不是引用不能改变。 B:字面值作为字符串对象和通过构造方法创建对象的不同 String s = new String("hello");和String s = "hello"的区别?
(4)字符串的面试题(看程序写结果)
A:==和equals() 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 B:字符串的拼接 String s1 = "hello"; String s2 = "world"; String s3 = "helloworld"; System.out.println(s3 == s1 + s2);// false System.out.println(s3.equals((s1 + s2)));// true System.out.println(s3 == "hello" + "world");// false 这个我们错了,应该是true System.out.println(s3.equals("hello" + "world"));// true
(5)字符串的功能(自己补齐方法中文意思)
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) int indexOf(String str,int fromIndex) String substring(int start) String substring(int start,int end) C:转换功能 byte[] getBytes() char[] toCharArray() static String valueOf(char[] chs) static String valueOf(int i) String toLowerCase() String toUpperCase() String concat(String str) D:其他功能 a:替换功能 String replace(char old,char new) String replace(String old,String new) b:去空格功能 String trim() c:按字典比较功能 int compareTo(String str) int compareToIgnoreCase(String str)
StringBuffer类
(1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就
提供了 一个字符串缓冲区类。StringBuffer供我们使用。
(2)StringBuffer的构造方法
A:StringBuffer() B:StringBuffer(int size) C:StringBuffer(String str)
(3)StringBuffer的常见功能(自己补齐方法的声明和方法的解释)
A:添加功能 append(Object obj) 返回的是StringBuffer实例,可用于链式编程 B:删除功能 delete(int start, int end),deleteCharAt(int index) 返回的是StringBuffer实例 C:替换功能 replace(int start, int end, String str) 返回的是StringBuffer实例 D:反转功能 reverse() 返回的是StringBuffer实例 E:截取功能(注意这个返回值) substring(int start) substring(int start, int end) 返回的是String类型
(4)StringBuffer的练习(做一遍)
A:String和StringBuffer相互转换 String -- StringBuffer 构造方法 StringBuffer -- String toString()方法 B:字符串的拼接 C:把字符串反转 D:判断一个字符串是否对称
(5)面试题
小细节: StringBuffer:同步的,数据安全,效率低。 StringBuilder:不同步的,数据不安全,效率高。 A:String,StringBuffer,StringBuilder的区别 B:StringBuffer和数组的区别?
(6)注意的问题:
String作为形式参数,StringBuffer作为形式参数。
/* * 面试题: * 1:String,StringBuffer,StringBuilder的区别? * A:String是内容不可变的,而StringBuffer,StringBuilder都是内容可变的。 * B:StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高 * * 2:StringBuffer和数组的区别? * 二者都可以看出是一个容器,装其他的数据。 * 但是呢,StringBuffer的数据最终是一个字符串数据。 * 而数组可以放置多种数据,但必须是同一种数据类型的。 * * 3:形式参数问题 * String作为参数传递 * StringBuffer作为参数传递 * * 形式参数: * 基本类型:形式参数的改变不影响实际参数 * 引用类型:形式参数的改变直接影响实际参数 * * 注意: * String作为参数传递,效果和基本类型作为参数传递是一样的。 */public class StringBufferDemo { public static void main(String[] args) { String s1 = "hello"; String s2 = "world"; System.out.println(s1 + "---" + s2);// hello---world change(s1, s2); System.out.println(s1 + "---" + s2);// hello---world StringBuffer sb1 = new StringBuffer("hello"); StringBuffer sb2 = new StringBuffer("world"); System.out.println(sb1 + "---" + sb2);// hello---world change(sb1, sb2); System.out.println(sb1 + "---" + sb2);// hello---worldworld } public static void change(StringBuffer sb1, StringBuffer sb2) { sb1 = sb2; sb2.append(sb1); } public static void change(String s1, String s2) { s1 = s2; s2 = s1 + s2; }}
Array及Arrays—数组及操作数组的工具类
(1)排序
A:冒泡排序
相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处。同理,其他的元素就可以排好。
public static void bubbleSort(int[] arr) { for(int x=0; x<arr.length-1; x++) { for(int y=0; y<arr.length-1-x; y++) { if(arr[y] > arr[y+1]) { int temp = arr[y]; arr[y] = arr[y+1]; arr[y+1] = temp; } } } } B:选择排序 把0索引的元素,和索引1以后的元素都进行比较,第一次完毕,最小值出现在了0索引。同理,其他的元素就可以排好。 public static void selectSort(int[] arr) { for(int x=0; x<arr.length-1; x++) { for(int y=x+1; y<arr.length; y++) { if(arr[y] < arr[x]) { int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } } } }(2)查找 A:基本查找 针对数组无序的情况 public static int getIndex(int[] arr,int value) { int index = -1; for(int x=0; x<arr.length; x++) { if(arr[x] == value) { index = x; break; } } return index; } B:二分查找(折半查找) 针对数组有序的情况(千万不要先排序,在查找) public static int binarySearch(int[] arr,int value) { int min = 0; int max = arr.length-1; int mid = (min+max)/2; while(arr[mid] != value) { if(arr[mid] > value) { max = mid - 1; }else if(arr[mid] < value) { min = mid + 1; } if(min > max) { return -1; } mid = (min+max)/2; } return mid; }(3)Arrays工具类 A:是针对数组进行操作的工具类。包括排序和查找等功能。 B:要掌握的方法 把数组转成字符串:static String toString(Object[] a) 排序:static void sort(Object[] a) 二分查找:static int binarySearch(Object[] a, Object key) (4)把字符串中的字符进行排序 举例: "edacbgf" 得到结果:先转换为字符数组通过Arrays类排序后转换成字符串输出。 "abcdefg"
Calendar
(1) 日历类,封装了所有的日历字段值,通过统一的方法根据传入不同的日历字段可以获取值。
(2)如何得到一个日历对象呢?
Calendar rightNow = Calendar.getInstance(); 本质返回的是子类对象
(3)成员方法
A:根据日历字段得到对应的值 B:根据日历字段和一个正负数确定是添加还是减去对应日历字段的值 C:设置日历对象的年月日
(4)案例:
计算任意一年的2月份有多少天?
import java.util.Calendar;import java.util.Scanner;/* * 获取任意一年的二月有多少天 * * 分析: * A:键盘录入任意的年份 * B:设置日历对象的年月日 * 年就是A输入的数据 * 月是2 * 日是1 * C:把时间往前推一天,就是2月的最后一天 * D:获取这一天输出即可 */public class CalendarTest { public static void main(String[] args) { // 键盘录入任意的年份 Scanner sc = new Scanner(System.in); System.out.println("请输入年份:"); int year = sc.nextInt(); // 设置日历对象的年月日 Calendar c = Calendar.getInstance(); c.set(year, 2, 1); // 其实是这一年的3月1日 // 把时间往前推一天,就是2月的最后一天 c.add(Calendar.DATE, -1); // 获取这一天输出即可 System.out.println(c.get(Calendar.DATE)); }}
Date/DateFormat
(1)Date是日期类,可以精确到毫秒。
A:构造方法 Date() Date(long time) B:成员方法 getTime() setTime(long time) C:日期和毫秒值的相互转换
案例:你来到这个世界多少天了?
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Scanner;/* * 算一下你来到这个世界多少天? * * 分析: * A:键盘录入你的出生的年月日 * B:把该字符串转换为一个日期 * C:通过该日期得到一个毫秒值 * D:获取当前时间的毫秒值 * E:用D-C得到一个毫秒值 * F:把E的毫秒值转换为年 * /1000/60/60/24 */public class MyYearOldDemo { public static void main(String[] args) throws ParseException { // 键盘录入你的出生的年月日 Scanner sc = new Scanner(System.in); System.out.println("请输入你的出生年月日:"); String line = sc.nextLine(); // 把该字符串转换为一个日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date d = sdf.parse(line); // 通过该日期得到一个毫秒值 long myTime = d.getTime(); // 获取当前时间的毫秒值 long nowTime = System.currentTimeMillis(); // 用D-C得到一个毫秒值 long time = nowTime - myTime; // 把E的毫秒值转换为年 long day = time / 1000 / 60 / 60 / 24; System.out.println("你来到这个世界:" + day + "天"); }}
(2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类SimpleDateFormat
A:SimpleDateFormat(String pattern) 给定模式 yyyy-MM-dd HH:mm:ss B:日期和字符串的转换 a:Date -- String format() b:String -- Date parse()
1 0
- 常见类的基本使用
- vi的基本使用和常见命令
- iOS常见控件的基本使用
- Git的基本使用和常见场景
- 常见的基本流
- Objective-C 【NSArray 的基本使用&常见方法&简写形式】
- 常见对象_正则表达式的概述和基本使用
- 常见集合类的使用
- 常见的基本Windows类型
- 常见的基本汇编书籍
- XAMPP的基本常见使用方法
- 常见的网络基本问题
- 常见操作系统的基本类型
- const的常见基本用法
- 图的基本、常见操作
- 常见的php基本函数
- php常见基本的函数
- 黑马程序员——NSArray的基本使用和常见方法的使用
- 飞机游戏增加BOSS功能
- Spring MVC 流程图
- LeetCode 063 Unique Paths II
- 安装虚拟机
- CheckedTextView 复选框左显示
- 常见类的基本使用
- UI坐标系转换
- Kinect开发教程——OpenNI的安装与开发环境的配置
- 教你ios10怎么降级到ios9.3.2
- iOS--错误集锦--load the "xx" nib but the view outlet was not set.
- MAC地址
- 347. Top K frequent element
- servlet request getHeader("x-forwarded-for") 获取真实IP
- 神经网络入门(连载之二)