本周主题:Java中自带的常用接口和类的使用

​一、Java Number & Math 类​

​二、Java 数据结构​

​枚举(Enumeration)​

​位集合(BitSet)​

​向量(Vector)​

​栈(Stack)​

​字典(Dictionary)​

​哈希表(Hashtable)​

​属性(Properties)​


一、Java Number & Math 类

一般地,当我们需要使用数字的时候,我们通常使用Java内置的基本数据类型,如:byte、int、long、double 等。如:

int x = 100;
float f = 123.56f;
double d = 3.14;
byte b = 0x32;

然而,在实际开发过程中,我们经常会遇到需要使用对象,而不是内置数据类型的情形。为了解决这个问题,Java 语言为每一个内置数据类型提供了对应的包装类。

所有的包装类(Integer、Long、Byte、Double、Float、Short)都是抽象类 Number 的子类。

第十周Java作业_2d

 1、Number & Math 类常用方法

第十周Java作业_i++_02

2、Math 的 floor,round 和 ceil 方法实例比较:

第十周Java作业_i++_03

3、举个Numbers和Math用法的例子:

/**
* 文件名:NumbersDemo.java
* 功能描述:Numbers和Math类操作Demo
*/
public class NumbersDemo {
public static void main(String[] args) {

double dVer = 11.5;
float fVer = -11.5f;

System.out.printf("dVer=%.2f,fVer=%.2f\n",dVer,fVer);

//ceil()返回>=给定参数的最小整数,返回类型为双精度浮点型
System.out.printf("Math.ceil(%.2f)=%.2f,",dVer,Math.ceil(dVer));
System.out.printf("Math.ceil(%.2f)=%.2f\n",fVer,Math.ceil(fVer));

//floor()返回<=给定参数的最大整数,返回类型为双精度浮点型
System.out.printf("Math.floor(%.2f)=%.2f,",dVer,Math.floor(dVer));
System.out.printf("Math.floor(%.2f)=%.2f\n",fVer,Math.floor(fVer));

// round() 表示"四舍五入",算法为Math.floor(x+0.5) ,即将原来的数字加上 0.5 后再向下取整,
// 所以 Math.round(11.5) 的结果为 12,Math.round(-11.5) 的结果为 -11。
System.out.printf("Math.round(%.2f)=%d,",dVer,Math.round(dVer));
System.out.printf("Math.round(%.2f)=%d\n",fVer,Math.round(fVer));

//ceil()返回与参数最接近的整数,返回类型为双精度浮点型
System.out.printf("Math.rint(%.2f)=%.2f,",dVer,Math.rint(dVer));
System.out.printf("Math.rint(%.2f)=%.2f\n",fVer,Math.rint(fVer));

//parseXXX():将字符串解析为XXX的基本数据类型
dVer = Double.parseDouble("11.5"); //parseDouble() 返回一个基本数据类型
Double dObj = Double.valueOf("11.5"); //valueOf() 返回一个基本数据类型的包装类对象

System.out.println("dVer="+dVer+",dObj="+dObj.toString());

int iVer = (int)Double.parseDouble("11.5");
Integer iObj = Double.valueOf("-11.5").intValue();
System.out.println("iVer="+iVer+",iObj="+iObj.toString());

System.out.println("dObj.compareTo(dVer):"+dObj.compareTo(dVer));
System.out.println("dObj.equals(dVer):"+dObj.equals(dVer));

System.out.println("Math.pow(3,2):"+Math.pow(3,2));

System.out.println("输出5个1~100之间的随机数:");
for (int i = 0; i < 5; i++) {
System.out.print((Math.round(Math.random()*100))+" ");
}
}
}

运行结果: 

   

第十周Java作业_i++_04

二、Java 数据结构

Java工具包提供了强大的数据结构。在Java中的数据结构主要包括以下几种接口和类:

枚举(Enumeration)

位集合(BitSet)

向量(Vector)

栈(Stack)

字典(Dictionary)

哈希表(Hashtable)

属性(Properties)

以上这些类是传统遗留的,在Java2中引入了一种新的框架--集合框架(Collection),我们后面再讨论。

对这些Java经常要使用的接口和类,举几个简单的例子加以说明:

1、枚举(Enumeration)和向量(Vector)操作Demo:

import java.util.Enumeration;
import java.util.Vector;

/**
* 文件名:EnumerationDemo.java
* 功能描述:枚举接口用法Demo
*/
public class EnumerationDemo {
public static void main(String[] args) {
Enumeration<String> weekDays; //声明一个枚举类对象
Vector<String> weekDayNames = new Vector<String>(); //Vector:向量类,类似于一个可变数组

weekDayNames.add("星期一");
weekDayNames.add("星期二");
weekDayNames.add("星期三");
weekDayNames.add("星期四");
weekDayNames.add("星期五");
weekDayNames.add("星期六");
weekDayNames.add("星期日");

weekDays = weekDayNames.elements(); //取出向量对象中的所有元素,放置到枚举对象中

while(weekDays.hasMoreElements()) {
System.out.println(weekDays.nextElement());
}
}

}

2、位集合(BitSet)操作Demo:

import com.sun.org.apache.xalan.internal.xsltc.dom.BitArray;

import java.util.Arrays;
import java.util.BitSet;

