简体   繁体   中英

shifting in the array

I want the code to shift any number less than 20 to the end of the array and only print the numbers greater than 20 but it doesn't it keeps printing 13 and 3 while it shouldn't

this is the output :

The remained : 22 13 3 26

the code :

public class Test {

   public static void main (String[]args){


      int[]cpoies = {22,12,13,4,3,2,26};

      int numOfNumbers=cpoies.length;

      for(int i=0;i<cpoies.length;i++){
         if(cpoies[i] < 20 ){
            for(int j=i;j<cpoies.length-1;j++)
               cpoies[j]=cpoies[j+1];
            --numOfNumbers;
         }}

      System.out.println("The remained : ");

      for(int i=0;i<numOfNumbers;i++){
         System.out.println(cpoies[i]);   
      }


   }}

If your number is less than 20 don't increment i. Because in your code you are moving i+1 element into ith position.

if(cpoies[i] < 20 ){
            for(int j=i;j<cpoies.length-1;j++)
               cpoies[j]=cpoies[j+1];
            --numOfNumbers;
            i--;
         }}

And your code does not swap the element it simply make disappear element. add a temp value to hold swapping element.

temp=cpoies[j];
cpoies[j]=cpoies[j+1];
cpoies[j+1]=temp; 

This is not a swap. You're overwriting existing data in the array instead of swapping it. You need a swap variable.

for(int i=0;i<cpoies.length;i++){
     if(cpoies[i] < 20 ){
        for(int j=i;j<cpoies.length-1;j++)
           cpoies[j]=cpoies[j+1];
        --numOfNumbers;
     }
}

This will print numbers under 20, not over. You're printing all the beginning numbers in the array not the ending:

 for(int i=0;i<numOfNumbers;i++){
         System.out.println(cpoies[i]);   
      }

You can try this,

public class Test {

   public static void main (String[]args){


      int[]cpoies = {22,12,13,4,3,2,26};

      int numOfNumbers=cpoies.length;

      for(int i=0;i<cpoies.length;i++){
         if(cpoies[i] < 20 ){
            int temp = cpoies[i];
            for(int j=i+1;j<cpoies.length;j++) {
               if(cpoies[j] > 20) {
                  cpoies[i] = cpoies[j];
                  cpoies[j] = temp;
                  numOfNumbers--;
               }
            }
         }
     }

      System.out.println("The remained : ");

      for(int i=0;i<=cpoies.length-numOfNumbers;i++){
         System.out.println(cpoies[i]);   
      }


   }}

Besides all the answers already given, here is the shifting of all large numbers to the beginning. a bit more efficiant due to the break comment i hope

public class Test {

   public static void main (String[]args){


      int[]numbers = {22,12,13,4,3,2,26};

      int numOfNumbers=numbers.length;

      for(int i=0;i<numbers.length;i++){
         if(numbers[i] > 20){
            for(int j=i;j>0;j--)
                if (numbers[j-1]>20){
                    break;
                    numOfNumbers=j;
                }
                tempnum=numbers[j];
                numbers[j]=numbers[j-1];
                numbers[j-1]=tempnum;
         }}

      System.out.println("The remained : ");

      for(int i=0;i<=numOfNumbers;i++){
         System.out.println(numbers[i]);   
      }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM