【发布时间】:2014-03-10 23:20:20
【问题描述】:
好的,我正在尝试查找数组中的最大元素,但我意识到这不是最好的方法,它只在某些情况下有效。希望能提供一些关于如何更改我的代码以使其适用于所有实例的指示。
public static int maxArray(int[] a) {
int max = 0;
for (int j = 0; j < a.length-1; j++) {
if (a[j+1] > a[j]) {
max = a[j+1];
} else {
max = a[j];
}
}
return max;
}
【问题讨论】: