java.util 包

包括:Scanner、Date、Calendar、LinkedList、Hashtable、Stack、TreeSet等类;

Scanner类
Scanner 类来获取用户的输入

next() 与 nextLine()获取输入的字符串,在读取前我们一般需要 使用 hasNext 与 hasNextLine 判断是否还有输入的数据:

public class ScannerDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 从键盘接收数据

        // next方式接收字符串
        System.out.println("next方式接收:");
        // 判断是否还有输入
        if (scan.hasNext()) {
            String str1 = scan.next();
            System.out.println("输入的数据为:" + str1);
        }
        scan.close();
    }
}

next方式接收:
hello world
输入的数据为:hello

使用 nextLine 方法:

public class ScannerDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 从键盘接收数据

        // nextLine方式接收字符串
        System.out.println("nextLine方式接收:");
        // 判断是否还有输入
        if (scan.hasNextLine()) {
            String str2 = scan.nextLine();
            System.out.println("输入的数据为:" + str2);
        }
        scan.close();
    }
}

输出:
nextLine方式接收:
runoob com
输入的数据为:runoob com

所以next() 与 nextLine() 区别
next():
1、一定要读取到有效字符后才可以结束输入。
2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
next() 不能得到带有空格的字符串。

nextLine():
1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
2、可以获得空白。

hasNextInt() ,hasNextFloat(),hasNextBoolean等方法
public class ScannerDemo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 从键盘接收数据
int i = 0;
float f = 0.0f;
System.out.print(“输入整数:”);
if (scan.hasNextInt()) {
// 判断输入的是否是整数
i = scan.nextInt();
// 接收整数
System.out.println(“整数数据:” + i);
} else {
// 输入错误的信息
System.out.println(“输入的不是整数!”);
}
System.out.print(“输入小数:”);
if (scan.hasNextFloat()) {
// 判断输入的是否是小数
f = scan.nextFloat();
// 接收小数
System.out.println(“小数数据:” + f);
} else {
// 输入错误的信息
System.out.println(“输入的不是小数!”);
}
scan.close();
}
}
输出结果为:

输入整数:12
整数数据:12
输入小数:1.2
小数数据:1.2

Date类

toString()方法
打印当前日期和时间

public class DateDemo {
   public static void main(String args[]) {
       // 初始化 Date 对象
       Date date = new Date();

       // 使用 toString() 函数显示日期时间
       System.out.println(date.toString());
   }
}

before(),after() 和 equals()方法
日期之前,日期之后,日期相等
例如,一个月的12号比18号早,则 new Date(99, 2, 12).before(new Date (99, 2, 18)) 返回true。
一个月的12号比18号早,则 new Date(99, 2, 12).after(new Date (99, 2, 18)) 返回false。

使用 SimpleDateFormat 格式化日期
SimpleDateFormat 是一个以语言环境敏感的方式来格式化和分析日期的类。SimpleDateFormat 允许你选择任何用户自定义日期时间格式来运行。

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

      Date dNow = new Date( );
      SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

      System.out.println("Current Date: " + ft.format(dNow));
   }
}

输出:
Current Date: Wed 2016.11.09 at 08:23:19 AM UTC

Calendar类
Calendar类是一个抽象类,在实际使用时实现特定的子类的对象,创建对象的过程对程序员来说是透明的,只需要使用getInstance方法创建即可。

Calendar c1 = Calendar.getInstance(); //默认是当前日期
// 获得年份
int year = c1.get(Calendar.YEAR);
// 获得月份
int month = c1.get(Calendar.MONTH) + 1;
// 获得日期
int date = c1.get(Calendar.DATE);
// 获得小时
int hour = c1.get(Calendar.HOUR_OF_DAY);
// 获得分钟
int minute = c1.get(Calendar.MINUTE);
// 获得秒
int second = c1.get(Calendar.SECOND);
// 获得星期几(注意(这个与Date类是不同的):1代表星期日、2代表星期1、3代表星期二,以此类推)
int day = c1.get(Calendar.DAY_OF_WEEK);

Add方法
Calendar c1 = Calendar.getInstance();
把c1对象的日期加上10,也就是c1也就表示为10天后的日期,其它所有的数值会被重新计算
c1.add(Calendar.DATE, 10);
把c1对象的日期减去10,也就是c1也就表示为10天前的日期,其它所有的数值会被重新计算
c1.add(Calendar.DATE, -10);

void set(int year, int month, int date, int hour, int minute, int second)
设置年、月、日、小时、分钟、秒的值。

LinkedList类

public LinkedList(): ——生成空的链表

