亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java編程問題

java編程問題

小濤b 2017-03-06 14:15:03
package demo;import java.util.Arrays;public class HelloWorld { public static void main(String[] args){ HelloWorld hello=new HelloWorld(); int[] score={89 , -23 , 64 , 91 , 119 , 52 , 73}; hello.test(score); } public void test(int[]score){ Arrays.sort(score); int number=0; for(int i=score.length-1;i>=0;i--){ if(score[i]<=0 || score[i]>=100){ continue; } number++; if(number>3){ break; } System.out.println(score[i]); } } }?for循環之前雖然經過sort方法進行排序過. 但是for循環為什么 i=score.length還要-1? 看不明白. 求解答
查看完整描述

3 回答

?
慕粉3233872

TA貢獻70條經驗 獲得超29個贊

數組下標是從0開始排序的。

例如int[] score={89 , -23 , 64 , 91 , 119 , 52 , 73};

score[0]=89,score[1]=-23,score[6]=73.

length是獲取數組長度的,從1開始數,這里的score.length=7

所以score[6]=score[score.length-1]=73

查看完整回答
1 反對 回復 2017-03-06
?
yanrun

TA貢獻317條經驗 獲得超240個贊

這個和有沒有排序是無關的,i=score.length-1是因為數組的下標是從0開始的,也就是說你的score數組的下標是從0到6的,而數組的長度為7,如果不減1,就會出現score[7],而數組中并不存在這個元素,會拋出數組下標越界異常

查看完整回答
1 反對 回復 2017-03-06
  • 3 回答
  • 0 關注
  • 1367 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號