美文网首页
Java核心API之lang包与util包

Java核心API之lang包与util包

作者: c4a1d989518e | 来源:发表于2017-06-20 22:54 被阅读87次

Java本身自带的API就十分强大,自身就已经携带了各种类。直接调用就好了。Java的各种类库,比如这张图


其中applet是浏览器中的小程序。就其他经常用到的,整理下用法。
首先lang包是不用import的。使用其他包就需要引入啦。

java.lang用法

java.lang 包中的 Integer 类、Long 类和 Short 类都是 Number 的子类.

Integer构造方法有两种:

  • 以 int 型变量作为参数创建 Integer 对象。例如Integer a = new Integer(10);

  • 以 String 型变量作为参数创建 Integer 对象,例如Integer a = new Integer("10")

Character 类在对象中包装一个基本类型 char 的值。

Boolean类的构造方法也有两个:

  • 创建一个表示 value 参数的 Boolean 对象,如Boolean b = new Boolean(true)
  • 如果 String 参数不为 null 且在忽略大小写时等于 "true",创建一个表示 true 值的 Boolean 对象,如Boolean b = new Boolean("ok"),为 true

比较是否相同

System.out.println(a.equals(b));

那如果我想忽略掉大小写关系,比如:java 和 Java 是一样的,我们怎么办呢?我们可以调用equalsIgnoreCase()

字符串连接

String s0 = new String("Hello ");
String s1 = "World" + "!";   //+号连接
String s2 = s0.concat(s1); //concat()方法连接

字符串常用的提取方法

public class StringTest {
    public static void main(String[] args) {
         String s = "abcdefabc";
         System.out.println("字符a第一次出现的位置为"+s.indexOf('a'));
         System.out.println("字符串bc第一次出现的位置为"+s.indexOf("bc"));
         System.out.println("字符a最后一次出现的位置为"+s.lastIndexOf('a'));
         System.out.println("从位置3开始到结束的字符串"+s.substring(3));
         System.out.println("从位置3开始到6之间的字符串"+s.substring(3,6));
    }
}

java.lang中的数学操作

        System.out.println(Math.abs(-12.7));
        System.out.println(Math.ceil(12.7));
        System.out.println(Math.rint(12.4));
        System.out.println(Math.random());

java.util包

util包中包含很多重要的类,比如系统时间的Date,Calendar类还有生成随机数random类。

Date类

Date有些方法已经过时,慎用,可以用Calendar代替。

import java.util.*;
public class DateDemo {
    public static void main(String[] args) throws ClassNotFoundException {
        String strDate, strTime = ""; 
        Date objDate = new Date();
        System.out.println("今天的日期是:" + objDate);
        long time = objDate.getTime();
        System.out.println("自1970年1月1日起以毫秒为单位的时间(GMT):" + time);
        strDate = objDate.toString();
        //提取 GMT 时间
        strTime = strDate.substring(11,(strDate.length() - 4));
        //按小时、分钟和秒提取时间
        strTime = "时间:" + strTime.substring(0,8);
        System.out.println(strTime);
    }
}

得到的结果为

Calendar用法

import java.util.*;
import java.text.*;

public class CalendarDemo  
{    
  public static void main(String[] args)  
  {  

    // 得到日期格式对象  
    // Date date = fmt.parse(strDateMake);  

    System.out.println("完整显示日期时间:");
    // 字符串转换日期格式  
    DateFormat fdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    String str =fdate.format(new Date()); 
    System.out.println(str);  

    // 创建 Calendar 对象  
    Calendar calendar = Calendar.getInstance();  
    // 初始化 Calendar 对象,但并不必要,除非需要重置时间  
    calendar.setTime(new Date());  

    // 显示年份
    System.out.println("年: " + calendar.get(Calendar.YEAR));  

    // 显示月份 (从0开始, 实际显示要加一)    
    System.out.println("月: " + calendar.get(Calendar.MONTH)); 


    // 当前分钟数   
    System.out.println("分钟: " + calendar.get(Calendar.MINUTE));

    // 今年的第 N 天    
    System.out.println("今年的第 " + calendar.get(Calendar.DAY_OF_YEAR) + "天");  

    // 本月第 N 天  
    System.out.println("本月的第 " + calendar.get(Calendar.DAY_OF_MONTH) + "天");  

    // 3小时以后 
    calendar.add(Calendar.HOUR_OF_DAY, 3);    
    System.out.println("三小时以后的时间: " + calendar.getTime());  
    // 格式化显示  
    str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());  
    System.out.println(str);  

    // 重置 Calendar 显示当前时间  
    calendar.setTime(new Date());  
    str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());  
    System.out.println(str); 

    // 创建一个 Calendar 用于比较时间  
    Calendar calendarNew = Calendar.getInstance();  

    // 设定为 5 小时以前,后者大,显示 -1  
    calendarNew.add(Calendar.HOUR, -5);  
    System.out.println("时间比较:" + calendarNew.compareTo(calendar));  

    // 设定7小时以后,前者大,显示 1  
    calendarNew.add(Calendar.HOUR, +7);  
    System.out.println("时间比较:" + calendarNew.compareTo(calendar));  

    // 退回 2 小时,时间相同,显示1  
    calendarNew.add(Calendar.HOUR, -2);  

    // calendarNew创建时间点
    System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendarNew.getTime()));
    // calendar创建时间点
    System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime()));
    System.out.println("时间比较:" + calendarNew.compareTo(calendar));     
  }  
}

运行结果为:


将时间整理格式

DateFormat fdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    String str =fdate.format(new Date()); 
    System.out.println(str);  

也可以直接连用,SimpleDateFormat是在定制格式,format是在转换为模板的格式。

 str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());  
    System.out.println(str); 

Random类

//该方法是产生一个整型随机数
public int nextInt()

Java 集合


List与Set有什么区别?List中的元素可以是重复的,而Set中的元素不能是重复的。举一个List的例子,Set的例子用到的时候再去谷歌。

List 是一个接口,不能实例化,需要一个具体类来实现实例化。List 集合中的对象按照一定的顺序排放,里面的内容可以重复。 List 接口实现的类有:ArrayList(实现动态数组),Vector(实现动态数组),LinkedList(实现链表),Stack(实现堆栈)。


/*
 * 学生类
 */

public class Student {

    public String id;
    public String name;
    public Student(String id, String name){
        this.id = id;
        this.name = name;
    }
}

再创建一个学生列表,来管理学生


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ListTest {
    /*
     * 用于存放学生的List
     */
    //集合后面的<>代表泛型的意思
    //泛型是规定了集合元素的类型
    //我们以后会详细讲到
    public List<Student> students;

    public ListTest() {
        this.students = new ArrayList<Student>();
    }

    /*
     * 用于往students中添加学生
     */
    public void testAdd() {
        // 创建一个学生对象,并通过调用add方法,添加到学生管理List中
        Student st1 = new Student("1", "张三");
        students.add(st1);

        // 取出 List中的Student对象
        Student temp = students.get(0);
        System.out.println("添加了学生:" + temp.id + ":" + temp.name);

        Student st2 = new Student("2", "李四");
        students.add(0, st2);
        Student temp2 = students.get(0);
        System.out.println("添加了学生:" + temp2.id + ":" + temp2.name);

        // 对象数组的形式添加
        Student[] student = { new Student("3", "王五"), new Student("4", "马六") };

        // Arrays类包含用来操作数组(比如排序和搜索)的各种方法,asList() 方法用来返回一个受指定数组支持的固定大小的列表
        students.addAll(Arrays.asList(student));
        Student temp3 = students.get(2);
        Student temp4 = students.get(3);
        System.out.println("添加了学生:" + temp3.id + ":" + temp3.name);
        System.out.println("添加了学生:" + temp4.id + ":" + temp4.name);
        Student[] student2 = { new Student("5", "周七"), new Student("6", "赵八") };
        students.addAll(2, Arrays.asList(student2));
        Student temp5 = students.get(2);
        Student temp6 = students.get(3);
        System.out.println("添加了学生:" + temp5.id + ":" + temp5.name);
        System.out.println("添加了学生:" + temp6.id + ":" + temp6.name);
    }

    /*
     * 取得List中的元素的方法
     */
    public void testGet() {
        int size = students.size();
        for (int i = 0; i < size; i++) {
            Student st = students.get(i);
            System.out.println("学生:" + st.id + ":" + st.name);

        }
    }

    /*
     * 通过迭代器来遍历
     */
    // 迭代器的工作是遍历并选择序列中的对象,Java 中 Iterator 只能单向移动
    public void testIterator() {
        // 通过集合的iterator方法,取得迭代器实例
        Iterator<Student> it = students.iterator();
        System.out.println("有如下学生(通过迭代器访问):");
        while (it.hasNext()) {

            Student st = it.next();
            System.out.println("学生" + st.id + ":" + st.name);
        }
    }

    /**
     * 通过for each 方法访问集合元素
     * 
     * @param args
     */
    public void testForEach() {
        System.out.println("有如下学生(通过for each):");
        for (Student obj : students) {
            Student st = obj;
            System.out.println("学生:" + st.id + ":" + st.name);
        }
    }

    /**
     * 修改List中的元素
     * 
     * @param args
     */
    public void testModify() {
        students.set(4, new Student("3", "吴酒"));
    }

    /**
     * 删除List中的元素
     * 
     * @param args
     */
    public void testRemove() {
        Student st = students.get(4);
        System.out.println("我是学生:" + st.id + ":" + st.name + ",我即将被删除");
        students.remove(st);
        System.out.println("成功删除学生!");
        testForEach();

    }

    public static void main(String[] args) {
        ListTest lt = new ListTest();
        lt.testAdd();
        lt.testGet();
        lt.testIterator();
        lt.testModify();
        lt.testForEach();
        lt.testRemove();

    }
}

迭代后的一部分结果为

相关文章

网友评论

      本文标题:Java核心API之lang包与util包

      本文链接:https://www.haomeiwen.com/subject/kkigqxtx.html