remove element from a specific index from an array in java

Solutions on MaxInterview for remove element from a specific index from an array in java by the best coders in the world

showing results for - "remove element from a specific index from an array in java"
Lori
29 Aug 2020
1// Java program to delete specified integer from an array
2import java.util.Scanner;
3public class DeleteSpecifiedInteger
4{
5   public static void main(String[] args)
6   {
7      int num, n, temp = 1, place = 0;
8      Scanner sc = new Scanner(System.in);
9      System.out.println("Please enter number of elements: ");
10      num = sc.nextInt();
11      int[] arrNum = new int[num];
12      System.out.println("Please enter all the elements: ");
13      for(int a = 0; a < num; a++)
14      {
15         arrNum[a] = sc.nextInt();
16      }
17      System.out.println("Enter the element you want to delete: ");
18      n = sc.nextInt();
19      for(int a = 0; a < num; a++)
20      {
21         if(arrNum[a] == n)
22         {
23            temp = 1;
24            place = a;
25            break;
26         }
27         else
28         {
29            temp = 0;
30         }
31      }
32      if(temp == 1)
33      {
34         for(int a = place + 1; a < num; a++)
35         {
36            arrNum[a - 1] = arrNum[a];
37         }
38         System.out.println("After deleting element: ");
39         for(int a = 0; a < num - 2; a++)
40         {
41            System.out.print(arrNum[a] + ",");
42         }
43         System.out.print(arrNum[num - 2]);
44      }
45      else
46      {
47         System.out.println("Element not found!!");
48      }
49      sc.close();
50   }
51}
Luka
03 Oct 2020
1// remove element from a specific index from an array
2import java.util.Arrays;
3public class DeleteElementDemo
4{
5   // remove element method 
6   public static int[] removeElement(int[] arrGiven, int index) 
7   {
8      // if empty 
9      if(arrGiven == null || index < 0 || index >= arrGiven.length) 
10      {
11         return arrGiven; 
12      }
13      // creating another array one less than initial array 
14      int[] newArray = new int[arrGiven.length - 1];
15      // copying elements except index 
16      for(int a = 0, b = 0; a < arrGiven.length; a++) 
17      { 
18         if(a == index)
19         {
20            continue;
21         }
22         newArray[b++] = arrGiven[a]; 
23      }
24      return newArray; 
25   }
26   public static void main(String[] args) 
27   { 
28      int[] arrInput = { 2, 4, 6, 8, 10 };
29      // printing given array 
30      System.out.println("Given array: " + Arrays.toString(arrInput));
31      // getting specified index 
32      int index = 3;
33      // print index 
34      System.out.println("Index to be removed: " + index);
35      // removing element 
36      arrInput = removeElement(arrInput, index);
37      // printing new array 
38      System.out.println("New array: " + Arrays.toString(arrInput));
39   }
40}
queries leading to this page
how to remove an index from an array javahow to remove a specific element from an array in javaint array remove javaarrays delete integer javajava remove index from arrayremove item in array java and pull indexremove element from a specific index from an array in javaremove element index from array javasimple java program to delete an element in an arrayremoving specific elements from an array javahow to delete value with index in array javahow to remove one element from array in java by indexjava remove from array indexremove specific element from arrayliat javaremove array element at specific index javashow to remove specific element from array in javahow to remove an element from an integer array in javaremove from int array javadelete an element from an array at specified position in javajava remove a value by index from arrayremove specific element from array java inbuilthow to delete some specific index from array in javaremove from specific index to end in javajava array remove element at indexjava program to remove a specific element from an array java how to remove int from arrayremove an element at specific index from an array in javajava remove from array at indexdelete index in array javahow to remove element at a particular index in javaarray remove element at index javaarray remove by index javaremove element by index javadelete element in array at index javahow to get index of remove element from array in javaremove array element at specific index javadeleting from array using index in javajava program on remove element from string arrayhow to remove index from array javahow to remove an int from an array javaremove specific element from array javaremove from array with index javahow to remove an index from array javahow to remove specific index from arraylist in javajava remove specific index from array stringjava program to remove a specific element from an arrayhaving new array when index is remove from array javaremove element using index from array javawrite a java program to remove a specific element from an array how to remove a specific element from an array in java 3dremove a part of an array javajava remove specific element from an arraydelete from int array javahow to remove specific index value from array in javadelete an item in array javahow to delete element specific index from array javaremove item by index from array javadelete the element in array in java specific indexremove int from array javajava array remove element by indexremove element of array java at indexdelete element from array at given index in javaremove an index from array javahow to remove element from specific index in java arraylistdelete an integer in int array javaremvoe certain index from array list javaremove specific element from java arrayhow to remove value at index in an array javajava program to delete specified integer from an arrayremove index from array javadeleting element in array java from specific positionjava remove specific index from array of stringjava remove specific element from arrayjava remove specific value from arrayremove specific index from array javaremove a specific element from an array javajava remove element from array by indexjava delete index from arraydelete element in array by index javahow to remove an index from an array in javaremove element from a specific index from an array in java