Java 算法有时有效
我目前正在尝试为一个数组设置一个排序算法,该算法扫描最高和最低数字,并将它们放入一个新数组中,一次两个。我注意到它似乎只在某些条件下起作用。例如,我可以将它作为{5,3,10,7}或{3,5,10,7}输入,但是{7,3,5,10}生成一个IndexOutOfBoundsException:index 1,size:1 代码如下:Java 算法有时有效,java,arrays,Java,Arrays,我目前正在尝试为一个数组设置一个排序算法,该算法扫描最高和最低数字,并将它们放入一个新数组中,一次两个。我注意到它似乎只在某些条件下起作用。例如,我可以将它作为{5,3,10,7}或{3,5,10,7}输入,但是{7,3,5,10}生成一个IndexOutOfBoundsException:index 1,size:1 代码如下: import java.util.Scanner; // program uses class Scanner import java.util.ArrayList;
import java.util.Scanner; // program uses class Scanner
import java.util.ArrayList; //helps with arrays
public class ArrayAlg
{
// main method begins execution of Java application
public static void main( String[] args )
{
// create a Scanner to obtain input from the command window
Scanner input = new Scanner( System.in );
boolean AS = false; //array sorted.
int hi = 0; //high number
int low = 100; //set to 100 for logic reasons.
int oldhi = 0;
int oldlow = 0;
int NI = 0;
int addA = 0;
int p = 0; //places, moves right after one scan to place the next int
int n = 1; //number of times, moves left after one to place the next int
String cont = "n";
ArrayList<Integer> IParray = new ArrayList<Integer>(); //input array
ArrayList<Integer> Sarray = new ArrayList<Integer>(); //sorted array
while (cont.equals("n"))
{
System.out.print("Please enter a number for the array: ");
addA = input.nextInt();
IParray.add(addA);
NI++;
System.out.print("\n is that all? (y/n): ");
cont = input.next();
}
for (int c = 0; c < NI; c++) //adds 0 so sorting will be easier
Sarray.add(0); //matches the inputted array
System.out.print("The inputted array: ");
System.out.print(IParray);
System.out.println("");
while (AS == false){
hi = 0;
low = 100;
for (int i = 0; i < IParray.size(); i++)
{
if (IParray.get(i) < low)
low = IParray.get(i);
if (IParray.get(i) > hi)
{
//if (IParray.get(i) > hi) currently commented out, doesn't effect the logic by the looks of it.
hi = IParray.get(i);
}
}//end for
Sarray.set(p, low); //sets the number in the left most position then moves to the right
Sarray.set(Sarray.size() - n, hi); //sets the number to the rightmost position and moves left
p++; //increase place count to the right
n++; //increases the place count to the left
oldhi = IParray.indexOf(hi); //oldhi becomes the index of the recent highest number.
oldlow = IParray.indexOf(low); //oldlow becomes the index of the recent lowest number
IParray.remove(oldhi); //removes the highest number at the index
IParray.remove(oldlow); //removes the lowest number at the index, exceptions occurs right here.
System.out.print("The inputted array: ");//mostly here to see what the inputted array looks like after one iteration
System.out.print(IParray);
System.out.println("");
System.out.print("The sorted array: ");
System.out.print(Sarray);
System.out.println("");
if (IParray.isEmpty() == true) //checks to see if the input array is empty
AS = true;
else
AS = false;
}//end while
} // end method main
} // end class ArrayAlg
import java.util.Scanner;//程序使用类扫描程序
导入java.util.ArrayList//帮助处理数组
公共类排列
{
//main方法开始执行Java应用程序
公共静态void main(字符串[]args)
{
//创建扫描仪以从命令窗口获取输入
扫描仪输入=新扫描仪(System.in);
布尔值AS=false;//数组已排序。
int hi=0;//高数值
int low=100;//出于逻辑原因设置为100。
int-oldhi=0;
int-oldlow=0;
int-NI=0;
int addA=0;
int p=0;//places,在一次扫描后向右移动以放置下一个int
int n=1;//次数,在一次之后向左移动以放置下一个int
字符串cont=“n”;
ArrayList IParray=新ArrayList();//输入数组
ArrayList Sarray=新的ArrayList();//排序数组
而(续等于(“n”))
{
System.out.print(“请为数组输入一个数字:”);
addA=input.nextInt();
IParray.add(addA);
NI++;
System.out.print(“\n就这些吗?(y/n):”;
cont=input.next();
}
对于(int c=0;chi)
{
//如果(IParray.get(i)>hi)当前已被注释掉,则它的外观不会影响逻辑。
hi=IParray.get(i);
}
}//结束
Sarray.set(p,low);//在最左边的位置设置数字,然后向右移动
Sarray.set(Sarray.size()-n,hi);//将数字设置到最右边的位置并向左移动
p++;//向右增加位置计数
n++;//增加左侧的位置计数
oldhi=IParray.indexOf(hi);//oldhi成为最近最高数字的索引。
oldlow=IParray.indexOf(low);//oldlow成为最近最低数字的索引
IParray.remove(oldhi);//删除索引处的最大数字
IParray.remove(oldlow);//删除索引中的最小数字,异常就发生在这里。
System.out.print(“输入的数组:”);//这里主要是查看一次迭代后输入的数组的外观
系统输出打印(IParray);
System.out.println(“”);
System.out.print(“排序数组:”);
系统输出打印(Sarray);
System.out.println(“”);
if(IParray.isEmpty()==true)//检查输入数组是否为空
AS=真;
其他的
AS=假;
}//结束时
}//结束方法main
}//结束类数组
有人能给我一些关于为什么会发生这种情况的提示吗?我已经试了一个多小时了。尝试用谷歌搜索此网站的答案,但没有运气您可能无法找到比以前更高的值和更低的值。例如,输入数组是
{7,3,5,10}
,现在hi
是10,low
是7,它们已从IParray
中删除
for (int i = 0; i < IParray.size(); i++)
{
if (IParray.get(i) < low)
low = IParray.get(i); // low becomes 3
if (IParray.get(i) > hi) //!!! never able to find a higher value than 10 !!!
{
//if (IParray.get(i) > hi) currently commented out, doesn't effect the logic by the looks of it.
hi = IParray.get(i);
}
}
您正在使索引越界,因为它试图删除一个不存在的索引。当删除索引时,它首先删除最低的索引,导致整个数组索引向下移动。它不保留其原始索引
[0] = 7
[1] = 3
[2] = 5
[3] = 10
删除高位数字10时,它将变为:
[0] = 7
[1] = 3
[2] = 5
删除低数值时,它将变为:
[0] = 7
[1] = 5
下一个循环首先删除7,在索引0处只留下5,但代码正在调用以删除索引1,这会导致越界异常
为了解决这个问题,让它在调用remove方法时动态地获取索引
IParray.remove(IParray.indexOf(hi)); //removes the highest number at the index
IParray.remove(IParray.indexOf(low)); //removes the lowest number at the index
通过在调试模式下运行代码,然后单步执行引发异常的代码(第73行),您应该能够快速找到此错误 哪一行产生异常?数组中的数字是偶数,还是仅奇数?K_g:第73行。大卫,偶数和奇数都有。Paul:n和p将数组中的hi和low数字设置为某个索引。我将值重置为low=100;hi=0;在while的开始,这样如果循环继续,他们就不会指向以前的值。现在我明白了,现在我开始工作了,我将已经使用的数字设置为-1,并且排序算法在扫描时不包括这些数字,谢谢。现在修复退出条件欢迎您。通过调试可以很容易地发现这类错误。我强烈建议您仔细阅读有问题的代码,观察数组和变量的变化。
IParray.remove(IParray.indexOf(hi)); //removes the highest number at the index
IParray.remove(IParray.indexOf(low)); //removes the lowest number at the index