📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 1553| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

[学Java/C#] 为啥studentComparator class不能写在里面...

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
楼主在练PQ问题,深受Comparator class困扰,
来个技术流问题:StudentComparator这个class放在comparatorGeeksfGeeks这个class里面就会出现
PriorityQueue<Student> pq = new PriorityQueue<Student>(5, new StudentComparator());这个问题
报错信息是Exception in thread "main" java.lang.Error: Unresolved compilation problem: No enclosing instance of type comparatorGeeksfGeeks is accessible. Must qualify the allocation with an enclosing instance of type comparatorGeeksfGeeks (e.g. x.new A() where x is an instance of comparatorGeeksfGeeks).
看不太懂耶...
所以为啥非要拿出来才可以?下面是拿出来后才正确的代码
  1. class StudentComparator implements Comparator<Student> {

  2.         // Overriding compare()method of Comparator
  3.         // for descending order of cgpa
  4.         public int compare(Student s1, Student s2) {
  5.                 if (s1.cgpa < s2.cgpa)
  6.                         return 1;
  7.                 else if (s1.cgpa > s2.cgpa)
  8.                         return -1;
  9.                 return 0;
  10.         }
  11. }

  12. class Student {
  13.         public String name;
  14.         public double cgpa;

  15.         // A constructor
  16.         public Student(String name, double cgpa) {
  17.                 this.name = name;
  18.                 this.cgpa = cgpa;
  19.         }

  20.         public String getName() {
  21.                 return name;
  22.         }
  23. }

  24. public class comparatorGeeksfGeeks {
  25.         
  26.         
  27.         // Java program to demonstrate working of
  28.         // comparator based priority queue constructor

  29.         public static void main(String[] args) {
  30.                 Scanner in = new Scanner(System.in);
  31.                 // Creating Priority queue constructor having
  32.                 // initial capacity=5 and a StudentComparator instance
  33.                 // as its parameters
  34.                 PriorityQueue<Student> pq = new PriorityQueue<Student>(5, new StudentComparator());

  35.                 // Invoking a parameterized Student constructor with
  36.                 // name and cgpa as the elements of queue
  37.                 Student student1 = new Student("Nandini", 3.2);

  38.                 // Adding a student object containing fields
  39.                 // name and cgpa to priority queue
  40.                 pq.add(student1);
  41.                 Student student2 = new Student("Anmol", 3.6);
  42.                 pq.add(student2);
  43.                 Student student3 = new Student("Palak", 4.0);
  44.                 pq.add(student3);

  45.                 // Printing names of students in priority order,poll()
  46.                 // method is used to access the head element of queue
  47.                 System.out.println("Students served in their priority order");

  48.                 while (!pq.isEmpty()) {
  49.                         System.out.println(pq.poll().getName());
  50.                 }
  51.         }
  52. }
复制代码




上一篇:非码农,纯好奇:刷Leetcode和背GRE哪个更难些?
下一篇:Backtracking类型题的经验和攻略整理
推荐
potplus 2020-2-2 12:52:36 | 只看该作者
全局:
说个跑题的, 都0202年了写comparator就用lambda吧。。。java8都过去了10年了吧
回复

使用道具 举报

🔗
holybeast 2020-2-1 05:43:14 | 只看该作者
全局:
错误的意思感觉:如果你想把那个挪到里面,你需要把那个class设为static。
  1. static class StudentComparator implements Comparator<Student> {
  2.     // code
  3. }
复制代码

回复

使用道具 举报

🔗
 楼主| akdhfikbk 2020-2-2 11:05:18 | 只看该作者
全局:
holybeast 发表于 2020-2-1 05:43
错误的意思感觉:如果你想把那个挪到里面,你需要把那个class设为static。
[mw_shl_code=java,true]static ...

我去试试,谢谢您的回复,我觉得应该是这个问题
不过这个,technically叫什么问题..?
回复

使用道具 举报

🔗
holybeast 2020-2-2 15:14:18 | 只看该作者
全局:
akdhfikbk 发表于 2020-2-2 11:05
我去试试,谢谢您的回复,我觉得应该是这个问题
不过这个,technically叫什么问题..?

我也没具体深究过,根据我所做project,我默认都会给内部类加static。
以我目前的经验,我答不好,所以你可以看看这个知乎问答:
https://www.zhihu.com/question/28197253
回复

使用道具 举报

🔗
lifuyao 2020-2-3 03:20:45 | 只看该作者
全局:
akdhfikbk 发表于 2020-2-2 11:05
我去试试,谢谢您的回复,我觉得应该是这个问题
不过这个,technically叫什么问题..?

我觉得是因为Main函数是static的method, 是依附于主类的method, 你如果放到内部的话,如果想要调用这个comparator必须也得同一级别的实例。如果不加static,那么static的main方法作为类预加载的内容是看不到你写的这个comparator的。如果你加上static, 那个这个comparator就会预加载,你的main就能找到这个comparator.
跟之前的层主是一个意思。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表