-2

I am trying to remove an element from an array using ArrayList. however I cannot seem to get it to work. I'm new to java. Here is the code what i do

package workspace;
import java.util.*;
import java.util.stream.*;
public class Worksapce{
    private static Collection<Integer> returnarrayList;
    static int[] arrayRemover(int[] myArray,int index) {
        List<Integer>arrayList = IntStream.of(myArray) .boxed().collect(Collectors.toList()); 
        arrayList.remove(2); 
        returnarrayList = null;
        return returnarrayList.stream().mapToInt(Integer::intValue).toArray(); 
    }
public static void main(String[] args)  { 
    int myArray[]= {1,2,3,4};
    int index=2;
    myArray=arrayRemover(myArray,index);
    System.out.println(Arrays.toString(myArray));
}
}

Here is the error showing

enter image description here

I want to remove an element from the array.I know there is'nt a direct method in java to remove an element from the array.But there are exist several indirect methods.There I'm trying to remove element using arraylist. I didnt get the result. What are the mistakes in it? What i need to change?

2

2 Answers 2

3

You have the line returnarrayList = null;, then you attempt to invoke a method on null at the next line. I would just create a new array of myArray.length - 1 (because you're removing one element); then copy from 0 to index and then from index + 1 to the new array. Like,

static int[] arrayRemover(int[] myArray, int index) {
    if (myArray == null || myArray.length < 2) {
        return myArray;
    }
    int[] r = new int[myArray.length - 1];
    System.arraycopy(myArray, 0, r, 0, index);
    System.arraycopy(myArray, index + 1, r, index, myArray.length - index - 1);
    return r;
}
0

I've become a big fan of streams for this sort of thing if you're on java 8 or later. It collapses a lot of boilerplate code, often down to one line. Here is a working unit test I put together demonstrating doing this with streams.

import java.io.IOException;
import java.util.Arrays;
import java.util.stream.IntStream;

Those are the imports for the code inside the test method. If you are actually recreating the test itself place the method in a class and include your favorite unit test framework (in my case import org.junit.jupiter.api.Test;).

@Test
public void testRemoveArrayElement() throws IOException {
    int[] myArray = new int[] { 10, 20, 30, 40, 50, 60 };
    int indexToRemove = 3;

    System.out.println(Arrays.toString(myArray));    // Prints: [10, 20, 30, 40, 50, 60]
    int[] myFilteredArray = IntStream.range(0, myArray.length).filter(i -> i != indexToRemove).map(i -> myArray[i]).toArray();
    System.out.println(Arrays.toString(myFilteredArray));    // Prints: [10, 20, 30, 50, 60]
}
2
  • Showing the package org is not accessible
    – Amalshanth
    Commented Jul 23, 2023 at 7:10
  • Odd. Maybe you are recreating the entire unit test? If so, I've updated my answer to include some imports and a word about the test itself. The line of Java streaming code that actually answers the question is 100% standard Java (Java 8 or higher).
    – Todd
    Commented Jul 23, 2023 at 16:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.