JAVA基础之泛型与集合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunshunli/article/details/84493028

一:泛型:

泛型类的定义格式:

class class-name  <type -parma-list>{}

实例化泛型类的格式:

class-name<type-parma-list> obj = new class-name<type-parma-list>(cons-arg-list)

type-parma-list用于指定当前泛型类可接受的类型参数的个数

例子:

泛型类

package com.dh.ch07;

public class Generic<T> {
	private T ob; // 定义泛型成员变量
	public Generic(T ob) {
		this.ob = ob;
	}
	public T getOb() {
		return ob;
	}
	public void setOb(T ob) {
		this.ob = ob;
	}
	public void showType() {
		System.out.println("T实际类型是: " + ob.getClass().getName());
	}
}
package com.dh.ch07;

public class GenericDemo {
	public static void main(String[] args) {
//		Generic<Integer> obj;
		// 定义泛型类Genneric的一个Integer版本
		Generic<Integer> intOb = new Generic<Integer>(88);
		intOb.showType();
		int i = intOb.getOb();
		System.out.println("value= " + i);
		System.out.println("----------------------------------");
		// 定义泛型类Genneric的一个String版本
		Generic<String> strOb = new Generic<String>("Hello Gen!");
		strOb.showType();
		String s = strOb.getOb();
		System.out.println("value= " + s);
	}
}

(二)集合:

 上面是集合的框架。

List接口继承Collection接口,元素允许重复,一元素的添加顺序来放置元素,不会重新排列。

List的实现类有ArrayList和LinkList。

List方法列表我就一一列举了,打击可以查看一下API。

下面举几个常用的集合例子:

1.ArrayList和Iterator



import java.util.*;

class ArrayListDemo {
//	public static List<String> arrayList;
	public static ArrayList<String> arrayList;
	// 初始化链表
	public static void init() {
		arrayList = new ArrayList<String>(4);
		// 即使生命长度,ArrayList还是根据需要动态分配
		System.out.println("初始化长度: " + arrayList.size());
		// 添加元素
		arrayList.add("first");
		arrayList.add("second");
		arrayList.add("third");
		arrayList.add("forth");
	}
	// 打印链表信息
	public static void printInfo() {
		System.out.println("增加元素后的长度: " + arrayList.size());
		// 通过集合构造链表
		ArrayList<String> arrayList2 = new ArrayList<String>(arrayList);
		// AbstractCollection对toString()方法提供了实现
		System.out.println("arrayList : " + arrayList);
		System.out.println("arrayList2: " + arrayList2);
	}
	// 对链表实现修改、删除操作
	public static void modify() {
		// 添加一个元素
		arrayList.add(1, "insert data");
		System.out.println("增加元素后的长度: " + arrayList.size());
		// 删除一个元素
		arrayList.remove("second");
		System.out.println("删除'second'元素后的长度: " + arrayList.size());
		arrayList.remove(2);
		System.out.println("删除第3个元素后的长度: " + arrayList.size());
		// 删除一个不存在的元素
		arrayList.remove("nothing");
		System.out.println("删除'nothing'元素后的长度: " + arrayList.size());
		// 抛出IndexOutOfBoundsException
		// arrayList.remove(10);
	}
	// 从List中获取数组,并遍历
	public static void toArray() {
		Object[] arr = arrayList.toArray();
		for (int i = 0; i < arr.length; i++) {
			String str = (String) arr[i];
			System.out.println((i + 1) + " : " + str);
		}
	}
	// 使用Iterator遍历
	public static void travel(){
		System.out.println("遍历前的长度: " + arrayList.size());
		// 使用迭代器进行遍历
		Iterator<String> iterator = arrayList.iterator();
		int i = 0;
		while (iterator.hasNext()) {
			String str = iterator.next();
			i++;
			System.out.println(str);
			if (i % 3 == 0) {
				// 通过迭代器删除元素
				iterator.remove();
			}
		}
		System.out.println("删除后的长度: " + arrayList.size());
	}
	// 使用for-each遍历
	public static void travel2(){
		for(String str:arrayList){
			System.out.println(str);
		}
	}
	public static void main(String args[]) {
		init();
		printInfo();
		modify();
		toArray();
		travel();
		travel2();
	}
}
初始化长度: 0
增加元素后的长度: 4
arrayList : [first, second, third, forth]
arrayList2: [first, second, third, forth]
增加元素后的长度: 5
删除'second'元素后的长度: 4
删除第3个元素后的长度: 3
删除'nothing'元素后的长度: 3
1 : first
2 : insert data
3 : forth
遍历前的长度: 3
first
insert data
forth
删除后的长度: 2
first
insert data

Set接口:继承自colletion接口,Set中的元素是不能重复的,其元素添加后不能保证与添加的顺序是一致的。

具体的实现类有HashSet和TreeSet

例子:

package com.dh.ch07;

import java.util.HashSet;

public class HashSetDemo {
	public static void main(String args[]) {
		HashSet<String> hashSet = new HashSet<String>();
		// 添加元素
		hashSet.add("first");
		hashSet.add("second");
		hashSet.add("third");
		hashSet.add("forth");
		System.out.println(hashSet);
		// 遍历
		for(String str: hashSet){
			System.out.println(str);
		}
	}
}
[third, forth, first, second]
third
forth
first
second
package com.dh.ch07;

import java.util.TreeSet;

public class TreeSetDemo {
	public static void main(String args[]) {
		TreeSet<String> treeSet = new TreeSet<String>();
		// 添加元素
		treeSet.add("first");
		treeSet.add("second");
		treeSet.add("third");
		treeSet.add("forth");
		System.out.println(treeSet);
		// 遍历
		for(String str: treeSet){
			System.out.println(str);
		}
	}
}
[third, forth, first, second]
third
forth
first
second

TreeSet元素安字符串顺序排列存储。

Map接口:实现的类HashMap和TreeMap

Map是一中把键对象和值对象进行关联的容器,Map中的键是不允许重复的。

Map中提供了Map.Entry接口,通过Map的Entry方法返回一个实现Map.Entry接口的对象的集合。

例子:

package com.dh.ch07;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class HashMapDemo {
	public static void main(String args[]) {
		// 创建缺省HashMap对象
		HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
		// 添加数据
		hashMap.put("Tom", new Integer(23));
		hashMap.put("Rose", new Integer(18));
		hashMap.put("Jane", new Integer(26));
		hashMap.put("Black", new Integer(24));
		hashMap.put("Smith", new Integer(21));
		// 获取Entry的集合
		Set<Map.Entry<String, Integer>> set = hashMap.entrySet();
		// 遍历所有元素
		for (Entry<String, Integer> entry : set) {
			System.out.println(entry.getKey() + " : " + entry.getValue());
		}
		System.out.println("---------");
		// 获取键集
		Set<String> keySet = hashMap.keySet();
		StringBuffer buffer = new StringBuffer("");
		for (String str : keySet) {
			buffer.append(str + ",");
		}
		String str = buffer.substring(0, buffer.length() - 1);
		System.out.println(str);
	}
}
Tom : 23
Smith : 21
Rose : 18
Black : 24
Jane : 26
---------
Tom,Smith,Rose,Black,Jane

下一篇是关于JDBC

猜你喜欢

转载自blog.csdn.net/sunshunli/article/details/84493028