JavaDay09--Notes(集合的接口与功能+常用类Math,Random,System,Runtime,Calendar)

一、collections

1.区别
set元素不可重复,无序
list元素可重复,有序
2.set集合的分类
(1)HashSet
方法:1.用算法算出存储索引hashCode(Object obj);;2.存入元素equals();)
效果:自动去重,散列形式
返回值:int型整数,对象的hashcode
TreeSet
(2)TreeSet
特点:对加入集合中的元素自动进行去重和排序,但是必须在重写的方法中编写排序规则
comparable<T>接口
int comparableTo();方法
if return 1;
else if return 1;
else return 0;

3.接口

JavaDay09--Notes(集合的接口与功能+常用类Math,Random,System,Runtime,Calendar)

4. 功能

动态存放多个对象
5. 例子
List<String> list=new ArrayList<String>();
Collections.addAll(list,"字符串1","字符串2",……);\\全部添加
Collections.add(list);\\添加
Collections.shuffle(list);  \\打乱
Collections.reverse(list);\\反转


模拟栈数据结构(先进后出FILO)::
LinkedList<String> list=new LinkedList<String>();
list.push("字符串1");
list.push("字符串2");
list.pop();
list.pop();
\\输出为 字符串2 字符串1
模拟队列数据结构(先进先出FIFO):
list.add("字符串1");
list.add("字符串2");
list.removeFirst();
list.removeFirst();
\\输出为 字符串1 字符串2


遍历字符串类型的数组
for(int i=0;i<list.size();i++){
     list.get(i)+"  ";   // List接口扩展法:get(int index)方法
for(String str:list){
     str+"  ";           // List加强for循环遍历

二、常用类

1. Math类

常用静态方法

JavaDay09--Notes(集合的接口与功能+常用类Math,Random,System,Runtime,Calendar)

2. Random类

public int nextInt(int  bound)

      返回从0(包含)bound(不包含)的一个“伪随机”  整数值。

 

public boolean nextBoolean()

     返回一个“伪随机”的boolean


3. System类

public static void arraycopy(Object src,intsrcPos,Objectdest,intdestPos,int length)

public static void exit(int status)
public static long currentTimeMillis()

*final不能被继承,不能被实例化(private 私有化)


4. Runtime类

totalMemory();
当前内存总量  (返回字节数)
maxMemory();
最大内存总量 …
freeMemory();
空闲内存总量 …


5.Date类与SimpleDateFormat类

Date类:表示日期和时间,提供操作日期和时间各组成部分的方法
SimpleDateFormat类:定制日期时间的格式

6.  Calendar
JavaDay09--Notes(集合的接口与功能+常用类Math,Random,System,Runtime,Calendar)



Copyright © 2018 Jin Hanquan. All rights reserved.