数组的传递问题

coconever 2014-05-02 01:48:24
public class Times
{
static int[] array = {5,2,4,1,10,9,6,3,7,8};
public static void main(String[] args)
{
long start_time = 0;long end_time = 0;
start_time = System.currentTimeMillis();
sort_mean1(array);
end_time = System.currentTimeMillis();
System.out.println("The time is:"+(start_time-end_time)+"ms");
}

static void sort_mean1(int[] a)
{
int[] b = a;
int j = 0;
int key = 0;
for(int i = 1;i<=b.length;i++)
{
key = b[i];
j = i-1;
while(j>0 && b[j]>key)
{
b[j+1] = b[j];
j = j-1;
}
b[j+1] = key;
}
System.out.println(b[10]);
}
}


上面代码为什么运行是出行java.lang.ArrayIndexOutOfBoundsException
...全文
110 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kenzson 2014-05-02
  • 打赏
  • 举报
回复
public class Times {
	static int[] array = { 5, 2, 4, 1, 10, 9, 6, 3, 7, 8 };

	public static void main(String[] args) {
		long start_time = 0;
		long end_time = 0;
		start_time = System.currentTimeMillis();
		sort_mean1(array);
		end_time = System.currentTimeMillis();
		System.out.println("The time is:" + (start_time - end_time) + "ms");
	}

	static void sort_mean1(int[] a) {
		int[] b = a;
		int j = 0;
		int key = 0;
		for (int i = 1; i <= b.length; i++) {
			key = b[i]; //主要错在这块,i不可以大于等于b数组的长度
			j = i - 1;
			while (j > 0 && b[j] > key) {
				b[j + 1] = b[j];
				j = j - 1;
			}
			b[j + 1] = key; //这里
		}
		System.out.println(b[10]); //还有这里,不能大于9
	}
}
Kenzson 2014-05-02
  • 打赏
  • 举报
回复
	static void sort_mean1(int[] a) {
		int[] b = a;
		int j = 0;
		int key = 0;
		for (int i = 1; i <= b.length; i++) {
			key = b[i]; //主要错在这块,i不可以大于等于b数组的长度
			j = i - 1;
			while (j > 0 && b[j] > key) {
				b[j + 1] = b[j];
				j = j - 1;
			}
			b[j + 1] = key;
		}
		System.out.println(b[10]);

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