/**
* 文件名:BitSetDemo.java
* 功能描述:
*/
public class BitSetDemo {
public static void main(String[] args) {
BitSet bits1 = new BitSet(16);
BitSet bits2 = new BitSet(16);

for (int i = 0; i < 16; i++) {
if(i%2 == 0)
bits1.set(i);
if(i%5 == 0)
bits2.set(i);
}

System.out.printf("bits1中二进制位内容为1的索引:%s\n",bits1.toString());
System.out.printf("bits2中二进制位内容为1的索引:%s\n",bits2.toString());

System.out.print("bits1:");
for (int i = 0; i < 16; i++) {
if (bits1.get(i))
System.out.printf("%2d",1);
else
System.out.printf("%2d",0);
}

System.out.print("\nbits2:");
for (int i = 0; i < 16; i++) {
if (bits2.get(i))
System.out.printf("%2d",1);
else
System.out.printf("%2d",0);
}


bits1.and(bits2);
System.out.printf("\nbits1和bits2进行[与]运算后:%s\n",bits1);
System.out.print("bits1:");
for (int i = 0; i < 16; i++) {
if (bits1.get(i))
System.out.printf("%2d",1);
else
System.out.printf("%2d",0);
}

bits1.or(bits2);
System.out.printf("\nbits1和bits2进行[或]运算后:%s\n",bits1);
System.out.print("bits1:");
for (int i = 0; i < 16; i++) {
if (bits1.get(i))
System.out.printf("%2d",1);
else
System.out.printf("%2d",0);
}

bits1.xor(bits2);
System.out.printf("\nbits1和bits2进行[异或]运算后:%s\n",bits1);
System.out.print("bits1:");
for (int i = 0; i < 16; i++) {
if (bits1.get(i))
System.out.printf("%2d",1);
else
System.out.printf("%2d",0);
}
}
}

3、栈(Stack)操作Demo:

import java.util.EmptyStackException;
import java.util.Stack;

/**
* 文件名:StackDemo.java
* 功能描述:Stack类使用Demo
*/
public class StackDemo {
static void showPush(Stack<Integer> st, int v) {
st.push(new Integer(v));//压入一个数据至栈中:入栈
System.out.print("执行:push("+v+")后,");
System.out.println("stack:"+st);
}

static void showPop(Stack<Integer> st) {
int v = (Integer) st.pop(); //从栈中取数:出栈
System.out.print("执行:pop() -> 取出 "+v+" 后,");
System.out.println("stack:"+st);
}

public static void main(String[] args) {
Stack<Integer> st = new Stack<Integer>();
System.out.println("stack:" + st);
showPush(st,10);
showPush(st,20);
showPush(st,30);

if (!st.isEmpty())
System.out.println("st.peek() -> 取出栈顶内容:"+st.peek());
int x = st.search(20);
System.out.println("st.search(20) -> 查询20在栈中的位置为:"+x);

showPop(st);
showPop(st);
showPop(st);

try {
showPop(st); //之前已所数据取完了,此语句会抛出一个“空栈”异常
} catch (EmptyStackException e) {
System.out.println("捕获到一个对空栈进行访问的异常!");
}
}
}

4、字典(Dictionary)操作Demo:

Dictionary 类是一个抽象类,用来存储键/值对,作用和Map类相似。Dictionary类已经过时了。在实际开发中,你可以​​实现Map接口​​来获取键/值的存储功能。

给出键和值,你就可以将值存储在Dictionary对象中。一旦该值被存储,就可以通过它的键来获取它。所以和Map一样, Dictionary 也可以作为一个键/值对列表。

Dictionary定义的抽象方法如下表所示:

第十周Java作业_2d_05

5、哈希表(Hashtable)操作Demo:

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;

/**
* 文件名:HashTableDemo.java
* 功能描述:哈希表测试Demo
*/
public class HashTableDemo {
public static void main(String[] args) {
Hashtable balance = new Hashtable();
balance.put("张三",new Double(100.00));
balance.put("李四",new Double(200.00));
balance.put("王五",new Double(300.00));
balance.put("钱六",new Double(400.00));
balance.put("孙七",new Double(500.00));

Enumeration names;
String str;

names = balance.keys();
while(names.hasMoreElements()) {
str = names.nextElement().toString();
System.out.println(str + " : " + balance.get(str));
}

Scanner input = new Scanner(System.in);
System.out.print("请输入要查找的帐号:");
String key = input.nextLine();

if (balance.containsKey(key)) {
double x = (Double)balance.get(key);
System.out.println("变动之前的余额为:");
System.out.println(x);

balance.put(key,x+500);

System.out.println("增加500之后的余额为:");
System.out.println(balance.get(key));
}
else {
System.out.println("key为:"+key+" 的帐号未找到!");
}

input.close();
}
}

6、属性(Properties)操作Demo:

Properties 继承于 Hashtable,Properties 类表示了一个持久的属性集。属性列表中每个键及其对应值都是一个字符串。Properties 类被许多Java类使用。例如,在获取环境变量时它就作为System.getProperties()方法的返回值。

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
* 文件名:PropertiesDemo.java
* 功能描述:
*/
public class PropertiesDemo {
public static void main(String[] args) {
Properties dbSrvConnInfo = new Properties();
dbSrvConnInfo.put("SrvIp","172.18.4.13");
dbSrvConnInfo.put("dbType","MySQL");
dbSrvConnInfo.put("dbName","student");
dbSrvConnInfo.put("loginName","root");
dbSrvConnInfo.put("loginPwd","abc@123");

System.out.println("使用Iterator读取信息:");

Set set = dbSrvConnInfo.keySet();

Iterator iterator = set.iterator();

while(iterator.hasNext()) {
String key = iterator.next().toString();
String value = dbSrvConnInfo.get(key).toString();

System.out.println(key+" : "+value);
}

System.out.println("---------------------------------------");
System.out.println("使用Enumeration读取信息:");

Enumeration keys = dbSrvConnInfo.keys();

while (keys.hasMoreElements()) {
String key = keys.nextElement().toString();
System.out.println(key+":"+dbSrvConnInfo.get(key));
}

}
}

三、演示DEMO源代码在github上的仓库地址:

​https://github.com/xieyunc/java_demo.git​