黑马程序员——Java基础(六)之常用类

-----------android培训、java培训、java学习型技术博客、期待与您交流!------------

Eclipse的使用

Eclipse是一种常见的Java开发工具,它是一种可扩展的、开放源代码的IDE(Integrated Development Environment)。

下载地址:http://eclipse.org

Eclipse的工作空间:源代码的目录。

Eclipse的编译特点:在保存代码的时候就自动进行编译。

Eclipse的配置:

A:程序的编译和运行的环境配置:

window——Preferences——Java

编译环境:Compiler

运行环境:Installed JREs

注意:编译环境不能高于运行环境。

B:去掉默认注释:

window——Preferences——Java——Code Style——Code Templates

C:设置字体:

window——Preferences——General——Appearance——Colors And Fonts

Eclipse的常用快捷键

Alt+/  提示

Ctrl+Shift+f  格式化

Ctrl+Shift+o  导入包

Ctrl+/,Ctrl+Shift+/  注释

Alt+上下箭头  将选中代码上下移动

Eclipse中jar包和注释文档的使用

在编程过程中,当需要使用其它项目中的方法时,需要将该项目打包为一个jar包才能使用,并且还需要将该项目制作一个注释文档以供我们查看。

jar是多个class文件的压缩包,制作方法为:选中项目——右键——Export——Java——Jar——指定路径名称——Finish,制作完成后的使用方法为:复制制作好的jar包到项目路径下,右击——Build Path——Add to Build Path

注释文档是来解释说明jar包的,制作方法为:选中项目——右键——Export——Java——Javadoc——Finish

Debug的作用:调试程序,查看程序执行流程。


API(Application Programming Interface)

应用程序编程接口,指的是JDK中提供的各种功能的Java类。

API中提供了Java中所用的类及其方法,下面是一些常见的类和方法。


Object类(java.lang包)

概述:类层次结构的根类,所有类都直接或者间接的继承自该类。

构造方法:

public Object();

成员方法:

public int hashCode();  返回该对象的哈希码值。

哈希码值是将该对象的内部地址值转换成的一个整数值。

public final Class getClass();  返回此Object 的运行时类。

Class类下的方法:public String getName(); 以String的形式返回此Class对象所表示的实体。

public String toString();  返回该对象的字符串表示。

此方法等价于this.getClass().getName() + '@' + Integer.toHexString(this.hashCode());所以运行结果没意义,一般要重写。

public boolean equals(Object obj);  指示其他某个对象是否与此对象“相等”;

默认情况下比较的是地址值,所以无意义,要重写,重写后,一般用来比较对象的成员变量值是否相同。

protected void finalize();  用于垃圾回收。

protected Object clone();  穿件并返回该对象的一个副本。

因为该方法用protected修饰,所以对象所在的类要重写此方法,并且还要实现Cloneable接口,才能实现对象的复制功能。


Scanner类(java.util包)

概述:JDK5以后用于获取用户的键盘录入对象。

构造方法:

public Scanner(InputStream source);

成员方法:

public boolean hasNextInt();

     判断键盘输入的数据是否是Int类型。

public  int nextInt();

     将键盘输入的数据转换为int类型并返回。

public String nextLine();

     将键盘输入的数据转换为String类型并返回。


String类(java.lang包)

概述:字符串是是由多个字符数组组成的一串数据,且字符串是常量,赋值后不能更改。

构造方法:

public String();

     空构造。

public Stirng(byte[] bytes);

     把字节数组转成字符串。

public Stirng(byte[] bytes,int offset,int length);

     把字节数组的一部分(从offset开始length个字节)转成字符串。

public Stirng(char[] value);

     把字符数组转成字符串。

public Stirng(char[] value,int offset,int count);

     把字符数组的一部分(从offset开始count个字节)转成字符串。

成员方法:

判断功能的方法:

public boolean equals(Object obj);

     比较字符串内容是否相同,区分大小写。

public boolean equalsIgnoreCase(String str);

     比较字符串内容是否相同,不区分大小写。

public boolean contains(String str);

     判断大字符串中是否包含小字符串。

public boolean startsWith (String str);

     判断字符串是否以指定的小字符串开头。

public boolean endsWith (String str);

     判断字符串是否以指定的小字符串结尾。

public boolean isEmpty();

     判断字符串是否为空。

获取功能的方法:

public int length();

     获取字符串长度。

public char charAt(int index);

     获取指定索引位置的字符。

public int indexOf(int ch);

     返回指定字符在此字符串中第一次出现处的索引。

public int indexOf(String str);

     返回指定字符串在此字符串中第一次出现处的索引。

public int indexOf(int ch,int fromIndex);

     返回指定字符在此字符串中从指定位置往后第一次出现处的索引。

public int indexOf(String str,int fromIndex);

     返回指定字符串在此字符串中从指定位置往后第一次出现处的索引。

public String substring(int start);

     从指定位置开始截取字符串,默认到末尾。

public String sbustring(int start,int end);

     从指定位置开始至指定位置结束截取字符串。

转换功能的方法:

public byte[] getBytes();

     把字符串转换为字节数组。

public char[] toCharArray();

     把字符串转换为字符数组。

public static String valueOf(char[] chs);

     把字节数组转换为字符串。

public static String valueOf(int i);

     把int类型数据转换为字符串。

     String类的valueOf方法可以把任意类型的数据转换为字符串。

public String toLowerCase();

     把字符串转成小写。

public String toUpperCase();

     把字符串转成大写。

public concat(String str);

     拼接字符串。

其它功能的方法:

public String replace(char old,char new);

     将字符串中的所有old字符替换为new字符。

public String replace(String old,String new);

     将字符串中的所有old字符串替换为new字符串。

public String trim();

     去除字符串两端的空格。

public int compareTo(String str);

     按字典顺序比较两个字符串。

public int compareToIgnoreCase(String str);

     按字典顺序比较两个字符串,不区分大小写。


StringBuffer类(java.lang包)

概述:线程安全的可变字符序列。

构造方法:

public StringBuffer();

     不带字符的字符串缓冲区,其初始容量为 16 个字符。

public StringBuffer(int capacity);

     创建一个指定容量的字符串缓冲区。

public StringBuffer(String str);

     创建一个指定字符串的字符串缓冲区。

成员方法:

添加功能的方法:

public StringBuffer append(String str);

     把任意类型的数据添加到字符串缓冲区中,并返回该缓冲区本身。

public StringBuffer insert(int offset,String str);

     在指定位置将任意类型的数据添加到字符串缓冲区中。

删除功能的方法:

public StringBuffer deleteCharAt(int index);

     删除指定位置的字符。

public StringBuffer delete(int start,int end);

     删除从指定位置开始至指定位置结束的内容。

替换功能的方法:

public StringBuffer replace(int start,int end,String str);

     将从start开始至end中的内容替换为str。

反转功能的方法:

public StringBuffer reverce();

     将字符串缓冲区中的内容反转。

截取功能的方法:

public String substring(int start);

     截取字符串缓冲区里从start开始至末尾的内容,以String类型返回。

public String substring(int start,int end);

     截取字符串缓冲区里从start开始至end的内容,以String类型返回。

其它功能:

public String toString();

     将字符串缓冲区对象转换为一个String对象。(任何引用类型调用toString()方法都可以将其转换为String类型)


StringBuilder类(java.lang包)

概述:线程不安全的可变字符序列,该类的方法与StringBuffer的兼容。


冒泡排序

数组中相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处,重复多次后,元素即从小到大排列完毕。

下面是用冒泡排序将数组从小到大排序演示,

<span style="font-size:14px;">public class sortDemo {
	public static void main(String[] args) {
		int[] arr = { 33, 55, 22, 66, 11, 44 };
		System.out.println("数组排序前:");
		show(arr);
		System.out.println("数组从小到大排序后:");
		sort(arr);
	}
	
	public static void sort(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+1];
					arr[y+1] = arr[y];
					arr[y] = temp;
				}
			}
		}
		show(arr);
	}
	
	public static void show(int[] arr) {
		System.out.print("[");
		for(int x = 0; x<arr.length; x++) {
			if(x== arr.length-1) {
				System.out.print(arr[x]);
			}else {
				System.out.print(arr[x] + ", ");
			}
		}
		System.out.println("]");
	}
}</span>


选择排序

将数组的0索引元素依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处, 重复多次后,元素即从小到大排列完毕。

下面是用选择排序将数组从小到大排序演示,

<span style="font-size:14px;">public class sortDemo2 {
	public static void main(String[] args) {
		int[] arr = { 33, 55, 22, 66, 11, 44 };
		System.out.println("数组排序前:");
		show(arr);
		System.out.println("数组从小到大排序后:");
		sort(arr);
	}
	
	public static void sort(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;
				}
			}
		}
		show(arr);
	}
	
	public static void show(int[] arr) {
		System.out.print("[");
		for(int x = 0; x<arr.length; x++) {
			if(x== arr.length-1) {
				System.out.print(arr[x]);
			}else {
				System.out.print(arr[x] + ", ");
			}
		}
		System.out.println("]");
	}
}</span>

查找的两种基本方法:

基本查找:当数组元素无序时,依次遍历元素查找。

二分查找:当数组元素有序时,先拿数组正中间的元素和目标值比较,判断目标元素在数组中的位置,然后再次循环操作,找出目标元素的索引。


