Chance
Chance

Reputation: 17

How to remove an element from an array

In Java, The teacher taught us how to remove an element from an array without using array utils and so on. So I tried the method he gave to us. It updates the index value exactly as I want. after it changes the index value, I want it to delete the last index, "sizeOfArray-1" but I couldn't do that! Any help?

Here the code:

import java.util.Scanner;

public class Arrays {

static int x[] = { 1, 2, 3, 4, 5, 6, 7 };

static Scanner input = new Scanner(System.in);

public int search(int target) {
    for (int index = 0; index < x.length; index++) {
        if (x[index] == target)
            return index;
    }
    return -1;
}

public void deleteIndex(int target) {
    int deleted = search(target);
    if (deleted == -1)
        System.out.println("Entry Not Found!");
    else {
        x[target] = x[7-1];
    }
}

public static void main(String[] args) {
    Arrays f = new Arrays();
    int counteri = 0;
    int counterj = 0;

    for (int j = 0; j < x.length; j++) {
        System.out.print(counterj + "=>" + x[j] + "  \n");
        counterj++;
    }

    f.deleteIndex(input.nextInt());

    for (int i = 0; i < x.length; i++) {
        System.out.print(counteri + "=>" + x[i] + "   \n");
        counteri++;
    }
}
}

Upvotes: 0

Views: 433

Answers (4)

Gyana Ranjan Tripathy
Gyana Ranjan Tripathy

Reputation: 11

//delete the element the perticular element in a position//
import java.util.*;
class main13
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the range");
        int no=sc.nextInt();
        System.out.println("Enter the array elements");
        int a[]=new int[no];
        int i,j;

        for(i=0;i<no;i++)
        {
            a[i]=sc.nextInt();
        }
        System.out.println("Enter the element you want to delete");
        int d=sc.nextInt();
        for(i=0;i<no;i++)
        {
            if(d==a[i])
            {
                for(j=i;j<no-1;j++)
                {
                    a[j]=a[j+1];
                }

                break;
            }
            else
            {
                System.out.println("Element not found");
                System.exit(0);
            }
        }
        System.out.println("After deletion:");
        for(i=0;i<no-1;i++)
        {
            System.out.println(a[i]);
        }
    }
}

Upvotes: 0

aurelius
aurelius

Reputation: 4076

An array in Java has fixed predefined length, once you initialize the array you cannot actually remove an element from it. The best thing you can do is to create another array containing all the elements of the original array without that specific one.

Upvotes: 0

Lrrr
Lrrr

Reputation: 4817

First of all you have to change this line

x[target] = x[7-1];

to this :

x[deleted] = x[7-1];

because you find an element in your search function, and return its index to deleted so you have to do your action in x[deleted] not x[target]

Your code just replace the actual value of element with amount of last element in here :

else {
    x[target] = x[7-1];
}

So when you want to (so as you call it) delete the last element it just replace last element with it self so it didnot do anything.

You can just simply assign another value that doesnt exist in your array for instance -1 and you could see your function works as you want.

a thing like this :

else {
    x[deleted] = -1;
}

But it is not delete actually, and you cant delete items of array in java.

Upvotes: 1

matsev
matsev

Reputation: 33749

You really cannot delete an item from an array in Java. Here is some pseudo code that shows what you can do instead:

  • create a new array that has size -1 of the original array
  • start looping, keep track of the current index
  • copy the item(s) from the original array to the new array corresponding to the current index if it should be kept (otherwise skip the item that should be removed)
  • return the new array

Upvotes: 1

Related Questions