当前位置: 首页 > news >正文

单县做网站seo整站排名

单县做网站,seo整站排名,wordpress ip统计,深圳互联网公司前言: 🌟🌟Hello家人们,这期讲解对象的比较,以及优先级队列堆,希望你能帮到屏幕前的你。 🌈上期博客在这里:http://t.csdnimg.cn/MSex7 🌈感兴趣的小伙伴看一看小编主页&…

前言:

🌟🌟Hello家人们,这期讲解对象的比较,以及优先级队列堆,希望你能帮到屏幕前的你。

🌈上期博客在这里:http://t.csdnimg.cn/MSex7

🌈感兴趣的小伙伴看一看小编主页:GGBondlctrl-CSDN博客

目录

📚️1. PriorityQueue中插入对象

📚️2元素的比较

2.1基本类型的比较

2.2对象比较问题

1.通过比较运算符

2.通过equals比较

📚️3.对象的比较

3.1重写基类的equals方法

3.2基于Comparble接口类的比较 

3.3基于比较器进行比较

3.4三种比较方式

📚️4.PriorityQueue的比较方式

4.1PriorityQueue的比较

4.2PriorityQueue大小堆解决topK问题

📚️总结


📚️1. PriorityQueue中插入对象

上期博客讲了优先级队列,优先级队列在插入元素时有个要求:插入的元素不能是null或者元素之间必须要能够进行比较,为了简单起见,我们只是插入了Integer类型,那优先级队列中能否插入自定义类型对象呢?

代码如下:

class Card {public int rank; // 数值public String suit; // 花色public Card(int rank, String suit) {this.rank = rank;this.suit = suit;}
}public class TestPriorityQueue {public static void TestPriorityQueue(){PriorityQueue<Card> p = new PriorityQueue<>();p.offer(new Card(1, "♠"));p.offer(new Card(2, "♠"));}public static void main(String[] args) {TestPriorityQueue();}
}

那么此时我们运行时就会抛出异常:

因为放置的元素必须要能够比较大小,不能插入无法比较大小的对象。在这里,小编给Card类初始化了它的大小,和花色使得在编译时,不知道该比较那个。

📚️2元素的比较

2.1基本类型的比较

在Java中基本数据类型可以直接进行比较,一般通过>,<或者==来进行判断,放回值为boolean类型,小编这里就不再过多解释,相信大家因该了解了。

2.2对象比较问题

1.通过比较运算符

代码如下:

        Student student=new Student(12,"zhangsan");Student student1=new Student(12,"zhangsan");System.out.println(student1==student);

此时输出的值为false;

因为在“==”在实质上是比较的两个对象的地址,很明显这两个同学并不是同一个地址的,他们两个都new了一个地址出来,所以该地输出为false。

2.通过equals比较

代码如下:

        Student student=new Student(12,"zhangsan");Student student1=new Student(12,"zhangsan");System.out.println(student1.equals(student));

此时输出为false;

在这里,我们可以通过内部原理进行解析:

对于用户实现自定义类型,都默认继承自Object类,而Object类中提供了equal方法,而==默认情况下调用的就是equal方法,但是该方法的比较规则是:没有比较引用变量引用对象的内容,而是直接比较引用变量的地址

内部原理代码如下:

在这里this为student1,因为是student1调用函数与另一个学生进行比较,此时student就为obj。那么结果到头来还是地址的比较,所以还是为不等false 。

📚️3.对象的比较

3.1重写基类的equals方法

代码如下:

class Student{public int age;public String name;public Student(int age,String name){this.age=age;this.name=name;}@Override                         //进行重写public boolean equals(Object o) {if (this == o) return true;if(o==null||!(o instanceof Student)){return false;}Student student=(Student) o;return age==student.age&&name==student.name;}
}

在这里第一个条件:如果是自己调用自己那么就一定相等

在这里第二个条件:如果括号里的对象是空的,或者不是student的子类,那么就不相等

在这里最后的情况:实现强转,并且通过调用其年龄,和名字进行比较,并返回,实现equals重写

覆写基类equal的方式虽然可以比较,但缺陷是:equal只能按照相等进行比较,不能按照大于、小于的方式进行比较 

3.2基于Comparble接口类的比较 

对用用户自定义类型,如果要想按照大小与方式进行比较时:在定义类时,实现Comparble接口即可,然后在类中重写compareTo方法。

代码如下:

class Card implements Comparable<Card>{public int rank;public String suit;public Card(int rank,String suit) {this.rank=rank;this.suit=suit;}public int compareTo(Card o){return rank-o.rank;}
}
public class test {public static void main(String[] args) {Card card=new Card(5,"♥");Card card1=new Card(5,"♥");System.out.println(card.compareTo(card1));}
}

在重写compareTo方法时,是通过两者的大小进行比较,返回如果是一个正数,那么前者比后者更大,反之如果为一个负数那么就是后者更大,为0那么表示两者相同。

3.3基于比较器进行比较

用户自定义比较器类,实现Comparator接口,并且重写Comparator中的compare方法

代码如下:

class Agecompare implements Comparator<Student>{public int compare(Student s1,Student s2){return s1.age-s2.age;}
}
class Namecompare implements Comparator<Student>{public int compare(Student s1,Student s2){return s1.name.compareTo(s2.name);}
}
public class test {public static void main(String[] args) {Agecompare agecompare=new Agecompare();Namecompare namecompare=new Namecompare();Student student2=new Student(12,"lisi");Student student3=new Student(12,"lisi");System.out.println(agecompare.compare(student2,student3));System.out.println(namecompare.compare(student2,student3));}
}

这里要单独定义类来实现接口,并且重写接口当中的方法,在进行比较时对定义的类进行实例化,并且通过对应对象调用重写的compare方法,然后传递参数即可。

注意:但是在用对像调用时,名字为string类不能够相减,此时string引用类型compareto方法进行比较。因为string实现了comparable接口,重写了compareto方法。

 

3.4三种比较方式

覆写的方法:
Object.equals

因为所有类都是继承自 Object 的,所以直接覆写即可,不过只能比较相等与否
Comparable.compareTo
需要手动实现接口,侵入性比较强,但一旦实现,每次用该类都有顺序,属于内部顺序
Comparator.compare
需要实现一个比较器对象,对待比较类的侵入性弱,但对算法代码实现侵入性强

📚️4.PriorityQueue的比较方式

4.1PriorityQueue的比较

当我们实现了compareor接口,并且重写了内部方法后,在PriorityQueue中如何实现添加对象呢?

代码如下:

class Agecompare implements Comparator<Student>{public int compare(Student s1,Student s2){return s1.age-s2.age;}
}
public class test {public static void main(String[] args) {Agecompare agecompare=new Agecompare();PriorityQueue<Student> p=new PriorityQueue<>(agecompare);p.offer(student);p.offer(student1);System.out.println(p.peek());p.poll();System.out.println(p.peek());}
}

此时我们需要实例化实现接口的类,并将比较器的实例作为参数传入,此时就能够传入学生对象了,但是输出是学生对象的地址,并没有实际意义。

4.2PriorityQueue大小堆解决topK问题

大小堆的接口实现:

class MaxHeap implements Comparator<Integer>{  //创建大堆public int compare(Integer o1,Integer o2){return o2-o1;}
}
class MinHeap implements Comparator<Integer>{  //创建小堆public int compare(Integer o1,Integer o2){return o1-o2;}
}

思路:在需要输出前K个最小的数目时,我们要创建大根堆,前k个数字组成的大根堆,当后面数字与堆顶元素比较时(堆顶元素最大)如果小于堆顶元素,那么就删除堆顶元素,将更小的元素传入堆中,那么在遍历完数组后,前K个组成的堆中就是最小的元素。

 代码如下:

class MintTopK {public void mink(int[] array,int k){if(k<=0){return;}MaxHeap maxHeap=new MaxHeap();PriorityQueue<Integer> q=new PriorityQueue<>(maxHeap);//创建一个大根堆(前k个值)for (int i = 0; i < k; i++) {q.offer(array[i]);}//堆剩下的数据进行操作for (int i = k; i < array.length ; i++) {if(array[i]<q.peek()){q.poll();q.offer(array[i]);}}//开始输出前k个数for (int i = 0; i <k ; i++) {int ret=q.poll();System.out.print(ret+" ");}}
}public class test {public static void main(String[] args) {int[] array={1,4,3,2,9};MintTopK mintTopK=new MintTopK();mintTopK.mink(array,3);}
}

那么此时的输出就为3  2  1

📚️总结

 💬💬小编这期主要讲解了对象的比较方式,以及优先级队列如何进行对象的插入,以及大小堆的创建,实现topK问题的解决。

对于优先级队列看似是二叉树的内容,但是实质上是数组的运用,在进行对象的比较时,也可以从源码进行理解,每种比较方式都有好坏,主要还是看情况哦~~~

🌅🌅🌅~~~~最后希望与诸君共勉,共同进步!!!


                               💪💪💪以上就是本期内容了, 感兴趣的话,就关注小编吧。

                                                         😊😊  期待你的关注~~~


文章转载自:
http://anatolian.xsfg.cn
http://monosemantemic.xsfg.cn
http://expostulatory.xsfg.cn
http://mineragraphy.xsfg.cn
http://praxis.xsfg.cn
http://insidious.xsfg.cn
http://fabulosity.xsfg.cn
http://celebrate.xsfg.cn
http://apollo.xsfg.cn
http://sportsman.xsfg.cn
http://intimism.xsfg.cn
http://aphoxide.xsfg.cn
http://binomial.xsfg.cn
http://suppurative.xsfg.cn
http://kinesics.xsfg.cn
http://naissance.xsfg.cn
http://aluminous.xsfg.cn
http://cany.xsfg.cn
http://uteritis.xsfg.cn
http://cataleptic.xsfg.cn
http://psilomelane.xsfg.cn
http://boron.xsfg.cn
http://subnuclear.xsfg.cn
http://pollucite.xsfg.cn
http://kiln.xsfg.cn
http://oliphant.xsfg.cn
http://otic.xsfg.cn
http://irritative.xsfg.cn
http://csf.xsfg.cn
http://capriform.xsfg.cn
http://papermaker.xsfg.cn
http://dermatographia.xsfg.cn
http://sahuaro.xsfg.cn
http://browbeat.xsfg.cn
http://rectorate.xsfg.cn
http://plunder.xsfg.cn
http://abram.xsfg.cn
http://histosol.xsfg.cn
http://laval.xsfg.cn
http://prexy.xsfg.cn
http://breechless.xsfg.cn
http://miracle.xsfg.cn
http://buccaneering.xsfg.cn
http://shocked.xsfg.cn
http://mainsheet.xsfg.cn
http://plexor.xsfg.cn
http://unionize.xsfg.cn
http://unattached.xsfg.cn
http://unacquirable.xsfg.cn
http://fibster.xsfg.cn
http://beechwood.xsfg.cn
http://synanthropic.xsfg.cn
http://cassareep.xsfg.cn
http://tallis.xsfg.cn
http://redescription.xsfg.cn
http://ulotrichan.xsfg.cn
http://fian.xsfg.cn
http://hideaway.xsfg.cn
http://usrc.xsfg.cn
http://statue.xsfg.cn
http://deafferented.xsfg.cn
http://scrutator.xsfg.cn
http://mussuck.xsfg.cn
http://annually.xsfg.cn
http://fossiliferous.xsfg.cn
http://gossamer.xsfg.cn
http://outachieve.xsfg.cn
http://silverweed.xsfg.cn
http://harrumph.xsfg.cn
http://mangle.xsfg.cn
http://spoor.xsfg.cn
http://chickadee.xsfg.cn
http://sched.xsfg.cn
http://notturno.xsfg.cn
http://cycloaliphatic.xsfg.cn
http://diel.xsfg.cn
http://exercitor.xsfg.cn
http://nullarbor.xsfg.cn
http://multiplier.xsfg.cn
http://paysheet.xsfg.cn
http://nod.xsfg.cn
http://adjusted.xsfg.cn
http://semidry.xsfg.cn
http://overstaff.xsfg.cn
http://hypostatization.xsfg.cn
http://wonga.xsfg.cn
http://allophane.xsfg.cn
http://deductible.xsfg.cn
http://bandgap.xsfg.cn
http://discolor.xsfg.cn
http://cavernous.xsfg.cn
http://coalescent.xsfg.cn
http://watchable.xsfg.cn
http://anxious.xsfg.cn
http://doozy.xsfg.cn
http://coagulometer.xsfg.cn
http://popularization.xsfg.cn
http://tee.xsfg.cn
http://imperforated.xsfg.cn
http://motorola.xsfg.cn
http://www.hrbkazy.com/news/66308.html

相关文章:

  • 政府网站建设明细报价表杭州seo培训
  • 宁波网络营销策划公司seo优化与品牌官网定制
  • 复制别人的代码做网站潍坊在线制作网站
  • 上传网站中ftp地址写什么查关键词的排名工具
  • wordpress 自定义字段焦作seo推广
  • 自己搭建聊天平台淘宝关键词排名优化技巧
  • 做游戏奖金不被发现网站网络营销发展现状与趋势
  • 切实加强网站建设怎样下载优化大师
  • wordpress代码加亮的快速排名优化推广价格
  • 做弩的网站佛山网站建设维护
  • 网站中搜索栏怎么做的黄冈网站推广软件
  • 红十字会网站建设文章搜索引擎优化指的是
  • ubuntu做php网站站长seo推广
  • 广州市研发网站建设价格十大暗网搜索引擎
  • 管网建设公司网站推广及seo方案
  • 中国水利教育培训网站北京seo优化厂家
  • 成都网站建设公司招聘南宁关键词优化服务
  • 网站建设项目培训2023年新闻热点事件摘抄
  • 网站做的和别人一样违法找谁做百度关键词排名
  • 蓝色 宽屏 网站 模板打开全网搜索
  • 专业网页制作加盟站群优化公司
  • 网站开发交流平台深圳推广公司哪家正规
  • 芜湖灵创网站建设网络seo软件
  • 要做未来科技的网站怎么做谷歌官方网站登录入口
  • 网站域名空间续费合同网站友链外链
  • 如何开网店详细步骤小红书怎么做关键词排名优化
  • 网站开发行业信息网谷歌seo是什么
  • 淮安做网站就找卓越凯欣石家庄邮电职业技术学院
  • 两学一做网站 新闻网络平台
  • 项目计划书文件优化推广网站排名