API补习之java.util包

来源:微学计算机考试发布时间:2008-07-17

  1.Date

  view plaincopy to clipboardprint?

  import java.util.Date;

  public class DateTimeDisplay {

  Date objDate;

  DateTimeDisplay() {

  objDate = new Date();

  }

  void display() {

  String strDate, strTime = "";

  System.out.println("今天的日期是:" + objDate);

  long time = objDate.getTime();

  System.out.println("自1970年1月1日起以毫秒为单位的时间(GMT):" + time);

  strDate = objDate.toString();

  strTime = strDate.substring(11, (strDate.length() - 4));

  strTime = "时间:" + strTime.substring(0, 8);

  System.out.println(strTime);

  }

  }

  public class DateTest2 {

  public DateTest2() {

  }

  public static void main(String[] args) {

  DateTimeDisplay objDateTime=new DateTimeDisplay();

  objDateTime.display();

  }

  }

  import java.util.Date;

  public class DateTimeDisplay {

  Date objDate;

  DateTimeDisplay() {

  objDate = new Date();

  }

  void display() {

  String strDate, strTime = "";

  System.out.println("今天的日期是:" + objDate);

  long time = objDate.getTime();

  System.out.println("自1970年1月1日起以毫秒为单位的时间(GMT):" + time);

  strDate = objDate.toString();

  strTime = strDate.substring(11, (strDate.length() - 4));

  strTime = "时间:" + strTime.substring(0, 8);

  System.out.println(strTime);

  }

  }

  public class DateTest2 {

  public DateTest2() {

  }

  public static void main(String[] args) {

  DateTimeDisplay objDateTime=new DateTimeDisplay();

  objDateTime.display();

  }

  }

  输出: 今天的日期是:Fri Jul 04 15:02:51 CST 2008 自1970年1月1日起以毫秒为单位的时间(GMT):1215154971375 时间:15:02:51

  例子2 格式化时间

  view plaincopy to clipboardprint?

  package my;

  import java.util.Date;

  import java.util.Locale;

  import java.text.DateFormat;

  class DateTime {

  Date objDate;

  DateTime() {

  objDate = new Date();

  }

  void convert() {

  DateFormat simpleFormat =DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINESE);

  String strDate = objDate.toString();

  System.out.println("当前日期: " + strDate);

  System.out.println("\n格式化后: "

  + simpleFormat.format(objDate));

  }

  }

  class DateTimeTest {

  protected DateTimeTest() {

  }

  public static void main(String[] args) {

  DateTime objDateTime = new DateTime();

  objDateTime.convert();

  }

  }

  package my;

  import java.util.Date;

  import java.util.Locale;

  import java.text.DateFormat;

  class DateTime {

  Date objDate;

  DateTime() {

  objDate = new Date();

  }

  void convert() {

  DateFormat simpleFormat =DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINESE);

  String strDate = objDate.toString();

  System.out.println("当前日期: " + strDate);

  System.out.println("\n格式化后: "

  + simpleFormat.format(objDate));

  }

  }

  class DateTimeTest {

  protected DateTimeTest() {

  }

  public static void main(String[] args) {

  DateTime objDateTime = new DateTime();

  objDateTime.convert();

  }

  }

  2 Calendar类是抽象类,无法像Date一样实例化 objCalendar=Calendar.getInstance();

  3 Random类 new Random().nextFloat(); 再生成一个0.0到1.0之间的随机数或者用Math.random()来产生0.0到1.0之间的随机数 4.ArrayList类

  view plaincopy to clipboardprint?

  import java.util.ArrayList;

  import java.util.Random;

  import java.util.List;

  import java.util.Collections;

  class PlayersList {

  ArrayList playerArray;

  List subListObj;

  List otherListObj;

  PlayersList() {

  playerArray = new ArrayList();

  subListObj = new ArrayList();

  }

  void add() {

  for (int ctr = 0; ctr < 5; ctr++) {

  playerArray.add(new Integer(ctr));

  }

  playerArray.add("Martina");

  playerArray.add("Serena");

  playerArray.add("Venus");

  playerArray.add("Serena");

  System.out.println(playerArray);

  }

  void display() {

  for (int ctr = 0; ctr < playerArray.size(); ctr++) {

  System.out.print(" " + playerArray.get(ctr));

  }

  System.out.println();

  }

  void search() {

  System.out.println("字符串 Serena 第一次出现在位置 "+playerArray.indexOf("Serena"));

  System.out.println("字符串 Serena 第一次出现在位置 "+playerArray.lastIndexOf("Serena"));

  }

  /**

  * 从 ArrayList 提取一个子列表

  */

  void extract() {

  subListObj = playerArray.subList(5, playerArray.size());

  System.out.println("索引 5 至" + playerArray.size() + "的新子列表为: " + subListObj);

  }

  /**

  * 随机更改顺序

  */

  void shuffle() {

  System.out.println("播放器列表 (之前): " + playerArray);

  Collections.shuffle(playerArray, new Random());

  System.out.println("播放器列表 (之后): " + playerArray);

  }

  /**

  * 对 ArrayList 进行排序.

  */

  void sort() {

  System.out.println("排序前: " + playerArray);

  Collections.sort(bookArray);

  System.out.println("排序后: " + playerArray);

  }

  /**

  * 反转 ArrayList.

  */

  void reverse() {

  System.out.println("反转前: " + playerArray));

  Collections.reverse(bookArray);

  System.out.println("反转后: " + playerArray));

  }

  void copy() {

  System.out.println("playerArray 是否为空? " + playerArray.isEmpty());

  System.out.println("playerArray (之前): " + playerArray);

  bookCopyObj = new ArrayList(bookArray);

  System.out.println("otherListObj (之后): " + otherListObj);

  }

  }

  class PlayerListTest {

  public static void main(String[] args) {

  PlayersList playerObj = new PlayersList();

  playerObj.add();

  playerObj.display();

  playerObj.search();

  playerObj.extract();

  playerObj.shuffle();

  playerObj.sort();

  playerObj.reverse();

  playerObj.copy();

  }

  }

  import java.util.ArrayList;

  import java.util.Random;

  import java.util.List;

  import java.util.Collections;

  class PlayersList {

  ArrayList playerArray;

  List subListObj;

  List otherListObj;

  PlayersList() {

  playerArray = new ArrayList();

  subListObj = new ArrayList();

  }

  void add() {

  for (int ctr = 0; ctr < 5; ctr++) {

  playerArray.add(new Integer(ctr));

  }

  playerArray.add("Martina");

  playerArray.add("Serena");

  playerArray.add("Venus");

  playerArray.add("Serena");

  System.out.println(playerArray);

  }

  void display() {

  for (int ctr = 0; ctr < playerArray.size(); ctr++) {

  System.out.print(" " + playerArray.get(ctr));

  }

  System.out.println();

  }

  void search() {

  System.out.println("字符串 Serena 第一次出现在位置 "+playerArray.indexOf("Serena"));

  System.out.println("字符串 Serena 第一次出现在位置 "+playerArray.lastIndexOf("Serena"));

  }

  /**

  * 从 ArrayList 提取一个子列表

  */

  void extract() {

  subListObj = playerArray.subList(5, playerArray.size());

  System.out.println("索引 5 至" + playerArray.size() + "的新子列表为: " + subListObj);

  }

  /**

  * 随机更改顺序

  */

  void shuffle() {

  System.out.println("播放器列表 (之前): " + playerArray);

  Collections.shuffle(playerArray, new Random());

  System.out.println("播放器列表 (之后): " + playerArray);

  }

  /**

  * 对 ArrayList 进行排序.

  */

  void sort() {

  System.out.println("排序前: " + playerArray);

  Collections.sort(bookArray);

  System.out.println("排序后: " + playerArray);

  }

  /**

  * 反转 ArrayList.

  */

  void reverse() {

  System.out.println("反转前: " + playerArray));

  Collections.reverse(bookArray);

  System.out.println("反转后: " + playerArray));

  }

  void copy() {

  System.out.println("playerArray 是否为空? " + playerArray.isEmpty());

  System.out.println("playerArray (之前): " + playerArray);

  bookCopyObj = new ArrayList(bookArray);

  System.out.println("otherListObj (之后): " + otherListObj);

  }

  }

  class PlayerListTest {

  public static void main(String[] args) {

  PlayersList playerObj = new PlayersList();

  playerObj.add();

  playerObj.display();

  playerObj.search();

  playerObj.extract();

  playerObj.shuffle();

  playerObj.sort();

  playerObj.reverse();

  playerObj.copy();

  }

  }

  4.2LinkedList类 方法addLast(); removeFirst(); getFirst();获得队列第一个元素.

  输出: [dog, dog, cat, aat, Aat, b4] [dog, cat, b4, Aat, aat] 是随机的顺序

  4.3 HashMap类 键值对照表

  view plaincopy to clipboardprint?

  import java.util.*;

  public class PrintingContainers {

  static Map fill(Map m) {

  m.put("dog2", "Bosco"); //dog2是key,Bosco是value 用Map.Entry来取键值

  m.put("dog", "Spot");

  m.put("cat", "Rags");

  return m;

  }

  public static void main(String[] args) {

  System.out.println(fill(new HashMap()));

  }

  }

  import java.util.*;

  public class PrintingContainers {

  static Map fill(Map m) {

  m.put("dog2", "Bosco"); //dog2是key,Bosco是value 用Map.Entry来取键值

  m.put("dog", "Spot");

  m.put("cat", "Rags");

  return m;

  }

  public static void main(String[] args) {

  System.out.println(fill(new HashMap()));

  }

  }

  输出: {dog2=Bosco, dog=Spot, cat=Rags}

  5. Vector类

  view plaincopy to clipboardprint?

  import java.util.Vector;

  class PreciousStones {

  Vector preciousVector;

  PreciousStones() {

  preciousVector = new Vector();

  }

  void add() {

  preciousVector.addElement("翡翠");

  preciousVector.addElement("黄晶");

  preciousVector.addElement("绿宝石");

  preciousVector.addElement("祖母绿");

  }

  void insert() {

  preciousVector.insertElementAt("钻石" , 0);

  preciousVector.insertElementAt("猫眼石" , 4);

  }

  void display(String title) {

  int count = 0;

  while (count < preciousVector.size()) {

  System.out.print(preciousVector.elementAt(count));

  count++;

  if (count < preciousVector.size()) {

  System.out.print(", ");

  }

  }

  }

  void search() {

  if (preciousVector.contains("钻石")) {

  System.out.println("在索引 " + preciousVector.indexOf("钻石") + " 中找到钻石");

  }

  }

  void remove() {

  preciousVector.removeElement("黄晶");

  System.out.println("删除黄晶后的内容: ");

  int count = 0;

  while (count < preciousVector.size()) {

  System.out.print(preciousVector.elementAt(count));

  count++;

  if (count < preciousVector.size()) {

  System.out.print(", ");

  }

  }

  }

  void otherDetails() {

  System.out.println("\n第一个元素 = " + preciousVector.firstElement());

  System.out.println("默认容量 = " + preciousVector.capacity());

  }

  }

  class PreciousStoneTest {

  public static void main(String [] args) {

  PreciousStones objPrecious = new PreciousStones();

  objPrecious.add();

  objPrecious.display("显示内容");

  objPrecious.insert();

  objPrecious.display("显示插入后的内容");

  objPrecious.search();

  objPrecious.remove();

  objPrecious.otherDetails();

  }

  }

  import java.util.Vector;

  class PreciousStones {

  Vector preciousVector;

  PreciousStones() {

  preciousVector = new Vector();

  }

  void add() {

  preciousVector.addElement("翡翠");

  preciousVector.addElement("黄晶");

  preciousVector.addElement("绿宝石");

  preciousVector.addElement("祖母绿");

  }

  void insert() {

  preciousVector.insertElementAt("钻石" , 0);

  preciousVector.insertElementAt("猫眼石" , 4);

  }

  void display(String title) {

  int count = 0;

  while (count < preciousVector.size()) {

  System.out.print(preciousVector.elementAt(count));

  count++;

  if (count < preciousVector.size()) {

  System.out.print(", ");

  }

  }

  }

  void search() {

  if (preciousVector.contains("钻石")) {

  System.out.println("在索引 " + preciousVector.indexOf("钻石") + " 中找到钻石");

  }

  }

  void remove() {

  preciousVector.removeElement("黄晶");

  System.out.println("删除黄晶后的内容: ");

  int count = 0;

  while (count < preciousVector.size()) {

  System.out.print(preciousVector.elementAt(count));

  count++;

  if (count < preciousVector.size()) {

  System.out.print(", ");

  }

  }

  }

  void otherDetails() {

  System.out.println("\n第一个元素 = " + preciousVector.firstElement());

  System.out.println("默认容量 = " + preciousVector.capacity());

  }

  }

  class PreciousStoneTest {

  public static void main(String [] args) {

  PreciousStones objPrecious = new PreciousStones();

  objPrecious.add();

  objPrecious.display("显示内容");

  objPrecious.insert();

  objPrecious.display("显示插入后的内容");

  objPrecious.search();

  objPrecious.remove();

  objPrecious.otherDetails();

  }

  }

  6. Iterator类 hasNext() next() remove()

  view plaincopy to clipboardprint?

  list和set的区别比较的例子

  import java.util.*;

  public class PrintingContainers {

  static Collection fill(Collection c) {

  c.add("dog");

  c.add("dog");

  c.add("cat");

  c.add("aat");

  c.add("Aat");

  c.add("b4");

  return c;

  }

  public static void main(String[] args) {

  //List以特定顺序存储一组元素

  //set元素是不能重复的

  System.out.println(fill(new ArrayList()));

  System.out.println(fill(new HashSet()));

  }

  }

  list和set的区别比较的例子

  import java.util.*;

  public class PrintingContainers {

  static Collection fill(Collection c) {

  c.add("dog");

  c.add("dog");

  c.add("cat");

  c.add("aat");

  c.add("Aat");

  c.add("b4");

  return c;

  }

  public static void main(String[] args) {

  //List以特定顺序存储一组元素

  //set元素是不能重复的

  System.out.println(fill(new ArrayList()));

  System.out.println(fill(new HashSet()));

  }

  }