I am having problem with compilation that I don't understand and would appreciate some help. Excerpt from the code, other methods are omitted for brevity:
public class TableBuilder {
List<String[]> rows = new LinkedList<String[]>();
public void sortByColumn(final int colIndex) {
int maxIdx = 0;
for (String[] s : rows) maxIdx = Math.max(maxIdx, s.length - 1);
if (colIndex < 0 || colIndex >= maxIdx) return
Collections.sort(rows, new Comparator<String[]>() {
@Override
public int compare(String[] strings, String[] otherStrings) {
return strings[colIndex].compareTo(otherStrings[colIndex]);
}
});
}
}
Error that I receive is:
cannot return a value from method whose result type is void
Collections.sort(rows ,new Comparator<String[]>() {
^
When I change method signature to public int sortByColumn(final int colIndex)
then I am getting error:
incompatible types
found : void
required: int
Collections.sort(rows, new Comparator<String[]>() {
^
Why this code doesn't compile and how is return
in anonymous Comparator
class related to sortByColumn
signature? Is it possibly compiler bug or what do I do wrong? Using javac 1.6.0_45.
答案 0 :(得分:1)
您的比较者很好。问题是您尝试$('.fc-today-button').click(function () {
$('#calendar').fullCalendar('gotoDate', currentDate);
});
return
的结果Comparator#sort
。void
。只要丢失return
语句就可以了:
if (colIndex > 0 && colIndex <= maxIdx) {
Collections.sort(rows, new Comparator<String[]>() {
@Override
public int compare(String[] strings, String[] otherStrings) {
return strings[colIndex].compareTo(otherStrings[colIndex]);
}
});
}
答案 1 :(得分:0)
我建议删除return
语句,以便代码编译:
public class TableBuilder {
List<String[]> rows = new LinkedList<String[]>();
public void sortByColumn(final int colIndex) {
int maxIdx = 0;
for (String[] s : rows) maxIdx = Math.max(maxIdx, s.length - 1);
if (colIndex < 0 || colIndex >= maxIdx) {
Collections.sort(rows, new Comparator<String[]>() {
@Override
public int compare(String[] strings, String[] otherStrings) {
return strings[colIndex].compareTo(otherStrings[colIndex]);
}
});
}
}
}
Collections.sort
方法会将rows
集合排序为可变对象。它不会将已排序的集合作为新对象返回。
答案 2 :(得分:0)
这是由BackSlash
突出显示的拼写错误