1.long类型:建议数据后加L表示。
2.float类型:建议数据后加F表示。
3.自动转换:将 取值范围小的类型 自动提升为 取值范围大的类型 。
4.强制转换: int i = (int)1.5;
5.Scanner 类
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
6.java.lang包下的所有类无需导入
7.Random类
Random rd = new Random();
int i = rd.nextInt(100);
二.集合
数组:不可变的同种数据类型,可以是任意数据类型
集合:可变,只能是对象
Collection接口:所有单列集合的共性方法;
所有的单列集合都可使用的方法;没有带索引的方法
List接口:1.有序;可重复:索引(可以使用普通for) Set接口:不可重复;没有索引
2.Collection 接口
size();
isEmpty();
contains(<E>);
add(Object o);
remove(Object o);
clear();
toArray();
3.Iterator 接口:取出前判断元素是否存在,到一直取完.
hasNext(); 还有没有元素
next();取出下一个元素
获取Iterator: collection.iterator();
4.泛型的通配符 ?;
泛型上限限定? Extends E 只能是E的子或本身
泛型下限限定? super E 只能是E的本身或父类
5.list 接口: 有序,索引,重复
get(int index)
set(int index, Object element)
add(int index, Object element)
remove(int index)
indexOf(Object o)
subList(int fromIndex, int toIndex)
6.ArrayList:不是线程安全的,底层是数组,查询快,增删慢
7.linklist:不是线程安全的,底层是链表,查询慢,增删快
getFirst()
getLast()
removeFirst()
removeLast(
addFirst(E e)
addLast(E e)
push(E e)
pop()
8.Vector (1.0):底层是数组,是线程安全的.
9.set 接口, 不重复,没有索引.
10.hashSet:底层是一个哈希表结构,查询快
object.hashcode();
11.哈希表:
1.8前: 哈希表 = 数组 + 链表.
1.8后: 哈希表 = 数组 + 链表. (超过8位)
哈希表 = 数组 + 红黑树.
数组结构,把相同哈希值的元素分组;链表或红黑树结构把相同哈希值的元素连接到一起.
12.hashSet存储自定义元素(E)
E:必须重写hashcode方法和equls()方法;
13.linkhashSet:底层是hashSet+链表 双重链表,可预知顺序
14.Collections: 集合工具类
Collections.addAll()
Collections.shuffle():乱序
Collections.sort(list);自然排序,使用的前提,要实现Comparable接口的CompareTo()方法;
三,Map集合
1.Map接口:不是单列结合<K,E>;
HashMap: 不安全,底层是哈希表;查询快;无须的集合
LinkHashMap:哈希表+链表;可以保证顺序
2.Map:
put(k,v);
get(k);
remove(k);
contionsKey(k);
entrySet();
keySet();
String,StringBuff, Stringbuilder
String:常量时使用;StringBuff:线程安全的;Stringbuilder:非线程安全;
常用构造:String str1 = new String();
char[] chars = {‘a’,’b’,’c’};
String stre2 = new String(chars);
byte bytes = {97,98,99};
String str3 = new String(bytes);
String常用Api:
Public boolean equals(Object anObject);
Public boolean equalsIgnoreCase(String anotherStrimg);
Int length();
String concat(String str);
Char charAt(int index);
int indexof(String str);
String subsString(int beginIndex);
String subsString(int beginIndex ,int endIndex);
char[] toCharArray();
bytes[] getBytes();
String replace(CharSequence taget,CharSequence replaceMent);
CharSequence:是一个接口,也是一种引用类型,作为参数类型,可以吧String对象传递到方法中.
String[] split(String regex);
Arrays类:
Public static String toString(int[] a);
Public static void sort(int[] a);
Math类:
public static double abs(double a)
public static double floor(double a)
public static long round(double a)
Object类:
* `public String toString()`:返回该对象的字符串表示。
`public boolean equals(Object obj)`:指示其他某个对象是否与此对象“相等”。
Objects类:
在比较两个对象的时候,Object的equals方法容易抛出空指针异常,而Objects类中的equals方法就优化了这个问题。方法如下:
`public static boolean equals(Object a, Object b)`:判断两个对象是否相等。
new Date():创建日期对象,把当前的时间
new Date(0L):创建日期对象,把当前的毫秒值转成日期对象
网友评论