当前位置: 首页 > 工具软件 > Java-Sparrow > 使用案例 >

Java-week9-泛型类

乐宜民
2023-12-01

1. 泛型类


public class Cone<E> {
	double height;
	E bottom;///只能调用object中的类
	public Cone(E b)
	{
		bottom=b;
	}
	public void setHeight(double h)
	{
		height=h;
	}
	public double computerVolume()
	{
		String s=bottom.toString();
		double area=Double.parseDouble(s);
		return 1.0/3.0*area*height;
	}
}


public class Rect {
	double sideA,sideB,area;
	Rect(double a,double b)
	{
		sideA=a;
		sideB=b;
	}
	public String toString()
	{
		area=sideA*sideB;
		return " "+area;
	}

}



public class Circle {
	double area,radius;
	Circle(double r)
	{
		radius=r;
	}
	public String toString()
	{
		area=radius*radius*Math.PI;
		return " "+area;
	}

}

import java.util.Scanner;
public class test {

	public static void main(String[] args) {
		Circle circle=new Circle(10);
		Cone<Circle>coneone=new Cone<Circle>(circle);
		coneone.setHeight(16);
		System.out.println(coneone.computerVolume());
		Rect rect=new Rect(15,23);
		Cone<Rect>coneTwo=new Cone<Rect>(rect);
		coneTwo.setHeight(98);
		System.out.println(coneTwo.computerVolume());
	}

}

2.集合

import java.util.Scanner;
import java.util.*;
public class test {
	public static void main(String[] args) {
		List<String>list=new LinkedList<String>();
		for(int i=0;i<=6;i++)
		{
			list.add("speed"+i);
		}
		Iterator<String>iter=list.iterator();
		while(iter.hasNext())
		{
			String te=iter.next();
			System.out.println(te);
		}
		for(int i=0;i<list.size();i++)
		{
			String teString=list.get(i);
			System.out.println(teString);
		}
	}

}
import java.util.Scanner;
import java.util.*;
public class test {
	public static void main(String[] args) {
		List<String>list=new ArrayList<String>();
		list.add("melon");
		list.add("apple");
		list.add("pear");
		list.add("banana");
		Collections.sort(list);
		for(int i=0;i<list.size();i++)
			System.out.println(list.get(i));
	}

}
//从小到大
import java.util.*;
public class Student implements Comparable{
	//按照学生身高排序
	int height=0;
	String name;
	Student(String n,int h)
	{
		name=n;
		height=h;
	}
	public int compareTo(Object b)
	{
		//两个Student对象相等当且仅当二者的height值相等
		Student st=(Student)b;
		return (this.height-st.height);
	}

}
import java.util.*;
public class test {

	public static void main(String[] args) {
		List<Student>list=new LinkedList<Student>();
		list.add(new Student("tmc",190));
		list.add(new Student("李四",188));
		list.add(new Student("周五",198));
		Iterator<Student>iter=list.iterator();
		System.out.println("排序前,链表中的数据");
		while(iter.hasNext())
		{
			Student stu=iter.next();
			System.out.println(stu.name+"身高:"+stu.height);
		}
		Collections.sort(list);
		System.out.println("排序后,链表中的数据");
		iter=list.iterator();
		while(iter.hasNext())
		{
			Student stu=iter.next();
			System.out.println(stu.name+"身高:"+stu.height);
		}
		Student zhaolin=new Student("zhaolin",188);
		int index = Collections.binarySearch(list, zhaolin,null);
		if(index>=0)
		{
			System.out.println(zhaolin.name+"和链表中"+list.get(index).name+"身高相同");
		}
	}

}
//斐波拉切数的求解
import java.util.*;
public class test {
	public static void main(String[] args) {
		Stack<Integer>stack = new Stack<Integer>();
		stack.push(new Integer(1));
		stack.push(new Integer(1));
		int k=1;
		while(k<=10)
		{
			for(int i=1;i<=2;i++)
			{
				Integer F1=stack.pop();
				int f1=F1.intValue();
				Integer F2=stack.pop();
				int f2=F2.intValue();
				Integer temp=new Integer(f1+f2);
				System.out.println(" "+temp.toString());
				stack.push(temp);
				stack.push(F2);
				k++;
			}
		}
	}

}
//去除重复的数据
import java.util.*;
public class test {
	public static void main(String[] args) {
		HashSet<String>hashset=new HashSet<String>();
		hashset.add("two");
		hashset.add("one");
		hashset.add("four");
		hashset.add("one");
		System.out.println(hashset.size());
		Iterator<String>iterator=hashset.iterator();
		while(iterator.hasNext())
		{
			System.out.println(iterator.next());
		}
	}

}
//英汉小词典
import java.util.*;
public class test {
	public static void main(String[] args) {
		HashMap<String,String>hashtable=new HashMap<String,String>();
		hashtable.put("grandness", "伟大");
		hashtable.put("swim", "游泳");
		hashtable.put("sparrow", "麻雀");
		hashtable.put("boy", "男孩");
		hashtable.put("sun", "太阳");
		Scanner in = new Scanner(System.in);
		while(true)
		{
			System.out.print("请输入英文单词");
			String s=in.next();
			if(!s.equals("end"))
				System.out.printf("汉语解释是:%s\n",hashtable.get(s));
			else 
				{
				  System.out.printf("exit");
				   break;
				}
		}
	}

}

 

 类似资料: