Java泛型(拓展ArrayList、HashMap)

版权声明:翀版 https://blog.csdn.net/biggerchong/article/details/82925728

  泛型介绍

       Java中有泛型这个概念,最开始可能一脸懵逼,因为泛型听起来很高大上的样子,其实Java中的泛型就是C++中的模板类(Template),这样在Java泛型中就可以很轻松的建立各种自定义类型的List了,有没有觉得很舒服。另外Java中还提供ArrayList(继承于List,比List更优化)、HashMap(继承于抽象类Map,一种映射类)。泛型类中就是引入一个模板类T(其实就是一个Object类),下面我们来自己定义一个泛型类:

//GenericList.java
public class GenericList<T>  //模板T
{
	private static class GenericNode<T>  //相当于数据结构中得List struct
	{
		T value;
		GenericNode<T> next; 
		public GenericNode()
		{
			
		}
	}
	
	public GenericList()
	{
		head.next=null; //初始化
	}

	//链表头
	private GenericNode<T> head=new GenericNode<T>();

	//向链表中添加ELement
	public void add(T value)
	{
		
		GenericNode<T> tair=head;
		while(tair.next!=null)
			tair=tair.next;
		GenericNode<T> node=new GenericNode<T>();
		tair.next=node;
		node.value=value;
		node.next=null;
	}
	
    //取得链表长度
	public int length()
	{
		int count=0;
		GenericNode<T> m=head;
		while(m.next!=null)
		{
			m=m.next;
			count++;
		}
		return count;
	}
	
	// 获取迭代器,方便迭代遍历
	public Iterator<T> getIterator()
	{
		Iterator<T> iter = new Iterator<T>();
		iter.node = this.head;
		return iter;
	}
	
	//定义Iterator迭代类
	public static class Iterator<T>
	{
		private GenericNode<T> node;
		
		// 是否还有下一节点
		public boolean hasNext()
		{
			return node.next != null;
		}
		
		// 获取下一节点的值
		public T next()
		{
			T value = node.next.value;
			node = node.next;
			return value;
		}
	}

}

       以上代码中引入了,迭代器(自定义的,java中还有自带Iterator类)。迭代器叫相当于给GenericList提供了一种迭代遍历得方法,相信学过数据结构中List结构的小可爱们一定不陌生,这种迭代遍历方法是利用next指针来依次遍历整个List中的value。

 


ArrayList与HashMap

Java中提供自带的两个常用类似list的类:Array List、HashMap(数据结构中的哈希表)。

ArrayList常用方法有:add(index,value)向ArrayList对象中添加Element,还可以指定位置添加

                                    remove(index)删除指定位置的Element

                                    clear() 清空ArrayList

                                    get(index)取得index位置出的value

                                    iterator()载入迭代器中

HashMap常用方法有:put(key,value) 将value加入hashMap中

                                     remove(key) 移除key对应的value

                                    get(key)取得key对应的value

                       注:HashMap创建时要同时定义key与value。例如:HashMap<Interger,Student> key要满足唯一性、可比较性

                             当put的key重复时,以最后一个key为主。(put了几个key一模一样的value,HashMap以最后一个key对应的                                   value为主)


三种方法测试代码如下   

//GenericList
    前面已经列出,这里主要是介绍使用GenericList(见Hello.java)

//Student.java
public class Student
{
	int id;
	String name;
	String cellphone;
	public Student(int id,String name,String cellphone)
	{
		this.id=id;
		this.name=name;
		this.cellphone=cellphone;		
	}
	public void print()
	{
		System.out.println("( id:"+id+",name:"+name+",cellphone:"+cellphone+" )");
	}
}


//Teacher.java
public class Teacher
{
	String name;
	String perject;
	public Teacher(String name,String perject)
	{
		this.name=name;
		this.perject=perject;
	}
	
	public void print()
	{
		System.out.println("( name:"+name+",perject:"+perject+" )");
	}
}



//Hello.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;

public class Hello
{

	private static GenericList<Student> list2;

	public static void main(String[] args)
	{
		
//		//GenericList泛型List
		//测试Student
//		GenericList<Student> list1=new GenericList<Student>(); 
//		list1.add(new Student(1,"shen","13797368760"));
//		list1.add(new Student(2,"chong","13277351070"));
//		list1.add(new Student(3,"wang","13872261912"));
//		list1.add(new Student(4,"xue","13797368760"));
//		list1.add(new Student(5,"fei","13797368760"));
//		//输出
//		GenericList.Iterator<Student> iter=list1.getIterator();
//			while(iter.hasNext())
//				iter.next().print();
//		//测试Teacher
//		GenericList<Teacher> list2 = new GenericList<Teacher>(); 
//		list2.add(new Teacher("shen","语文"));
//		list2.add(new Teacher("chong","数学"));
//		list2.add(new Teacher("wang","英语"));
//		list2.add(new Teacher("xue","物理"));
//		list2.add(new Teacher("fei","化学"));
//		//输出
//		GenericList.Iterator<Teacher> iter=list2.getIterator();
//			while(iter.hasNext())
//				iter.next().print();
		
		
//		//ArrayList
//		ArrayList<Student> list=new ArrayList<Student>();
//		list.add(new Student(1,"shen","13797368760"));
//		list.add(new Student(2,"chong","13277351070"));
//		list.add(new Student(3,"wang","13872261912"));
//		list.add(new Student(4,"xue","13797368760"));
//		list.add(new Student(5,"fei","13797368760"));
//		//定义一个容器
//		Comparator<Student> compare=new Comparator<Student>()
//		{
//
//			@Override
//			public int compare(Student a, Student b)
//			{
//				if(a.id>b.id) return -1;
//				if(a.id<b.id) return 1;
//				return 0;
//			}
//		};
//		//排序(提供一个比较器比较)
//		Collections.sort(list, compare);
//		
////		//第一种遍历方法(编程效率高)
////		for(int i=0;i<list.size();i++)
////		{
////			Student s=list.get(i);
////			s.print();
////		}
//		//第二种遍历方法(运行效率高)
//		Iterator<Student> iter=list.iterator();
//		while(iter.hasNext())
//			iter.next().print();
		
		//HashMap
		HashMap<Integer,Student> list3=new HashMap<Integer,Student>();
		list3.put(1,new Student(1,"shen","13797368760"));
		list3.put(2,new Student(2,"chong","13277351070"));
		list3.put(3,new Student(3,"wang","13872261912"));
		list3.put(4,new Student(4,"xue","13797368760"));
		list3.put(5,new Student(5,"fei","13797368760"));
		//输出(利用key查找)
		for(int i=list3.size();i>0;i--)
			list3.get(i).print();
	}

}

感谢您的观看!欢迎评论!                                

猜你喜欢

转载自blog.csdn.net/biggerchong/article/details/82925728