package helloworld;
import java.util.Arrays;
/**
*
* @author Y
*/
public class HelloWorld
{
public static void main(String[] args)
{
int[]scores={89 , -23 , 64 , 91 , 119 , 52 , 73};
HelloWorld hello=new HelloWorld();
hello.put(scores);
}
public void put(int[]score){
Arrays.sort(score);
for(int i=score.length;i>score.length-3;i--)
System.out.print(score[i]);
}
错误:!!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at helloworld.HelloWorld.put(HelloWorld.java:25)
at helloworld.HelloWorld.main(HelloWorld.java:18)
C:\Users\123\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
构建失败 (总时间: 3 秒)int i=score.length;i>score.length-3;i-- 错了,score.length应该是7,当i=7的时候,scores[7]就越界了,因为score下标最大只到6,
数组下标越界
scores.length是7 ,但是数组下标最大是6 所以应该scores.length-1 ,代码里还有一些空格的问题
越界了i=score.length 但是 0<=i<score.length
角标错了 一楼说的对
scores[i]里的i值是从零开始,int i=score.length改为int i=score.length-1,希望可以帮到你