List接口中的特有的常用方法
package com.bjpowernode.javase.collection;
import java.util.*;
public class ListTest01 {
public static void main(String[] args) {
List mylist = new ArrayList();
mylist.add("A");
mylist.add("B");
mylist.add("C");
mylist.add("D");
mylist.add(1,"KING");
Iterator it = mylist.iterator();
while(it.hasNext()) {
Object obj = it.next();
System.out.println(obj);
}
Object firstObj = mylist.get(0);
System.out.println(firstObj);
for(int i = 0; i < mylist.size(); i ++) {
Object obj = mylist.get(i);
System.out.println(obj);
}
System.out.println(mylist.indexOf("KING"));
System.out.println(mylist.lastIndexOf("C"));
mylist.remove(0);
System.out.println(mylist.size());
mylist.set(0, "a");
for(int i = 0; i < mylist.size(); i ++) {
Object obj = mylist.get(i);
System.out.println(obj);
}
}
}
ArrayList集合初始化容量及扩容
package com.bjpowernode.javase.collection;
import java.util.*;
public class ArrayListTest01 {
public static void main(String[] args) {
List list1 = new ArrayList();
System.out.println(list1.size());
List list2 = new ArrayList(20);
System.out.println(list2.size());
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(4);
list1.add(5);
list1.add(6);
list1.add(7);
list1.add(8);
list1.add(9);
list1.add(10);
System.out.println(list1.size());
list1.add(11);
System.out.println(list1.size());
}
}
ArrayList集合的另一种构造方法
package com.bjpowernode.javase.collection;
import java.util.*;
public class ArrayListTest02 {
public static void main(String[] args) {
List myList1 = new ArrayList();
List myList2 = new ArrayList(100);
Collection c = new HashSet();
c.add(100);
c.add(200);
c.add(900);
c.add(50);
List myList3 = new ArrayList(c);
for(int i = 0; i < myList3.size(); i ++) {
System.out.println(myList3.get(i));
}
}
}
单向链表数据结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/74b845de47f82b55d09a8b4521e58516.png)
package com.bjpowernode.javase.danlink;
public class Node {
Object data;
Node next;
public Node(){
}
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
}
package com.bjpowernode.javase.danlink;
public class Link {
Node header;
int size = 0;
public int size() {
return size;
}
public void add(Object data) {
if(header == null) {
header = new Node(data,null);
}else {
Node currentLastNode = findLast(header);
currentLastNode.next = new Node(data, null);
}
size ++;
}
private Node findLast(Node node) {
if(node.next == null) {
return node;
}
return findLast(node.next);
}
public void modify(Object newObj) {
}
public int find(Object obj) {
return 1;
}
}
package com.bjpowernode.javase.danlink;
public class Test {
public static void main(String[] args) {
Link link = new Link();
link.add("abc");
link.add("def");
link.add("xyz");
System.out.println(link.size());
}
}
双项链表数据结构图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a7139453e0023ca33d9f3fe0ca8cee8c.png#pic_center)
链表的优点和缺点
package com.bjpowernode.javase.collection;
import java.util.*;
public class LinkedListTest01 {
public static void main(String[] args) {
List list = new LinkedList();
list.add("a");
list.add("b");
list.add("c");
for(int i = 0; i < list.size(); i ++) {
Object obj = list.get(i);
System.out.println(obj);
}
}
}
- 通过对LinkedList源码分析,将LinkedListTest01文件画出内存图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9bc21d546a2044c3ee833b5c6774f13c.png#pic_center)
Vector集合
package com.bjpowernode.javase.collection;
import java.util.*;
import java.util.Collections;
public class VectorTest01 {
public static void main(String[] args) {
List vector = new Vector();
vector.add(1);
vector.add(2);
vector.add(3);
vector.add(4);
vector.add(5);
vector.add(6);
vector.add(7);
vector.add(8);
vector.add(9);
vector.add(10);
vector.add(11);
Iterator it = vector.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
List myList = new ArrayList();
Collections.synchronizedList(myList);
myList.add("111");
myList.add("222");
myList.add("333");
myList.add("444");
myList.add("555");
}
}
泛型机制
package com.bjpowernode.javase.collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class GenericTest01 {
public static void main(String[] args) {
List<Animal> myList = new ArrayList<Animal>();
Cat c = new Cat();
Bird b = new Bird();
myList.add(c);
myList.add(b);
Iterator<Animal> it = myList.iterator();
while(it.hasNext()) {
Animal a = it.next();
a.move();
if(a instanceof Cat) {
Cat cat = (Cat)a;
cat.catchMouse();
}else if(a instanceof Bird) {
Bird bird = (Bird)a;
bird.fly();
}
}
}
}
class Animal{
public void move() {
System.out.println("动物在移动!");
}
}
class Cat extends Animal{
public void catchMouse() {
System.out.println("猫抓老鼠!");
}
}
class Bird extends Animal{
public void fly() {
System.out.println("鸟儿在飞翔!");
}
}
泛型-类型自动推断
package com.bjpowernode.javase.collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class GenericTest02 {
public static void main(String[] args) {
List<Animal> myList = new ArrayList<>();
myList.add(new Animal());
myList.add(new Cat());
myList.add(new Bird());
Iterator<Animal> it = myList.iterator();
while(it.hasNext()) {
Animal a = it.next();
a.move();
}
List<String> strList = new ArrayList<>();
strList.add("123");
strList.add("http://www.163.com");
strList.add("http://www.bjpowernode.com");
Iterator<String> it2 = strList.iterator();
while(it2.hasNext()) {
String s = it2.next();
String newString = s.substring(1);
System.out.println(newString);
}
}
}
自定义泛型
package com.bjpowernode.javase.collection;
public class GenericTest03<标志符随便写> {
public void doSome(标志符随便写 o) {
System.out.println(o);
}
public static void main(String[] args) {
GenericTest03<String> gt = new GenericTest03<>();
gt.doSome("abc");
GenericTest03<Integer> gt2 = new GenericTest03<>();
gt2.doSome(100);
MyInteger<String> mi = new MyInteger<>();
String s1 = mi.get();
MyInteger<Animal> mi2 = new MyInteger<>();
Animal a1 = mi2.get();
}
}
class MyInteger<T>{
public T get() {
return null;
}
}
foreach
package com.bjpowernode.javase.collection;
public class ForEachTest01 {
public static void main(String[] args) {
int[] arr = {23,45,67,89};
for(int i = 0;i < arr.length; i ++) {
System.out.println(arr[i]);
}
System.out.println("-------------------------");
for(int data : arr) {
System.out.println(data);
}
}
}
package com.bjpowernode.javase.collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class ForEachTest02 {
public static void main(String[] args) {
List<String> strList = new ArrayList<>();
strList.add("hello");
strList.add("world!");
strList.add("kitty!");
Iterator<String> it = strList.iterator();
while(it.hasNext()) {
String s = it.next();
System.out.println(s);
}
System.out.println(" ----------------1-----------------");
for(int i = 0; i < strList.size(); i ++) {
System.out.println(strList.get(i));
}
System.out.println(" -----------------2----------------");
for(String s : strList) {
System.out.println(s);
}
List<Integer> myList2 = new ArrayList<>();
myList2.add(100);
myList2.add(200);
myList2.add(300);
for(Integer i : myList2) {
System.out.println(i);
}
}
}