java 有序数组中插入一个数
本人初学,请教各位前辈:做了一个在有序的数组中插入一个数字,数组依然有序,代码如下:结果发现数组执行插入前和执行插入后没有变,但是在函数中打印是已经插入了?代码写得有点不...
本人初学,请教各位前辈:
做了一个在有序的数组中插入一个数字,数组依然有序,代码如下:
结果发现数组执行插入前和执行插入后没有变,但是在函数中打印是已经插入了?
代码写得有点不规范,请见谅!
class ArryFind
{
public static void main(String[] args)
{
int[]b={1,12,33,47,58,72,97,167};
printArr(b);
inst(b,36);
printArr(b); //插入函数执行后,数组还是原来的???
}
public static int search(int []arry, int key)
{
for (int i=0;i<arry.length-1 ;i++ )
{
if (key<=arry[i])
return i;
}
return arry.length-1;
}
public static void inst(int[]arry, int key)
{
int result=search(arry,key);
System.out.println("result="+result);
int []arry1 = new int [arry.length+1];
for (int j=0;j<=arry1.length-1 ;j++ )
{
if (j<result)
arry1[j]=arry[j];
else if (j>result)
arry1[j]=arry[j-1];
else
arry1[result]=key;
}
System.out.print("arry1=");
printArr(arry1);
System.out.println("");
System.out.print("arry=");
printArr(arry);
System.out.println("");
arry=arry1;
System.out.print("arry1=");
printArr(arry1);
System.out.println("");
System.out.print("arry=");
printArr(arry);
System.out.println("");
}
public static void printArr(int []arry)
{
for (int i=0;i<arry.length ;i++ )
{
if (i<arry.length-1)
System.out.print(arry[i]+",");
else
System.out.println(arry[i]);
}
}
} 展开
做了一个在有序的数组中插入一个数字,数组依然有序,代码如下:
结果发现数组执行插入前和执行插入后没有变,但是在函数中打印是已经插入了?
代码写得有点不规范,请见谅!
class ArryFind
{
public static void main(String[] args)
{
int[]b={1,12,33,47,58,72,97,167};
printArr(b);
inst(b,36);
printArr(b); //插入函数执行后,数组还是原来的???
}
public static int search(int []arry, int key)
{
for (int i=0;i<arry.length-1 ;i++ )
{
if (key<=arry[i])
return i;
}
return arry.length-1;
}
public static void inst(int[]arry, int key)
{
int result=search(arry,key);
System.out.println("result="+result);
int []arry1 = new int [arry.length+1];
for (int j=0;j<=arry1.length-1 ;j++ )
{
if (j<result)
arry1[j]=arry[j];
else if (j>result)
arry1[j]=arry[j-1];
else
arry1[result]=key;
}
System.out.print("arry1=");
printArr(arry1);
System.out.println("");
System.out.print("arry=");
printArr(arry);
System.out.println("");
arry=arry1;
System.out.print("arry1=");
printArr(arry1);
System.out.println("");
System.out.print("arry=");
printArr(arry);
System.out.println("");
}
public static void printArr(int []arry)
{
for (int i=0;i<arry.length ;i++ )
{
if (i<arry.length-1)
System.out.print(arry[i]+",");
else
System.out.println(arry[i]);
}
}
} 展开
2个回答
展开全部
在Java中,数组的长度是不可以改变的,所以你想往一个数组里面添加元素是不可以的。但是你可以把这个数组里面的元素和要添加的元素一起放到另一个数组里面。你的方法我给改了一下,你自己看看吧!
class ArryFind
{
public static void main(String[] args)
{
int[]b={1,12,33,47,58,72,97,167};
printArr(b);
int[]arrys=inst(b,36);
printArr(arrys);
}
public static int search(int []arry, int key)
{
for (int i=0;i<arry.length-1 ;i++ )
{
if (key<=arry[i])
return i;
}
return arry.length-1;
}
public static int[] inst(int[]arry, int key)
{
int result=search(arry,key);
System.out.println("result="+result);
int []arry1 = new int [arry.length+1];
for (int j=0;j<=arry1.length-1 ;j++ )
{
if (j<result)
arry1[j]=arry[j];
else if (j>result)
arry1[j]=arry[j-1];
else
arry1[result]=key;
}
System.out.print("arry1=");
printArr(arry1);
System.out.println("");
System.out.print("arry=");
printArr(arry);
System.out.println("");
arry=arry1;
System.out.print("arry1=");
printArr(arry1);
System.out.println("");
System.out.print("arry=");
printArr(arry);
System.out.println("");
return arry1;
}
public static void printArr(int []arry)
{
for (int i=0;i<arry.length ;i++ )
{
if (i<arry.length-1)
System.out.print(arry[i]+",");
else
System.out.println(arry[i]);
}
}
}
class ArryFind
{
public static void main(String[] args)
{
int[]b={1,12,33,47,58,72,97,167};
printArr(b);
int[]arrys=inst(b,36);
printArr(arrys);
}
public static int search(int []arry, int key)
{
for (int i=0;i<arry.length-1 ;i++ )
{
if (key<=arry[i])
return i;
}
return arry.length-1;
}
public static int[] inst(int[]arry, int key)
{
int result=search(arry,key);
System.out.println("result="+result);
int []arry1 = new int [arry.length+1];
for (int j=0;j<=arry1.length-1 ;j++ )
{
if (j<result)
arry1[j]=arry[j];
else if (j>result)
arry1[j]=arry[j-1];
else
arry1[result]=key;
}
System.out.print("arry1=");
printArr(arry1);
System.out.println("");
System.out.print("arry=");
printArr(arry);
System.out.println("");
arry=arry1;
System.out.print("arry1=");
printArr(arry1);
System.out.println("");
System.out.print("arry=");
printArr(arry);
System.out.println("");
return arry1;
}
public static void printArr(int []arry)
{
for (int i=0;i<arry.length ;i++ )
{
if (i<arry.length-1)
System.out.print(arry[i]+",");
else
System.out.println(arry[i]);
}
}
}
追问
谢谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询