Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1607084207 张阳 #22

Open
annyangya opened this issue Nov 23, 2017 · 2 comments
Open

1607084207 张阳 #22

annyangya opened this issue Nov 23, 2017 · 2 comments

Comments

@annyangya
Copy link

package Sort.src.BaseSort;

public class BaseSort {
public void sort(int []a)
{
System.out.println("排序算法");
}
}

package Sort.src.BaseSort;

public class InsertSort extends BaseSort{
public void sort(int []a,int n)
{
for(int i=0;i<n;i++)
{
for(int j=i;j>0;j--)
{
if(a[j]<a[j-1])
{
swap(a,j,j-1);
}
else
break;
}
}
}
public void swap(int []a,int i,int j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}

}
package Sort.src.BaseSort;

public class QuickSort extends BaseSort{
public int getElem(int []a,int l,int r)
{
int j=l;
for(int i=l+1;i<=r;i++)
{
if(a[i]<a[j])
{
swap(a,j+1,i);
}
}
return j;
}
public void swap(int []a,int i,int j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
public void sort(int []a,int n)
{
int m=getElem(a,0,n-1);
getElem(a,0,m-1);
getElem(a,m+1,n-1);
}
}
package Sort.src.BaseSort;

public class SelectSort extends BaseSort {
public void sort(int []a,int n)
{
for(int i=0;i<n;i++)
{
int min=i;
for(int j=i+1;j<n;j++)
{
if(a[min]>a[j])
{
min=j;
}
}
swap(a,i,min);
}
}
public void swap(int []a,int i,int j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
package Sort.src.BaseSort;

import java.util.Scanner;

public class Text {

public static void main(String[] args) {
int[] a=new int[10];
Scanner scanner=new Scanner(System.in);
for(int i=0;i<10;i++)
{
a[i]=scanner.nextInt();
}
System.out.println("选择排序:");
SelectSort one=new SelectSort();
one.sort(a,10);
for(int i=0;i<10;i++)
{
System.out.print(a[i]+",");
}
System.out.println();
System.out.println("选择排序:");
InsertSort two=new InsertSort();
two.sort(a,10);
for(int i=0;i<10;i++)
{
System.out.print(a[i]+",");
}
System.out.println();
System.out.println("快速排序:");
QuickSort three=new QuickSort();
three.sort(a,10);
for(int i=0;i<10;i++)
{
System.out.print(a[i]+",");
}
System.out.println();

}
}

@annyangya
Copy link
Author

第二次作业

单例模式:只能产生一个实例

`package Design.src.People;

public class People {
private int age;
private String name;
private double weight;

private People()//构造方法必须是private访问权限,禁止从类外部调用构造方法,只能产生一个实例
{
	
}

private People(int age, String name, double weight) {
	super();
	this.age = age;
	this.name = name;
	this.weight = weight;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public double getWeight() {
	return weight;
}
public void setWeight(double weight) {
	this.weight = weight;
}

private static People people1=new People(12,"ann",45.6);
public static People getPeople1()//通过该方法可以从类外部访问到类的实例
{
	return people1;
}
private static People people2=new People();
public static People getPeople2()
{
	return people2;
}
public String toString()
{
	return "\n姓名:"+this.getName()+"\n年龄:"+this.getName()+"\n体重:"+this.getWeight();
}

}
package Design.src.People;

public class PeopleText {

public static void main(String[] args) {
	People a=People.getPeople1();//调用该方法时,生成了唯一的一个实例
	System.out.println("a的个人信息为:"+a);
	People b=People.getPeople1();
	System.out.println("b的个人信息为:"+b);
	People c=People.getPeople2();
	People d=People.getPeople2();
	System.out.println("c==d的结果为:"+(c==d));//判断两个实例是否相等
	System.out.println("c的个人信息为:"+c);
	

}

}

`

为什么有了接口还要使用抽象类

1.一个类可以实现多个接口,同时要实现接口里所有的方法,但是一个类只能继承一个类,不能是多继承
2.一个类继承了另一个抽象类后,可以不用实现该抽象类中所有的方法,但是这个类必须也要是抽象类
3.接口中规定了必须有某些方法,如果一个类实现了该接口,也就是说要实现这个接口中规定的方法
4.一个类继承另一个抽象类时,不需要必须实现哪一个方法

@annyangya
Copy link
Author

第三次培训

Foreach

`package BianLi;

import java.util.Collection;
import java.util.HashSet;

public class Foreach {

public static void main(String[] args) {
	Collection book=new HashSet();
	book.add("book1");
	book.add("book2");
	book.add("book3");

// for(Object obj:book)
// {
// //System.out.println(obj);
// String a=(String)obj;
// System.out.println(a);
// }
for(Object obj:book)
{
System.out.println(obj);
}
}

}
`

Iterator

`package Demo1.src.Collection;

public class catSet {
private String name;
private int month;
private String species;
private double weight;
public catSet(String name, int month, String species, double weight) {
super();
this.name = name;
this.month = month;
this.species = species;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}

public String toString()
{
	return "姓名:"+this.getName()+"\t年龄:"+this.getMonth()+"\t种类:"+this.getSpecies()+"\t体重:"+this.getWeight();
}
@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + month;
	result = prime * result + ((name == null) ? 0 : name.hashCode());
	result = prime * result + ((species == null) ? 0 : species.hashCode());
	long temp;
	temp = Double.doubleToLongBits(weight);
	result = prime * result + (int) (temp ^ (temp >>> 32));
	return result;
}
@Override
public boolean equals(Object obj) {
	if(this==obj)
		return true;
	if(obj.getClass()==catSet.class)
	{
		catSet cat=(catSet)obj;
		//return cat.getName().equals(name)&&cat.getMonth().equals(month)&&cat.getSpecies().equals(species)&&cat.getWeight().equals(weight);
		return cat.getName().equals(name)&&(cat.getMonth()==month)&&cat.getSpecies().equals(species)&&(cat.getWeight()==weight);
	}
	return false;
	
}

}
package Demo1.src.Collection;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class catSetText {

public static void main(String[] args) {
	catSet huahua=new catSet("花花",1,"短毛猫",2.3);
	catSet honghong=new catSet("红红",4,"田园猫",3.4);
	catSet mingming=new catSet("明明",2,"斑点猫",2);
	Set<catSet> set=new HashSet<catSet>();
	set.add(huahua);
	set.add(honghong);
	set.add(mingming);
	Iterator it=set.iterator();
	System.out.println("小猫的信息为:");
	while(it.hasNext())
	{
		System.out.println(it.next());//要重写toString方法,否则输出的是地址值
	}
	catSet honghong2=new catSet("红红",4,"田园猫",3.4);
	set.add(honghong2);
	Iterator it2=set.iterator();
	System.out.println("增加红红二号的结果:");
	while(it2.hasNext())
	{
		System.out.println(it2.next());//添加信息相同的猫没有报错,所以要重写equals方法判断猫的信息是否相同
		
	}
	catSet huahua2=new catSet("花花二号",3,"田园猫",4.5);
	set.add(huahua2);
	set.remove(huahua);
	if(set.contains(huahua))
		System.out.println("花花找到了");
	else
		System.out.println("花花没找到");
	Iterator it3=set.iterator();
	while(it.hasNext())
	{
		System.out.println(it.next());
	}

}

}
`

lambda

package BianLi;

import java.util.HashSet;
import java.util.Set;

public class lambda {

public static void main(String[] args) {
	Set books=new HashSet();
	books.add("java book");
	books.add("c book");
	books.add("c# book");
	System.out.println("遍历结果:");
	books.forEach(obj->System.out.println(obj));

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant