Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to delete an element from an array in java data structure

import java.util.Arrays; 
class Main {   
public static void main(String[] args) 
    {   
        // define original array 
        int[] tensArray = { 10,20,30,40,50,60}; 
 
        // Print the original array 
        System.out.println("Original Array: " + Arrays.toString(tensArray)); 
 
        // the index at which the element in the array is to be removed 
        int rm_index = 2; 
 
        // display index 
        System.out.println("Element to be removed at index: " + rm_index); 
 
        // if array is empty or index is out of bounds, removal is not possible 
          if (tensArray == null
            || rm_index< 0
            || rm_index>= tensArray.length) { 
 
            System.out.println("No removal operation can be performed!!");
        } 
        // Create a proxy array of size one less than original array
        int[] proxyArray = new int[tensArray.length - 1]; 
 
        // copy all the elements in the original to proxy array except the one at index 
        for (int i = 0, k = 0; i <tensArray.length; i++) { 
 
            // check if index is crossed, continue without copying 
            if (i == rm_index) { 
                continue; 
            } 
 
            // else copy the element
            proxyArray[k++] = tensArray[i]; 
        } 
 
       // Print the copied proxy array 
       System.out.println("Array after removal operation: " + Arrays.toString(proxyArray)); 
    } 
}
Comment

PREVIOUS NEXT
Code Example
Java :: bukkit random 
Java :: get free player inventory slots spigot 
Java :: How to find the maximum occurring character in a given String? using hashmap java 
Java :: Java Creating a Java LinkedList 
Java :: type variable java 
Java :: java get year difference between two dates 
Java :: android studio Toast usage 
Java :: .jar window immediately closes on doubleclick 
Java :: jsp check if request parameter exists 
Java :: read excel file in java and store into arraylist 
Java :: how to read a table from text file in java 
Java :: date and time java 
Java :: Implementation of TreeMap Class in Java map 
Java :: mod 10e9+7 in java 
Java :: boolean operators in java 
Java :: what is static method in oop 
Java :: java hashmap remove by condition 
Java :: java exponencial 
Java :: Compare two csv files using java 
Java :: java clear scanner 
Java :: jdk jre jvm 
Java :: if ternaire java 
Java :: click on recyclerview item android animation 
Java :: java string loop backwards 
Java :: Traversing through java map foreach 
Java :: lambda function java 
Java :: hashtable in java 
Java :: control structures in java 
Java :: how to add element to dictionary 
Java :: islowercase java 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =