Java program to move all zero at the end of the array with/without using existing data structure ?

Поделиться
HTML-код
  • Опубликовано: 28 янв 2025

Комментарии • 8

  • @ps5gamesview
    @ps5gamesview 2 года назад

    Wow , awesome sir

  • @pushpalathas8089
    @pushpalathas8089 4 года назад

    Does while loop is necessary @ 05:26? Arrays by default initialize with zero's. May be while loop here causes performance.
    package arrays;
    import java.util.Arrays;
    public class MoveAllZerosToEnd {
    public static void main(String[] args) {
    int[] numbers = { -1, 0, -2, 0, -5, -7, 0, 0, 23, 32, -123 };
    System.out.println(Arrays.toString(moveAllZerosToEnd(numbers)));
    }
    private static int[] moveAllZerosToEnd(int[] numbers) {
    int[] temp = new int[numbers.length];
    int runningIndex = 0;
    for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] != 0) {
    temp[runningIndex] = numbers[i];
    runningIndex++;
    }
    }
    return temp;
    }
    }

  • @swatisinghrathore8131
    @swatisinghrathore8131 4 года назад

    Thankyou so much for you work

  • @maheshbabunadigoti7050
    @maheshbabunadigoti7050 4 года назад

    Great sir...

  • @SahanasAllinOneChannel
    @SahanasAllinOneChannel 9 месяцев назад

    int j = 0;
    for (int i = 0; i < arr.length; i++) {
    if (arr[i] != 0) {

    if (i != j) {
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    }
    j++; }
    }

  • @user1902-b7e
    @user1902-b7e 6 месяцев назад

    Can you please do this using java 8

  • @ratnasheelkamble6843
    @ratnasheelkamble6843 Год назад +1

    IntStream.of(nums).boxed().sorted(Comparator.comparing(n->n==0)).forEach(System.out::println);