java.lang.NoSuchMethodError: main Exception in thread "main"

发布于 2022-09-06 08:35:08 字数 1230 浏览 16 评论 0

Possible Duplicate:
Exception in thread “main” java.lang.NoSuchMethodError: main

My main syntax is correct. What else could be the problem?

public class BuildHeap
{       
  int a[]={1,2,6,3,5,1,7,8,4,9};

  public void build()
  {
      for(int i=5;i<=1;i--)
      {
         heapify(a,i);
      }
  }

  public void heapify(int a[],int i)
  { 
    System.out.print("hello");
    int j,temp,rchild,lchild;
    if(i<5)
     {
        if(2*i<5)
            lchild=a[(2*i)+1];
        if((2*i)+1<4)
            rchild=a[(2*i)+2];

        if(lchild>rchild)
            j=(2*i)+1;
        else
            j=(2*i)+2;

        if(a[i]<a[j])
         {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
            heapify(a,j);

} } }

class Heap
{
    public static void main(String[] args) 
    {
        BuildHeap bh=new BuildHeap();
        bh.build();
        for(int i=0;i<10;i++)
            System.out.print(bh.a[i]+" ");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聆听风音 2022-09-13 08:35:08

You need to put the main method in the public class BuildHeap (not the package-private class Heap). Or maybe you just specified the wrong class name on the command line. Again, main is in Heap, not BuildHeap.

╭⌒浅淡时光〆 2022-09-13 08:35:08

The most probable cause should be that your class name and file name doesnot match.
Create a new file containing the class Heap and have the filename as Heap.java.
Keep BuildHeap class in a different file in the same package. Run Heap. It should work.
Else change the BuildHeap class from public class and make the Heap class as public.

The better way would be to separate the classes in different files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文