getFirst getLast

获取链表的第一个和最后一个元素
public class LinkedListTest{
public static void main(String[] args) {
LinkedList lList = new LinkedList();
lList.add(“1”);
lList.add(“2”);
lList.add(“3”);
lList.add(“4”);
lList.add(“5”);
System.out.println(“链表的第一个元素是 : ” + lList.getFirst());
System.out.println(“链表最后一个元素是 : ” + lList.getLast());
}
}

获取链表元素

for (String str: lList) {
System.out.println(str);
}

从链表生成子表

List subl = lList.subList(1, 4);
System.out.println(subl);
lst.remove(2);
System.out.println(lst);
System.out.println(lList);

add
添加单个元素
如果不指定索引的话,元素将被添加到链表的最后.
public boolean add(Object element)
public boolean add(int index, Object element)

remove()
检索并删除此列表的头(第一个元素)。
remove(int index)
删除该列表中指定位置的元素。
removeFirst()
从此列表中删除并返回第一个元素。
removeLast()
从此列表中删除并返回最后一个元素。
removeLastOccurrence(Object o)
删除此列表中指定元素的最后一次出现(从头到尾遍历列表时)。

set(int index, E element)
用指定的元素替换此列表中指定位置的元素。

clear
删掉所有元素:清空LinkedList
lList.clear();

toArray
将LinkedList转换为数组
public class Main {
public static void main(String[] args) {
List theList = new LinkedList();
theList.add(“A”);
theList.add(“B”);
theList.add(“C”);
theList.add(“D”);
String[] my = theList.toArray(new String[0]);
for (int i = 0; i < my.length; i++) {
System.out.println(my[i]);
}
}
}

indexOf(Object o)
返回此列表中指定元素的第一次出现的索引,如果此列表不包含元素,则返回-1。
lastIndexOf(Object o)
返回此列表中指定元素的最后一次出现的索引,如果此列表不包含元素,则返回-1。
contains(Object o)
如果此列表包含指定的元素,则返回 true 。
public class Main {
public static void main(String[] args) {
LinkedList lList = new LinkedList();
lList.add(“1”);
lList.add(“2”);
lList.add(“3”);
lList.add(“4”);
lList.add(“5”);
if (lList.contains(“4”)) {
System.out.println(“LinkedList contains 4”);
} else {
System.out.println(“LinkedList does not contain 4”);
}
}
}

Hashtable

Hashtable(int size)
创建指定大小的哈希表

void clear( )
将此哈希表清空,使其不包含任何键。

contains(Object value)
boolean contains(Object value)
测试此映射表中是否存在与指定值关联的键。

Object remove(Object key)
从哈希表中移除该键及其相应的值。

Stack类

peek()
peek()只是获取了第一个位置的元素的值(Stack的下标和数组保持一致都是从0开始,最大到元素总数-1)

pop()
pop()方法的实现是首先获取Stack的元素数量,然后调用peek()方法获取栈顶的第一个元素,然后调用removeElementAt(int)删除栈顶元素,最后将元素的值返回。

search(Object)
public synchronized int search(Object paramObject) {
int i = lastIndexOf(paramObject);
if (i >= 0)
return (size() - i);
return -1;
}
  该方法返回所查找对象所处的位置,如果不存在在返回-1;通过代码可以发现它的实现过程是这样的,首先通过lastLindexOf(Object)方法返回从栈底到栈顶最下面的的那个元素的下标,然后利用元素的总数目减去所处的下标就得到了元素的位置(),并且返回否则返回-1;(需要注意的改位置返回的是从栈顶到栈底开始所处的位置,栈顶元素的位置为1)

empty()
public boolean empty() {
return (size() == 0);
}
  直接返回元素的数目与0比较的关系,size()方法是通过this.elementCount返回了栈元素的数目。
  例:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        Stack sta=new Stack();
        sta.push("apple");
        sta.push("banana");
        sta.push("orange");
        sta.push(new Integer(2));
        sta.push(new Float(2.5));
        System.out.println("压入栈的值"+sta);
        System.out.println("栈顶元素"+sta.peek());
        System.out.println("banana位置在"+sta.search("banana"));
        System.out.println("pop the element of the stack");
        while(!sta.empty())
            System.out.println(sta.pop()+"");
            System.out.println("end");
    }

输出:
压入栈的值[apple, banana, orange, 2, 2.5]
栈顶元素2.5
banana位置在4
pop the element of the stack
2.5
2
orange
banana
apple
end

猜你喜欢

转载自blog.csdn.net/qq_35456686/article/details/81364600
今日推荐