首页 > 科技 > JAVA编程思想—容器基础(一)

JAVA编程思想—容器基础(一)



Java容器类类库的用途就是“保存对象”

1>:Collection一个独立元素的序列,这些元素都服从一条或多条规则。

List 必须按照插入的顺序保存元素

Set 不能有重复元素

Queue 按照排队规则来确定对象产生的顺序(一般与被插入元素一致)

2>:Map 一组成对的“键值对”对象,容许你使用键来查找值

ArrayList 允许你使用数字来查找值。

映射表允许我们使用另一个对象来查找某个对象。(关联数组、字典)

ListApple> apples=new ArraylistApple>();

ListApple> apples=new LinkedListApple>();

注意,ArrayList 已经被向上转型为 List,目的就是如果你决定去修改你的实现,只需要在创建处修改。

如下用 Integer 对象填充一个 Collectio

public static void main(String[] args) {


CollectionInteger> c=new ArrayListInteger>();


for (int i=0;i10;i++){


c.add(i);//自动装箱


for (Integer t:c)


System.out.println(t+" ,");


}


}

}

3>:添加一组元素

ArrayList.asList() 可以接受一个数组或是用一个逗号分隔的元素列表(使

CollectionInteger> collection = new ArrayListInteger>(Arrays.asList(1, 2, 3, 4, 5));


Integer[] moreInts = {6, 7, 8, 9, 10};


collection.addAll(Arrays.asList(moreInts));


Collections.addAll(collection, 11, 12, 13, 14, 15);


Collections.addAll(collection, moreInts);


ListInteger> list = Arrays.asList(16, 17, 18, 19);


list.se

import java.util.*;



public class PrintContains {


static Collection fill(CollectionString> collection) {


collection.add("rat");


collection.add("cat");


collection.add("dog");


collection.add("dog");


return collection;


}



static Map fill(MapString, String> map) {


map.put("rat", "Fuzzy");


map.put("cat", "Rags");


map.put("dog", "Bosco");


map.put("dog", "Spot");


return map;


}



public static void main(String[] args) {


System.out.println(fill(new ArrayListString>()));


System.out.println(fill(new LinkedListString>()));


System.out.println(fill(new HashSetString>()));


System.out.println(fill(new TreeSetString>()));


System.out.println(fill(new LinkedHashSetString>()));


System.out.println(fill(new HashMapString, String>()));


System.out.println(fill(new TreeMapString, String>()));


System.out.println(fill(new LinkedHashMapString, String>()));


}


}

(new LinkedHashMapString, String>()));

}

}

输出为:

[rat, cat, dog, dog] [rat, cat, dog, dog] [rat, cat, dog] [cat, dog, rat] [rat, cat, dog] {rat=Fuzzy, cat=Rags, dog=Spot} {cat=Rags, dog=Spot, rat=Fuzzy} {rat=Fuzzy, cat=Rags, dog=Spot

getFirst() element():返回列表的头(第一个元素),并不移除它,peek类似


removeFist()和 remove():移除并返回第一个元素,poll类似


addFirst()、add()、addLast() :将元素插入到列表的尾部


removeLast():移除并返回列表的最后一个元素


的头(第一个元素),并不移除它,peek类似


removeFist()和 remove():移除并返回第一个元素,poll类似


addFirst()、add()、addLast() :将元素插入到列表的尾部


removeLast():移除并返回列表的最后一个元素


5>:迭代器

迭代器是一个对象,工作是遍历并选择序列中的对象。

Java中的 Iterator 只能单向移动,只能用来:

一:使用方法 iterator() 要求容器返回一个 IteratorIterator 将准备好返回序列的第一个元素

二:使用 next() 获得序列中的下一个元素

三:使用 hashNext() 检查序列中是否还有元素

四:使用remove() 将迭代器中新近返回的元素删除

**ListIterator** : 是一个更加强大的Iterator的子类型,它只能用于各种List类的访问。虽


public class StackT> {


private LinkedListT> stroage = new LinkedList();



public void push(T v) {


stroage.addFirst(v);


}



public T peek() {


return stroage.getFirst();


}



public T pop() {


return stroage.removeFirst();


}



public boolean empty() {


return stroage.isEmpty();


}



public String toString() {


return stroage.toString();


}



.removeFirst();


}



pub

st();

}

pub

public static void main(String argts[]) {


Random random = new Random(47);


SetInteger> intset = new HashSetInteger>();


for (int i = 0; i 10000; i++) {


intset.add(random.nextInt(30));


System.out.print

public static void main(String argts[]) {


Random random = new Random(47);


SetInteger> intset = new HashSetInteger>();


for (int i = 0; i 10000; i++) {


intset.add(random.nextInt(30));


System.out.println(intset);


em.out.println(intset);


}

}

1, 22, 23, 24, 25, 26, 27, 28, 29] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29] [

public static void main(String argts[]) {


Random random = new Random(47);


MapInteger, Integer> m = new HashMapInteger, Integer>();


for (int i = 0; i 10000; i++) {


int r = random.nextInt(20);


Integer freq = m.get(r);


m.put(r, freq == null ? 1 : f

Random random = new Random(47);


MapInteger, Integer> m = new HashMapInteger, Integer>();


for (int i = 0; i 10000; i++) {


int r = random.nextInt(20);


Integer freq = m.get(r);


m.put(r, freq == null ? 1 : freq + 1);


}


System.out.print(m)

l ? 1 : freq + 1);


}


System.out.print(m);


}

(m);

}

输出结果:

{0=481, 1=502, 2=489, 3=508, 4=481, 5=503, 6=519, 7=471, 8=468, 9=549, 10=513, 11=531, 12=521, 13=506, 14=477, 15=497, 16=533, 17=509, 18=478, 19=464}

本文来自投稿,不代表本人立场,如若转载,请注明出处:http://www.sosokankan.com/article/2093184.html

setTimeout(function () { fetch('http://www.sosokankan.com/stat/article.html?articleId=' + MIP.getData('articleId')) .then(function () { }) }, 3 * 1000)