• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about Comparable [ ]  RSS feed

 
Julie Robinson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to define a method that returns a Comparable []. My return statement is return a[i];. However, I am getting a compiling error because of incompatible types. I have declared a to be a new Comparable [] of an integer size and i is declared as an int. What am I missing?

public Comparable[] getAll() {
for(int i = 0; i < numObjects; i++) {
System.out.print(a[i] + " ");
return a[i];
}
}

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Julie,

Welcome to JavaRanch!

You're returning a[i] which is a reference to a single Comparable, not to a Comparable[]. I can't tell exactly what you want this method to do, so I can't tell if you ought to change the return type, or the return statement.

One other thing I note: as written the method won't compile because the return statement is inside the loop, and as a result, not every poble path through the function returns a value. In Java, that's not allowed.

If "getAll()" is just supposed to return the array "a", then



would do it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!