java,能否解释一下下面代码的意思及思路.
publicclassDullArray{privateint[]array;privateintblock=0;privateintNow=0;publicDullAr...
publicclass DullArray {
private int[] array;
private int block = 0;
private int Now = 0;
public DullArray(int[] array) {
this.array = array;
}
public void setBlock(int block) {
if (block > array.length)
throw newArrayIndexOutOfBoundsException();
this.block = block;
}
public int[] nextMaxMin() {
if (block == 0)
return null;
if (Now + block > array.length)
return null;
int[] Result = { array[Now], array[Now]};
for (int i = Now; i < Now + block;i++) {
if (array[i] > Result[0])
Result[0] = array[i];
if (array[i] < Result[1])
Result[1] = array[i];
}
Now++;
return Result;
}
}
publicclass Test {
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7,8,9};
DullArray dullArray = newDullArray(array);
dullArray.setBlock(3);
int[] Result = dullArray.nextMaxMin();
while(Result != null){
System.out.println("MAX:" +Result[0]+" MIN:" +Result[1]);
Result = dullArray.nextMaxMin();
}
}
} 展开
private int[] array;
private int block = 0;
private int Now = 0;
public DullArray(int[] array) {
this.array = array;
}
public void setBlock(int block) {
if (block > array.length)
throw newArrayIndexOutOfBoundsException();
this.block = block;
}
public int[] nextMaxMin() {
if (block == 0)
return null;
if (Now + block > array.length)
return null;
int[] Result = { array[Now], array[Now]};
for (int i = Now; i < Now + block;i++) {
if (array[i] > Result[0])
Result[0] = array[i];
if (array[i] < Result[1])
Result[1] = array[i];
}
Now++;
return Result;
}
}
publicclass Test {
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7,8,9};
DullArray dullArray = newDullArray(array);
dullArray.setBlock(3);
int[] Result = dullArray.nextMaxMin();
while(Result != null){
System.out.println("MAX:" +Result[0]+" MIN:" +Result[1]);
Result = dullArray.nextMaxMin();
}
}
} 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏10(财富值+成长值)
1个回答
展开全部
任何的代码都是根据需求写出来的,这段代码的意思倒是没什么难的,从这段代码逆向推测出的需求是:给定一个int类型的数组,将数组中的部分或全部元素依次从0角标比较下去,并打印出每一次比较的最大值和最小值。
package cn.itcast.test.font;
class dullArray {
private int[] array;
private int block = 0;
private int Now = 0;
public dullArray(int[] array) {
this.array = array;
}
public void setBlock(int block) {
if (block > array.length)
throw new ArrayIndexOutOfBoundsException();
this.block = block;
}
public int[] nextMaxMin() {
if (block == 0)
return null;
if (Now + block > array.length)
return null;
//定义一个名称为Result长度为2的数组,其目的是依次将Array数组中的元素依次临时存入其中。
int[] Result = { array[Now], array[Now]};
/*
* 通过for循环将Array数组中的元素逐个和Result数组中的两个临时元素进行比较
* 将较大的元素存入Result[0],将较小元素存入Result[1],这样就得到了每一次比较
* 的最大值和最小值。
*/
for (int i = Now; i < Now + block;i++) {
if (array[i] > Result[0])
Result[0] = array[i];
if (array[i] < Result[1])
Result[1] = array[i];
}
//Now变量通过自增来改变Result数组中元素
Now++;
return Result;
}
}
public class ArrayTest {
public static void main(String[] args){
int[] array = {4,22,33,44,5,6,7};
dullArray dullArray = new dullArray(array);
dullArray.setBlock(2);
int[] Result = dullArray.nextMaxMin();
while(Result != null){
System.out.println("MAX:" +Result[0]+" MIN:" +Result[1]);
Result = dullArray.nextMaxMin();
}
}
}
package cn.itcast.test.font;
class dullArray {
private int[] array;
private int block = 0;
private int Now = 0;
public dullArray(int[] array) {
this.array = array;
}
public void setBlock(int block) {
if (block > array.length)
throw new ArrayIndexOutOfBoundsException();
this.block = block;
}
public int[] nextMaxMin() {
if (block == 0)
return null;
if (Now + block > array.length)
return null;
//定义一个名称为Result长度为2的数组,其目的是依次将Array数组中的元素依次临时存入其中。
int[] Result = { array[Now], array[Now]};
/*
* 通过for循环将Array数组中的元素逐个和Result数组中的两个临时元素进行比较
* 将较大的元素存入Result[0],将较小元素存入Result[1],这样就得到了每一次比较
* 的最大值和最小值。
*/
for (int i = Now; i < Now + block;i++) {
if (array[i] > Result[0])
Result[0] = array[i];
if (array[i] < Result[1])
Result[1] = array[i];
}
//Now变量通过自增来改变Result数组中元素
Now++;
return Result;
}
}
public class ArrayTest {
public static void main(String[] args){
int[] array = {4,22,33,44,5,6,7};
dullArray dullArray = new dullArray(array);
dullArray.setBlock(2);
int[] Result = dullArray.nextMaxMin();
while(Result != null){
System.out.println("MAX:" +Result[0]+" MIN:" +Result[1]);
Result = dullArray.nextMaxMin();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询