Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Remove an element at a specific index from an array in Java


import java.util.Arrays;
import java.util.stream.IntStream;
 
class Main
{
    public static int[] remove(int[] a, int index) {
        if (a == null || index < 0 || index >= a.length) {		//lowering the possibilities of an error
            return a;
        }
 	
        else {
          return IntStream.range(0, a.length)
                        .filter(i -> i != index)
                        .map(i -> a[i])
                        .toArray();
        }
    }
 	
    public static void main(String[] args) {		//an example of how to use it
        int[] a = { 1, 2, 3, 4, 5 };
        int index = 2;
 
        a = remove(a, index);
        System.out.println(Arrays.toString(a));
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to do annotation configuration in spring 
Java :: flutter unable to find bundled java version 
Java :: and roid shape setCornerRadii 
Java :: android studio reg get float from numeric string 
Java :: StackAsMyArrayList 
Java :: what is constructor in java 
Java :: android studio clock bar change color programmatically 
Java :: add recyclerview in fragment 
Java :: java anonmyous array eg 
Java :: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config 
Java :: convert linkedlist to integer array java 
Java :: change short video to byte array android 
Java :: neither bindingresult nor plain target object for bean name spring mvc 
Java :: Describe Methods Overloading in Java 
Java :: default java keystore 
Java :: how to run a java file in terminal 
Java :: does not have a NavController set on 2131231008 
Java :: format print java 
Java :: Read File and Resource in JUnit Test into Stream 
Java :: export java path in ubuntu 
Java :: java session timeout 
Java :: put arraylist in hashtable java 
Java :: android get app build name 
Java :: pythagoras method in java 
Java :: android send fragment context 
Java :: arraylist replace 
Java :: java take and save screenshot 
Java :: java clear bufffer to take next string inpuit 
Java :: java user home directory macos 
Java :: how to get config values bukkit 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =