Java 为什么会跳过for循环?
我想按特定顺序(从最小到最大)将产品添加到数组中,但出于某种原因,代码跳过了执行移位部分的for循环。我试着创造条件“products.length-2”,但仍然不起作用Java 为什么会跳过for循环?,java,Java,我想按特定顺序(从最小到最大)将产品添加到数组中,但出于某种原因,代码跳过了执行移位部分的for循环。我试着创造条件“products.length-2”,但仍然不起作用 if(products!=null){ for(int i=0; i<products.length; i++){ if(products[i]!=null && product.getItemNum() < products[i].getItemNum()){ index=
if(products!=null){
for(int i=0; i<products.length; i++){
if(products[i]!=null && product.getItemNum() < products[i].getItemNum()){
index=i;
temp = products[index];
for(int j=products.length-1; j<=0; j--){
products[j+1]= products[j];
}
products[index]= product;
}
}
}
if(产品!=null){
对于(int i=0;i),让我们考虑您的<代码>产品< /代码>数组包含2个元素。
循环的内部的第一次迭代:
for(int j=products.length-1; j<=0; j--){
让我们考虑你的<代码>产品数组包含2个元素。
循环的内部的第一次迭代:
for(int j=products.length-1; j<=0; j--){
正确检查你的If条件。正确检查你的If条件。
for(int j=products.length-1; j >= 0; j--){ // j >= 0
products[j+1]= products[j];
}