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]+" ");
}
}
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
.
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.
精彩评论