学习一项语言,除了一些基本语法的使用方法,最重要的无外乎其常用数据结构,其实学习一项语言不难,真正难的是
用语言去解决实际问题,其中我感觉里面有数组,ArrayList,ListedList ,Stack,HashSet,Dictionary
其中Collections是一个类,容器的工具类,就如同Arrays是数组的工具类
我们都知道数组是最基本的数据结构,但普通的数组的命名(int a[10]={.......})有可能造成数组大小的浪费或者数组不够用
的情况,所以我们用ArrayList解决问题:
ArrayList的好处:
相当于动态创建数组,不会造成内存的浪费。
ArrayList的坏处:
对于一些比较多的元素的插入删除的时间复杂度就比较高,(所以我们用二年级就学过的链表解决这种)
首先新建一个Hero对象
package collection;
public class Hero {
public String name;
public int hp;
public Hero(){}
public Hero(String name,int hp){
this.name=name;
this.hp=hp;
}
public String getName(){
return name;
}
public int getHp(){
return hp;
}
}
package collection;
import java.util.ArrayList;
import java.util.Iterator;
import collection.Hero;
public class TestCollection {
public static void main(String[] args){
ArrayList heros=new ArrayList();//声明ArrayList
heros.add(new Hero("bob",100));
heros.add(new Hero("arm",100));
heros.add(new Hero("cow",100));
System.out.println(heros.size());
System.out.println(heros);
//迭代器就是一种遍历方法,next指向下个元素吧
Iterator<Hero> it=heros.iterator();//声明迭代器
while(it.hasNext()){
System.out.println(it.next());
}
}
}
ListedList可以实现链表和列表的使用:下面是一个基本的字符链表的创建,ListedList还支持好多方法,具体可以练练一些关于链表的算法题。
package LinkList;
import java.util.LinkedList;
public class LinkList {
public static void main(String[] args){
LinkedList<String> sits=new LinkedList<String>();
sits.add("aaaa");
sits.add("bbbb");
sits.add("cccc");
sits.add("dddd");
sits.addFirst("Wiki");//首元素的插入
System.out.println(sits);
for(int size=sits.size(),i=0;i<size;i++){
System.out.println(sits.get(i));
}
}
}
HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合。
HashSet 允许有 null 值。
HashSet 是无序的,即不会记录插入的顺序。
HashSet 不是线程安全的, 如果多个线程尝试同时修改 HashSet,则最终结果是不确定的。
你必须在多线程访问时显式同步对 HashSet 的并发访问。
package hashset;
import java.util.HashSet;
public class test {
public static void main(String[] args){
HashSet<String> sits=new HashSet<String>();
sits.add("aaaa");
sits.add("bbbb");
sits.add("cccc");
sits.add("dddd");
System.out.println(sits);
}
}
HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。
HashMap 实现了 Map 接口,根据键的 HashCode 值存储数据,具有很快的访问速度,最多允许一条记录的键为 null,不支持线程同步。
HashMap 是无序的,即不会记录插入的顺序。
HashMap 继承于AbstractMap,实现了 Map、Cloneable、java.io.Serializable 接口。
package hashmap;
import java.util.HashMap;
//HashMap 类提供类很多有用的方法,添加键值对(key-value)可以使用 put() 方法:
public class Test02 {
public static void main(String[] args){
HashMap<Integer,String> sits=new HashMap<Integer,String>();
sits.put(1,"aaaaa");
sits.put(2,"aaaaa");
sits.put(3,"aaaaa");
sits.put(4,"aaaaa");
System.out.println(sits);
System.out.println(sits.get(3));//访问元素
}
}
Java Stack 类
栈是Vector的一个子类,它实现了一个标准的后进先出的栈。
堆栈只定义了默认构造函数,用来创建一个空栈。 堆栈除了包括由Vector定义的所有方法,也定义了自己的一些方法。
package stack;
import java.util.EmptyStackException;
import java.util.Stack;
public class StackTest {
static void showpush(Stack<Integer> st, int a) {
st.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("Stack:" + st);
}
static void showpop(Stack<Integer> st) {
Integer a = (Integer) st.pop();
System.out.println("pop(" + a + ")");
System.out.println("Stack:" + st);
}
public static void main(String[] args){
Stack<Integer> st = new Stack<Integer>();
System.out.println("stack: " + st);
showpush(st, 42);
showpush(st, 66);
showpush(st, 99);
showpop(st);
showpop(st);
showpop(st);
try {
showpop(st);
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}
运行结构: