数组
一维数组
int a[]; //a 还没有new操作 实际上是null,也不知道内存位置
int[] c = new int[2]; //c有2个元素,都是0
c[0] = 10; c[1] = 20; //逐个初始化
int d[] = new int[]{0,2,4};//d有3个元素, 0,2,4,同时定义和初始化
int d1[] = {1,3,5}; //d1有3个元素, 1,3,5 同时定义和初始化
//注意声明变量时候没有分配内存,不需要指定大小,以下是错误示例
//int e[5];
//int[5] f;
//int[5] g = new int[5];
//int h[5] = new int[5];
两种访问方式:
//需要自己控制索引位置
for(int i=0;i<d.length;i++) {
System.out.println(d[i]);
}
//无需控制索引位置
for(int e : d) {
System.out.println(e);
多维数组
//规则数组
int a[][] = new int[2][3];
//不规则数组
int b[][];
b = new int[3][];
b[0]=new int[3];
b[1]=new int[4];
b[2]=new int[5];
两种访问方式
int k = 0;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
a[i][j] = ++k;
}
}
for(int[] items : a)
{
for(int item : items)
{
System.out.print(item + ", ");
}
System.out.println();
}
附图:
注:可以看成二维数组的每一个元素都是一个一维数组
JCF
- 列表
- 集合
- 映射
- JCF的集合接口是Collection
- add 增加
- contains 包含
- remove 删除
- size 元素个数
- iterator 迭代器
- JCF的迭代器接口是Iterator(提供遍历的工具)
- hasNext 判断是否有下一个元素
- next 获取下一个元素
- remove 删除某一个元素
(1)列表
主要实现类
- ArrayList(非同步)
- LinkedList(非同步)
- Vector(同步)
这里只介绍ArrayList
import java.util.ArrayList;
import java.util.Iterator;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(3);
al.add(2);
al.add(new Integer(6));
System.out.print("The third element is ");
System.out.println(al.get(3));
al.remove(3); //删除第四个元素,后面元素往前挪动
al.add(3, 9); //将9插入到第4个元素,后面元素往后挪动
三种遍历方法
public static void traverseByIterator(ArrayList<Integer> al)
{
long startTime = System.nanoTime();
System.out.println("============迭代器遍历==============");
Iterator<Integer> iter1 = al.iterator();
while(iter1.hasNext()){
iter1.next();
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
public static void traverseByIndex(ArrayList<Integer> al)
{
long startTime = System.nanoTime();
System.out.println("============随机索引值遍历==============");
for(int i=0;i<al.size();i++)
{
al.get(i);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
public static void traverseByFor(ArrayList<Integer> al)
{
long startTime = System.nanoTime();
System.out.println("============for循环遍历==============");
for(Integer item : al)
{
;
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
}
注:
- ArrayList al ,是泛型表示,只能装对象,当add(3)时,int 的 3 会自动装箱变成Integer对象。
- 根据访问数据的形式,不同的容器会有不同的特征。
(2)集合Set
主要实现类
- HashSet(遍历不保留顺序,可以null)
- TreeSet(遍历按照所储存对象大小升序,不可以null)
- LinkedHashSet(遍历按插入顺序,可以null)
- clear 清除整个集合
- retainAll计算交集
这里只介绍HashSet
HashSet<Integer> hs = new HashSet<Integer>();
hs.add(null);
hs.add(1000);
hs.add(20);
hs.add(3); //3 重复
hs.add(null); //null重复
System.out.println(hs.size()); //6
if(!hs.contains(6))
{
hs.add(6);
}
System.out.println(hs.size()); //7
hs.remove(4);
System.out.println(hs.size()); //6
//hs.clear();
//System.out.println(hs.size()); //0
System.out.println("============for循环遍历==============");
for(Integer item : hs)
{
System.out.println(item);
}
System.out.println("============测试集合交集==============");
HashSet<String> set1 = new HashSet<String>();
HashSet<String> set2 = new HashSet<String>();
set1.add("a");
set1.add("b");
set1.add("c");
set2.add("c");
set2.add("d");
set2.add("e");
//交集
set1.retainAll(set2);
System.out.println("交集是 "+set1);
System.out.println("============测试多种遍历方法速度==============");
HashSet<Integer> hs2 = new HashSet<Integer>();
for(int i=0;i<100000;i++) {
hs2.add(i);
}
traverseByIterator(hs2);
traverseByFor(hs2);
}
public static void traverseByIterator(HashSet<Integer> hs)
{
long startTime = System.nanoTime();
System.out.println("============迭代器遍历==============");
Iterator<Integer> iter1 = hs.iterator();
while(iter1.hasNext()){
iter1.next();
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
public static void traverseByFor(HashSet<Integer> hs)
{
long startTime = System.nanoTime();
System.out.println("============for循环遍历==============");
for(Integer item : hs)
{
;
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
}
注:集合具有互异性,那么判重标准如下
1.判断两个对象的hashCode返回值
【hashCode是经过一系列运算得到,两个对象内容一致但因为是两个不同对象,故hashCode不同,需要重写该方法判重】
2.判断两个对象equals方法
以上方法继承Object类,故所有类都有,所以有时需要重写自己的判断标准
注:
-
一般重写以上两个方法,也要把toString方法一起重写,保证结果一致,三个方法一体
-
添加到TreeSet的,需要实现Comparable接口,即实现compareTo方法,其判重标准与1.2.两个方法无关。
(3)映射Map
主要实现类
- Hashtable
- HashMap(遍历无序)
LinkedHashMap(保持插入顺序)
TreeMap(根据compareTo方法)
- Properties
- clear 清空数据
- contains==containsValue 是否包含某一个值
- containsKey是否包含某一个Key
- get 根据Key获取相应的值
- put 增加新的K-V对
- remove 删除某一个K-V对
- size 返回数据大小
这里只介绍HashMap
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class HashMapTest {
public static void main(String[] args) {
HashMap<Integer,String> hm =new HashMap<Integer,String>();
hm.put(1, null);
hm.put(null, "abc");
hm.put(1000, "aaa");
hm.put(2, "bbb");
hm.put(30000, "ccc");
System.out.println(hm.containsValue("aaa"));
System.out.println(hm.containsKey(30000));
System.out.println(hm.get(30000));
hm.put(30000, "ddd"); //更新覆盖ccc
System.out.println(hm.get(30000));
hm.remove(2);
System.out.println("size: " + hm.size());
hm.clear();
System.out.println("size: " + hm.size());
HashMap<Integer,String> hm2 =new HashMap<Integer,String>();
for(int i=0;i<100000;i++)
{
hm2.put(i, "aaa");
}
traverseByEntry(hm2);
traverseByKeySet(hm2);
}
public static void traverseByEntry(HashMap<Integer,String> ht)
{
long startTime = System.nanoTime();
System.out.println("============Entry迭代器遍历==============");
Integer key;
String value;
Iterator<Entry<Integer, String>> iter = ht.entrySet().iterator();//整个键值对
while(iter.hasNext()) {
Map.Entry<Integer, String> entry = iter.next();
// 获取key
key = entry.getKey();
// 获取value
value = entry.getValue();
//System.out.println("Key:" + key + ", Value:" + value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
public static void traverseByKeySet(HashMap<Integer,String> ht)
{
long startTime = System.nanoTime();
System.out.println("============KeySet迭代器遍历==============");
Integer key;
String value;
Iterator<Integer> iter = ht.keySet().iterator();//只用Key的迭代器
while(iter.hasNext()) {
key = iter.next();
// 获取value
value = ht.get(key);
//System.out.println("Key:" + key + ", Value:" + value);
}
long endTime = System.nanoTime();
long duration = endTime - startTime;
System.out.println(duration + "纳秒");
}
}
工具类
在数据容器上,实现高效操作
- Arrays
- Collection
(1)Arrays
处理对象是数组
Random r = new Random();
int[] a = new int[10];
for(int i=0;i<a.length;i++) {
a[i] = r.nextInt();
}
//排序
Arrays.sort(a);
//查找位置
a[a.length-1] = 10000;
System.out.println("10000 的位置是" + Arrays.binarySearch(a, 10000));
//拷贝前五个元素
int[] b = Arrays.copyOf(a, 5);
//批量赋值
Arrays.fill(a, 100);
Arrays.fill(a, 2, 8, 200);
//等价性比较
System.out.println(Arrays.equals(a, b));
(2)Collection
处理对象是Collection及其子类
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(12);
list.add(2);
list.add(19);
// 排序
Collections.sort(list);
// 检索
System.out.println("元素所在的索引值是:" + Collections.binarySearch(list, 12));
//最大最小
System.out.println("最大值:" + Collections.max(list));
System.out.println("最小值:" + Collections.min(list));
//反序
Collections.reverse(list); //翻转不需要用到排序
//批量赋值
Collections.fill(list, 100); //全部赋值为100
}
注:对象排序需要遵循一定的规则,Interger对象可以自然按照数值大小进行排序,但是普通的自定义对象无法排序,因此,普通的自定义对象需要实现Comparable 接口,即按照compareTo方法规定的原则进行排序。
附:Comparable接口是个啥?以上好几次提到
可以拿到源码就实现接口,即重写compareTo方法
public int compareTo(Person another) {
int i = 0;
i = name.compareTo(another.name); // 使用字符串的比较
if (i == 0) {
// 如果名字一样,比较年龄, 返回比较年龄结果
return age - another.age;
} else {
return i; // 名字不一样, 返回比较名字的结果.
}
如果没有源码就新建Comparator比较器
public class Person2Comparator implements Comparator<Person2> {
public int compare(Person2 one, Person2 another) {
int i = 0;
i = one.getName().compareTo(another.getName());
if (i == 0) {
// 如果名字一样,比较年龄,返回比较年龄结果
return one.getAge() - another.getAge();
} else {
return i; // 名字不一样, 返回比较名字的结果.
}
}
然后再排序中传入自己的比较器
Arrays.sort(ps, new Person2Comparator());//调用sort时给他比较器