0

Как правильно вывести элементы массива на консоль с учетом удаленного элемента.

public class Remove {
    public static void main(String[] args) {
        int[] arr = {77, 99, 44, 55, 22, 88, 11 , 0, 66, 33, 10} ;
        int count = 0;
        int element = 0;
        int searchKey;

        System.out.println(Arrays.toString(arr));

        searchKey = 77;

        for (int i = 0; i < arr.length; i++){
            if(arr[i] == searchKey){
                element = i;
                count++;
            }
        }

        for(int j = element; j < arr.length; j++){
            arr[j] = arr[j + 1];
        }

        System.out.println(Arrays.toString(arr));
    }
}

2 ответа 2

1

Сначала надо разобраться как корректно удалить элементы из массива, а вывести уже не проблема

import java.util.Arrays;

public class Remove {

    public static void main(String[] args) {
        int[] arr = {77, 99, 44, 55, 22, 88, 11, 0, 66, 33, 10};
        int removeValue=77;
        System.out.println(Arrays.toString(arr));
        
        int[] result = removeByIndex(arr, removeValue);
        System.out.println(Arrays.toString(result));
    }
    
    public static int[] removeByIndex(int[] array, int value) {
        return Arrays.stream(array)
                .filter(i->i!=value)
                .toArray();
    }
    
}

Без стримов есть несколько вариантов, но это в любом случае менее рационально:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Remove {

    public static void main(String[] args) {
        int[] arr = {77, 99, 44, 55, 22, 88, 11, 0, 66, 33, 10};
        System.out.println(Arrays.toString(arr));
        
        System.out.println(removeByIndex(arr, 99));
    }
    
    public static List <Integer> removeByIndex(int [] array, int value) {
        List <Integer> list = new ArrayList<>();
        for (int element : array) {
            if (element!=value) list.add(element);
        }
        return list;
    }
    
}
2
  • как выводит без stream. Я пока не знаю как он работает
    – RZF
    23 фев 2021 в 19:29
  • @Rasim Zamaltdinov ответ дополнил 23 фев 2021 в 20:36
0
import java.util.Arrays;

public class Main
{
    public static void main(String[] args)
    {
        int[] arr = {77, 99, 44, 55, 22, 88, 11, 0, 66, 33, 10};
        
        arr = removeValue(arr, 77);
        
        System.out.println(Arrays.toString(arr));
    }
    
    public static int[] removeValue(int[] array, int value)
    {
        int[] result = new int[array.length -1];
        int c = 0;
        
        for(int i = 0; i < array.length; i++)
        {
            if(array[i] != value)
            {
                result[c] = array[i];
                c++;
            }
        }
        
        return result;
    }
}

Вот вариант без листов, если Вам так будет удобнее.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.