Java数组下标越界
源代码如下:packageTest;publicclassArreyTest{publicstaticvoidmain(String[]args){int[]aaa={2...
源代码如下:
package Test;
public class ArreyTest {
public static void main(String[] args) {
int[] aaa={23,14,35,2,71,55,82,41,11,6};
Ary ay1=new Ary(aaa);
ay1.print();
ay1.order();
ay1.print();
for(int m=0;m<aaa.length;m++)System.out.print("---"+aaa[m]);
System.out.println();
Ary ay2=new Ary(ay1.subAry(2,6));
ay2.print();
}
}
class Ary{
int[] m_ary;
Ary(int[] p)
{m_ary=p;}
void print()
{
for(int i=0;i<m_ary.length;i++)
System.out.print(m_ary[i]+" ");
System.out.println();
}
void order()
{
int j,temp;
boolean exchange=true;
j=m_ary.length-2;
while(j>=0&&exchange)
{
exchange=false;
for(int i=0;i<=j;i++)
if(m_ary[i]>m_ary[i+1])
{
exchange=true;
temp=m_ary[i];
m_ary[i]=m_ary[i+1];
m_ary[i+1]=temp;
}
j=j-1;
}
}
int[] subAry(int a1,int a2){
int[] sa;
if(a1>=0&&a1<a2&&a2<m_ary.length){
sa=new int[a2-a1+1];
for(int i=0,j=a1;i<=a2;i++,j++)sa[i]=m_ary[j];
return sa;
}
else
{
System.out.println("error index");
return new int[1];
}
}
}
运行结果如下:
23 14 35 2 71 55 82 41 11 6
2 6 11 14 23 35 41 55 71 82
---2---6---11---14---23---35---41---55---71---82
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Test.Ary.subAry(ArreyTest.java:49)
at Test.ArreyTest.main(ArreyTest.java:11)
知道是下标越界,但不知道哪里错,求高手,求解释.好的话,我再加分. 展开
package Test;
public class ArreyTest {
public static void main(String[] args) {
int[] aaa={23,14,35,2,71,55,82,41,11,6};
Ary ay1=new Ary(aaa);
ay1.print();
ay1.order();
ay1.print();
for(int m=0;m<aaa.length;m++)System.out.print("---"+aaa[m]);
System.out.println();
Ary ay2=new Ary(ay1.subAry(2,6));
ay2.print();
}
}
class Ary{
int[] m_ary;
Ary(int[] p)
{m_ary=p;}
void print()
{
for(int i=0;i<m_ary.length;i++)
System.out.print(m_ary[i]+" ");
System.out.println();
}
void order()
{
int j,temp;
boolean exchange=true;
j=m_ary.length-2;
while(j>=0&&exchange)
{
exchange=false;
for(int i=0;i<=j;i++)
if(m_ary[i]>m_ary[i+1])
{
exchange=true;
temp=m_ary[i];
m_ary[i]=m_ary[i+1];
m_ary[i+1]=temp;
}
j=j-1;
}
}
int[] subAry(int a1,int a2){
int[] sa;
if(a1>=0&&a1<a2&&a2<m_ary.length){
sa=new int[a2-a1+1];
for(int i=0,j=a1;i<=a2;i++,j++)sa[i]=m_ary[j];
return sa;
}
else
{
System.out.println("error index");
return new int[1];
}
}
}
运行结果如下:
23 14 35 2 71 55 82 41 11 6
2 6 11 14 23 35 41 55 71 82
---2---6---11---14---23---35---41---55---71---82
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Test.Ary.subAry(ArreyTest.java:49)
at Test.ArreyTest.main(ArreyTest.java:11)
知道是下标越界,但不知道哪里错,求高手,求解释.好的话,我再加分. 展开
展开全部
i < a2出错,修改如下:
if (a1 >= 0 && a1 < a2 && a2 < m_ary.length) {
sa = new int[a2 - a1 + 1];//2 到6是5个元素
for (int i = 0, j = a1; i < a2-1; i++, j++)
sa[i] = m_ary[j];//a2=6,所以i< a2-1才可以
完整代码
package Test;
public class ArreyTest {
public static void main(String[] args) {
int[] aaa = { 23, 14, 35, 2, 71, 55, 82, 41, 11, 6 };
Ary ay1 = new Ary(aaa);
ay1.print();
ay1.order();
ay1.print();
for (int m = 0; m < aaa.length; m++)
System.out.print("---" + aaa[m]);
System.out.println();
Ary ay2 = new Ary(ay1.subAry(2, 6));
ay2.print();
}
}
class Ary {
int[] m_ary;
Ary(int[] p) {
m_ary = p;
}
void print() {
for (int i = 0; i < m_ary.length; i++)
System.out.print(m_ary[i] + " ");
System.out.println();
}
void order() {
int j, temp;
boolean exchange = true;
j = m_ary.length - 2;
while (j >= 0 && exchange) {
exchange = false;
for (int i = 0; i <= j; i++)
if (m_ary[i] > m_ary[i + 1]) {
exchange = true;
temp = m_ary[i];
m_ary[i] = m_ary[i + 1];
m_ary[i + 1] = temp;
}
j = j - 1;
}
}
int[] subAry(int a1, int a2) {
int[] sa;
if (a1 >= 0 && a1 < a2 && a2 < m_ary.length) {
sa = new int[a2 - a1 + 1];//2 到6是5个元素
for (int i = 0, j = a1; i < a2-1; i++, j++)
sa[i] = m_ary[j];//a2=6,所以i< a2-1才可以
return sa;
} else {
System.out.println("error index");
return new int[1];
}
}
}
if (a1 >= 0 && a1 < a2 && a2 < m_ary.length) {
sa = new int[a2 - a1 + 1];//2 到6是5个元素
for (int i = 0, j = a1; i < a2-1; i++, j++)
sa[i] = m_ary[j];//a2=6,所以i< a2-1才可以
完整代码
package Test;
public class ArreyTest {
public static void main(String[] args) {
int[] aaa = { 23, 14, 35, 2, 71, 55, 82, 41, 11, 6 };
Ary ay1 = new Ary(aaa);
ay1.print();
ay1.order();
ay1.print();
for (int m = 0; m < aaa.length; m++)
System.out.print("---" + aaa[m]);
System.out.println();
Ary ay2 = new Ary(ay1.subAry(2, 6));
ay2.print();
}
}
class Ary {
int[] m_ary;
Ary(int[] p) {
m_ary = p;
}
void print() {
for (int i = 0; i < m_ary.length; i++)
System.out.print(m_ary[i] + " ");
System.out.println();
}
void order() {
int j, temp;
boolean exchange = true;
j = m_ary.length - 2;
while (j >= 0 && exchange) {
exchange = false;
for (int i = 0; i <= j; i++)
if (m_ary[i] > m_ary[i + 1]) {
exchange = true;
temp = m_ary[i];
m_ary[i] = m_ary[i + 1];
m_ary[i + 1] = temp;
}
j = j - 1;
}
}
int[] subAry(int a1, int a2) {
int[] sa;
if (a1 >= 0 && a1 < a2 && a2 < m_ary.length) {
sa = new int[a2 - a1 + 1];//2 到6是5个元素
for (int i = 0, j = a1; i < a2-1; i++, j++)
sa[i] = m_ary[j];//a2=6,所以i< a2-1才可以
return sa;
} else {
System.out.println("error index");
return new int[1];
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没细看你的代码,不过肯定是49行这一句出错,改成这样:
for(int i=0,j=a1;i<=a2&&i<sa.length&&j<m_ary.length;i++,j++)sa[i]=m_ary[j];
for(int i=0,j=a1;i<=a2&&i<sa.length&&j<m_ary.length;i++,j++)sa[i]=m_ary[j];
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是想搞冒泡排序吧!你看下你这句代码: Ary ay2=new Ary(ay1.subAry(2,6));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询