Arrays类(java.util包)

概述:针对数组进行操作的工具类。

public static String toString(int[] a);

     把数组转换成字符串。

public static void sort(int[] a);

     对数组从小到大排序。

public static int binarySearch(int[] a,int key);

     二分查找。


Integer类(java.lang包)

概述:该类在对象中包装了一个基本类型int的值,它是int类的包装类类型。

成员变量:

public static final int MIN_VALUE;

public static final int MAX_VALUE;

     表示int类型能够表示的最大值。

构造方法:

public Integer(int value);

     构造一个Integer对象,并存储value的值。

public Integer(String s);

     构造一个Integer对象,并存储s的值。(该字符串必须由数字字符组成)

成员方法

转换数据类型的方法:

public String toString();

     返回该Integer值的String对象。

public static String toString(int i);

     将int类型转换为String类型返回。

public int intValue();

     以int类型返回该Integer类型的值。

public static Integer valueOf(int i);

     将int类型转换为Integer类型。

public static int parseInt(String s);

     将String类型转换为int类型。

进制转换的方法:

public static String toBianryString(int i);

     将int类型的值转换为二进制无符号整数,并以字符串形式返回。

public static String toOctalString(int i);

     将int类型的值转换为八进制无符号整数,并以字符串形式返回。

public static String toHexString(int i);

     将int类型的值转换为十六进制无符号整数,并以字符串形式返回。

public static String toString(int i,int radix);

     将int类型的十进制值转换为radix进制,并以字符串形式返回。(进制范围为2~36)

public static int parseInt(String s,int radix);

     将String类型的radix进制数据转换为十进制,以int类型返回。

JDK5的新特性之自动装箱

自动装箱:把基本类型转换为包装类类型。

自动拆箱:把包装类类型转换为基本类型。


Character类(java.lang包)

概述:该类在对象中包装了一个基本类型char的值,它是char类的包装类类型。

构造方法

public Character(char value);

     构造一个Character对象,并存储value的值。

成员方法

public static boolean isUpperCase(char ch);

     判断给定的字符是否是大写字符。

public static boolean isLowerCase(char ch);

     判断给定的字符是否是小写字符。

public static boolean isDigit(char ch);

     判断给定的字符是否是数字字符。

public static char toUpperCase(char ch);

     把给定的字符转换为大写。

public static char toLowerCase(char ch);

     把给定的字符转换为小写。


正则表达式

概述:符合一定规则的字符串,该字符串是用来匹配某个句法规则的一串字符,该规则在Pattern类中。

正则表达式常用规则:

<span style="font-size:14px;">/*
A:字符
	x   字符x
	\\   反斜线字符\
	\n   新行(换行符)
	\r   回车符
B:字符类
	[abc]   a、b或c
	[^abc]   任何字符,除a、b或c
	[a-zA-Z]   a到z,或A到Z
	[0-9]   数字0到9
C:预定义字符
	.   任何字符
	\d   数字:[0-9]
	\w   单词字符:[a-zA-Z_0-9]
D:边界匹配器
	^   行的开头
	$   行的开头
	\b   单词边界
E:数量词
	X?   X,一次或一次也没有
	X*   X,零次或多次
	X+   X,一次或多次
	X{n}   X,恰好 n次
	X{n,}   X,至少 n次
	X{n,m}   X,至少 n次,但是不超过 m次
 */</span>

String类中有关正则表达式的方法:

public boolean matches(String regex);

     判定字符串是否匹配给定的正则表达式。

public String[] splet(Stirng regex);

     根据给定正则表达式的匹配拆分此字符串。

public String replaceAll(String regex,String replacement);

     根据给定的正则表达式将原字符串中符合要求的子字符串替换。

指定为字符串的正则表达式必须首先被编译为Pattern类的实例。然后,可将得到的模式用于创建 Matcher 对象,依照正则表达式,该对象可以与任意字符序列匹配,格式如下:

<span style="font-size:14px;">	Pattern p = Pattern.compile("a*b");
	Matcher m = p.matcher("aaaaab");
	boolean b = m.matches();</span>

Pattern类(java.util.regex包)

概述:正则表达式的编译表示形式。

成员方法:

public static Pattern compile(String regex);

     将给定的正则表达式编译到模式中。

pulbic Matcher matcher(CharSequence input);

     创建匹配给定输入与此模式的匹配器。

Matcher类(java.util.regex包)

概述:通过解释Pattern对象对CharSequence 的实现类对象执行匹配操作的匹配器。

成员方法:

public boolean matches();

     尝试将整个区域与模式匹配。

public boolean find();

     尝试查找与该模式匹配的输入序列的下一个子序列。

public String group();

     返回由以前匹配操作所匹配的输入子序列。(使用此方法前,对象应先调用find()方法)


Math类(java.lang包)

概述:用于数学运算的类。

成员变量:

public static final double E;

     一个double类型的值e。(自然对数的底数)

public static final double PI;

     一个double类型的值π。

成员方法:

public static int abs (int a);

     求绝对值。

public static double ceil(double a);

     比a大的最小整数。(向上取整)

public static double floor(double a);

     比a小的最大整数。(向下取整)

public static int max(int a,int b);

     返回两数的最大值。

public static double pow(double a,double b);

     a的b次幂。

public static double random();

     返回随机数[0.0,1.0)。

public static int round(float a);

     对a四舍五入。

public static double sqrt(double a);

     返回a的正平方根。


Random类(java.util包)

概述:用来产生随机数的类。

构造方法:

public Random();

     用默认种子(当前毫秒值)构造对象。

public Random(long seed);

     用给定种子构造对象。当种子一定时,每次产生的随机数是固定的。

成员方法:

public int nextInt();

     返回int范围内的随机数。

public int nextInt(int n);

     返回[0,n)范围内的随机数。


System类(Java.lang包)

概述:该类包含一些有用的类字段和方法,它不能被实例化。

成员方法:

public static void gc();

     运行垃圾回收器。

public static void exit(int status);

     终止当前正在运行的虚拟机。(status值为0是表示正常退出)

public static long currentTimeMillis();

     返回以毫秒为单位的当前时间。

public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length);

     从指定源数组中复制一个子数组,复制从srcPos开始长度为length,覆盖目标数组上从destPos位置开始长度为length的子数组。


BigInteger类(java.math包)

概述:可以让超过Integer范围的数据进行运算。

构造方法:

public BigInteger(String val);

     将 BigInteger 的十进制字符串表示形式转换为 BigInteger。

成员方法:

public BigInteger add(BigInteger val);

     两数相加。

public BigInteger subtract(BigInteger val);

     两数相减。

public BigInteger multiply(BigInteger val);

     两数相乘。

public BigInteger divide(BigInteger val);

     两数相除。

public BigInteger[] dirideAndRemainder(BigInteger val);

     返回两数商和余数组成的数组。


BigDecimal类(java.math包)

概述:不可变的、任意精度的有符号十进制数,float和double类型的数据在运算中容易丢失精度,BigDecimal能有效解决这一问题。

构造方法:

public BigDecimal(String val);

     将BigDecimal的字符串表示形式转换为BigDecimal

public BigDecimal add(BigDecimal val);

     两数相加。(同理该类还具有减法subtract,乘法multiply和除法divide功能)

public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode);

     根据要求做除法。(要求商的小数点后保留scale位有效数字,并设置小数部分的取舍方式,常用取舍方式为ROUND_HALF_UP:四舍五入)


Data类(java.util包)

概述:表示特定的时间,精确到毫秒。

构造方法:

public Date();

     根据当前的默认毫秒值创建日期对象。

public Date(long date);

     根据给定的毫秒值创建日期对象。

成员方法:

public long getTime();

     获取以毫秒为单位的时间。

public void setTime(long time);

     设置时间。


DataFormat类(java.text包)

概述:可以进行日期和字符串的格式化和解析的抽象类,所以不能直接使用,需使用其子类SimpleDateFormat。

成员方法:

public final String format(date date);

     将一个Date对象格式化为字符串。

public Date parse(Stirng Souree) throws ParseException;

     从给定的字符串解析文本,生成一个日期。


SimpleDateFormat类(java.text包)

概述:可以进行日期和字符串的格式化和解析的具体类。

构造方法:

public SimpleDateFormat();

     用默认的模式和日期构造对象。

public SimpleDateFormat(String pattern);

     用给定模式和默认日期构造对象。

日期和时间模式:

常用的时间元素表示

年:y;     月:M;     日:d;

时:H;    分:m;     秒:s;

举例:"yyyy年MM月dd日 HH:mm:ss"即表示常见的年月日时分秒格式。


Calendar类(java.util包)

概述:它为特定瞬间和日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法。 该类为抽象类,且没有构造方法,所以该类的对象可用其静态成员方法创建:

public static Calendar getInstance();

     使用默认时区和语言环境获得一个日历。

常见日历字段:

public static final itn YEAR;   年

public static final int MONTH;   月

public static final int DATE;   日

public static final int HOUR_OF_DAY;   小时(24小时制)

public static final int MINUTE;   分钟

public static final int SECOND;   秒

成员方法:

public int get(int field);

     返回给定日历字段的值。

public abstract void add(int field,int amount);

     根据给定的日历字段和对应时间,来对当前日历进行操作。

public final void set(int year,int month,int day);

     设置当前日历。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值